Get, getbyUid, restore folder and archive folder
This commit is contained in:
parent
18808169bd
commit
a90c52167a
127
src/front/Api/LeCoffreApi/SuperAdmin/Folders/Folders.ts
Normal file
127
src/front/Api/LeCoffreApi/SuperAdmin/Folders/Folders.ts
Normal file
@ -0,0 +1,127 @@
|
||||
import { Service } from "typedi";
|
||||
import { OfficeFolder } from "le-coffre-resources/dist/Notary";
|
||||
import BaseSuperAdmin from "../BaseSuperAdmin";
|
||||
import { EFolderStatus } from "le-coffre-resources/dist/Customer/OfficeFolder";
|
||||
|
||||
// TODO Type get query params -> Where + inclue + orderby
|
||||
export interface IGetFoldersParams {
|
||||
q?: {};
|
||||
}
|
||||
|
||||
// TODO Type getbyuid query params
|
||||
|
||||
export type IPutFoldersParams = {
|
||||
uid?: OfficeFolder["uid"];
|
||||
folder_number?: OfficeFolder["folder_number"];
|
||||
name?: OfficeFolder["name"];
|
||||
description?: OfficeFolder["description"];
|
||||
archived_description?: OfficeFolder["archived_description"];
|
||||
status?: OfficeFolder["status"];
|
||||
};
|
||||
|
||||
@Service()
|
||||
export default class Folders extends BaseSuperAdmin {
|
||||
private static instance: Folders;
|
||||
private readonly baseURl = this.namespaceUrl.concat("/folders");
|
||||
|
||||
private constructor() {
|
||||
super();
|
||||
}
|
||||
|
||||
public static getInstance() {
|
||||
if (!this.instance) {
|
||||
return new this();
|
||||
} else {
|
||||
return this.instance;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @description : Get all folders
|
||||
*/
|
||||
public async get(q: IGetFoldersParams): Promise<OfficeFolder[]> {
|
||||
const url = new URL(this.baseURl);
|
||||
Object.entries(q).forEach(([key, value]) => url.searchParams.set(key, JSON.stringify(value)));
|
||||
try {
|
||||
return await this.getRequest<OfficeFolder[]>(url);
|
||||
} catch (err) {
|
||||
this.onError(err);
|
||||
return Promise.reject(err);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @description : Get a folder by uid
|
||||
*/
|
||||
public async getByUid(uid: string, q?: any): Promise<OfficeFolder> {
|
||||
const url = new URL(this.baseURl.concat(`/${uid}`));
|
||||
if (q) Object.entries(q).forEach(([key, value]) => url.searchParams.set(key, JSON.stringify(value)));
|
||||
try {
|
||||
return await this.getRequest<OfficeFolder>(url);
|
||||
} catch (err) {
|
||||
this.onError(err);
|
||||
return Promise.reject(err);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @description : Create a folder
|
||||
*/
|
||||
// public async post(body: IPostFoldersParams): Promise<OfficeFolder> {
|
||||
// const url = new URL(this.baseURl);
|
||||
// try {
|
||||
// return await this.postRequest<OfficeFolder>(url, body);
|
||||
// } catch (err) {
|
||||
// this.onError(err);
|
||||
// return Promise.reject(err);
|
||||
// }
|
||||
// }
|
||||
|
||||
/**
|
||||
* @description : Update the folder description
|
||||
*/
|
||||
public async put(uid: string, body: IPutFoldersParams): Promise<OfficeFolder> {
|
||||
const url = new URL(this.baseURl.concat(`/${uid}`));
|
||||
try {
|
||||
return await this.putRequest<OfficeFolder>(url, body);
|
||||
} catch (err) {
|
||||
this.onError(err);
|
||||
return Promise.reject(err);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @description : Delete a folder only if the folder don't contains customers
|
||||
*/
|
||||
public async delete(uid: string): Promise<OfficeFolder> {
|
||||
const url = new URL(this.baseURl.concat(`/${uid}`));
|
||||
const targetedFolder = await this.getByUid(uid);
|
||||
if (targetedFolder.office_folder_has_customers) return Promise.reject(`The folder ${uid} contains customers`);
|
||||
try {
|
||||
return await this.deleteRequest<OfficeFolder>(url);
|
||||
} catch (err) {
|
||||
this.onError(err);
|
||||
return Promise.reject(err);
|
||||
}
|
||||
}
|
||||
|
||||
public async archive(uid: string, body: IPutFoldersParams): Promise<OfficeFolder> {
|
||||
body.status = EFolderStatus.ARCHIVED;
|
||||
try {
|
||||
return await this.put(uid, body);
|
||||
} catch (err) {
|
||||
this.onError(err);
|
||||
return Promise.reject(err);
|
||||
}
|
||||
}
|
||||
|
||||
public async restore(uid: string, body: IPutFoldersParams): Promise<OfficeFolder> {
|
||||
body.status = EFolderStatus.LIVE;
|
||||
try {
|
||||
return await this.put(uid, body);
|
||||
} catch (err) {
|
||||
this.onError(err);
|
||||
return Promise.reject(err);
|
||||
}
|
||||
}
|
||||
}
|
@ -85,6 +85,7 @@ export default function FolderBoxInformation(props: IProps) {
|
||||
|
||||
function formatDate(date: Date | null): string {
|
||||
if (!date) return "...";
|
||||
if (!(date instanceof Date)) date = new Date(date);
|
||||
return date.toLocaleDateString("fr-FR", {
|
||||
year: "numeric",
|
||||
month: "long",
|
||||
|
@ -6,8 +6,6 @@ import FolderListContainer from "@Front/Components/DesignSystem/FolderListContai
|
||||
import Header from "@Front/Components/DesignSystem/Header";
|
||||
import Version from "@Front/Components/DesignSystem/Version";
|
||||
import BackArrow from "@Front/Components/Elements/BackArrow";
|
||||
import { folders } from "@Front/Components/Layouts/DesignSystem/dummyData";
|
||||
import { foldersArchived } from "@Front/Components/Layouts/DesignSystem/dummyData";
|
||||
import WindowStore from "@Front/Stores/WindowStore";
|
||||
import classNames from "classnames";
|
||||
import { OfficeFolder } from "le-coffre-resources/dist/Customer";
|
||||
@ -15,7 +13,8 @@ import Image from "next/image";
|
||||
import React, { ReactNode } from "react";
|
||||
|
||||
import classes from "./classes.module.scss";
|
||||
|
||||
import Folders, { IGetFoldersParams } from "@Front/Api/LeCoffreApi/SuperAdmin/Folders/Folders";
|
||||
import { EFolderStatus } from "le-coffre-resources/dist/Customer/OfficeFolder";
|
||||
|
||||
type IProps = {
|
||||
title: string;
|
||||
@ -42,6 +41,7 @@ export type IDashBoardFolder = {
|
||||
created_at: OfficeFolder["created_at"];
|
||||
office_folder_has_customers?: OfficeFolder["office_folder_has_customers"];
|
||||
archived_description: OfficeFolder["archived_description"];
|
||||
status: OfficeFolder["status"];
|
||||
};
|
||||
|
||||
export default class DefaultNotaryDashboard extends React.Component<IProps, IState> {
|
||||
@ -108,17 +108,23 @@ export default class DefaultNotaryDashboard extends React.Component<IProps, ISta
|
||||
);
|
||||
}
|
||||
|
||||
public override componentDidMount(): void {
|
||||
public override async componentDidMount() {
|
||||
this.onWindowResize = WindowStore.getInstance().onResize((window) => this.onResize(window));
|
||||
/**
|
||||
* We set folders state according to the isArchived props
|
||||
* TODO: Front connexion we need to get from bdd
|
||||
*/
|
||||
if (this.props.isArchived) {
|
||||
this.setState({ folders: foldersArchived });
|
||||
} else {
|
||||
this.setState({ folders: folders });
|
||||
}
|
||||
let targetedStatus: EFolderStatus = EFolderStatus["LIVE" as keyof typeof EFolderStatus];
|
||||
if (this.props.isArchived) targetedStatus = EFolderStatus.ARCHIVED;
|
||||
const query: IGetFoldersParams = {
|
||||
q: {
|
||||
where: { status: targetedStatus },
|
||||
include: {
|
||||
deed: { include: { deed_type: true } },
|
||||
office: true,
|
||||
office_folder_has_customers: { include: { customer: { include: { contact: true } } } },
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
const folders = await Folders.getInstance().get(query);
|
||||
this.setState({ folders: folders });
|
||||
}
|
||||
public override componentWillUnmount() {
|
||||
this.onWindowResize();
|
||||
|
@ -57,6 +57,7 @@ export default class ClientSection extends React.Component<IProps, IState> {
|
||||
}
|
||||
|
||||
private doesFolderHaveCustomer(): boolean {
|
||||
return this.props.folder.office_folder_has_customers !== undefined;
|
||||
if (!this.props.folder?.office_folder_has_customers) return false;
|
||||
return this.props.folder?.office_folder_has_customers!.length > 0;
|
||||
}
|
||||
}
|
||||
|
@ -7,17 +7,20 @@ import InputField from "@Front/Components/DesignSystem/Form/Elements/InputField"
|
||||
import Confirm from "@Front/Components/DesignSystem/Modal/Confirm";
|
||||
import QuantityProgressBar from "@Front/Components/DesignSystem/QuantityProgressBar";
|
||||
import Typography, { ITypo, ITypoColor } from "@Front/Components/DesignSystem/Typography";
|
||||
import { folders } from "@Front/Components/Layouts/DesignSystem/dummyData";
|
||||
import DefaultNotaryDashboard, { IDashBoardFolder } from "@Front/Components/LayoutTemplates/DefaultNotaryDashboard";
|
||||
import Module from "@Front/Config/Module";
|
||||
import Link from "next/link";
|
||||
import { useRouter } from "next/router";
|
||||
import { NextRouter, useRouter } from "next/router";
|
||||
import BasePage from "../../Base";
|
||||
import classes from "./classes.module.scss";
|
||||
import ClientSection from "./ClientSection";
|
||||
import Folders from "@Front/Api/LeCoffreApi/SuperAdmin/Folders/Folders";
|
||||
import { OfficeFolder } from "le-coffre-resources/dist/Customer";
|
||||
import { EFolderStatus } from "le-coffre-resources/dist/Customer/OfficeFolder";
|
||||
|
||||
|
||||
type IProps = {};
|
||||
type IProps = {
|
||||
router: NextRouter;
|
||||
};
|
||||
|
||||
type IPropsClass = IProps & {
|
||||
selectedFolderUid: string;
|
||||
@ -37,6 +40,7 @@ class FolderInformationClass extends BasePage<IPropsClass, IState> {
|
||||
this.onSelectedFolder = this.onSelectedFolder.bind(this);
|
||||
this.openArchivedModal = this.openArchivedModal.bind(this);
|
||||
this.closeArchivedModal = this.closeArchivedModal.bind(this);
|
||||
this.onArchivedModalAccepted = this.onArchivedModalAccepted.bind(this);
|
||||
}
|
||||
|
||||
// TODO: Message if the user has not created any folder yet
|
||||
@ -85,6 +89,7 @@ class FolderInformationClass extends BasePage<IPropsClass, IState> {
|
||||
</div>
|
||||
<Confirm
|
||||
isOpen={this.state.isArchivedModalOpen}
|
||||
onAccept={this.onArchivedModalAccepted}
|
||||
onClose={this.closeArchivedModal}
|
||||
closeBtn
|
||||
header={"Archiver le dossier ?"}
|
||||
@ -111,16 +116,14 @@ class FolderInformationClass extends BasePage<IPropsClass, IState> {
|
||||
);
|
||||
}
|
||||
public override async componentDidMount() {
|
||||
for (const folder of folders) {
|
||||
if (folder.uid === this.props.selectedFolderUid) {
|
||||
this.setState({ selectedFolder: folder });
|
||||
break;
|
||||
}
|
||||
}
|
||||
this.setState({
|
||||
selectedFolder: await this.getFolder(),
|
||||
});
|
||||
}
|
||||
|
||||
private doesFolderHaveCustomer(): boolean {
|
||||
return this.state.selectedFolder?.office_folder_has_customers !== undefined;
|
||||
if (!this.state.selectedFolder?.office_folder_has_customers) return false;
|
||||
return this.state.selectedFolder?.office_folder_has_customers!.length > 0;
|
||||
}
|
||||
|
||||
private onSelectedFolder(folder: IDashBoardFolder): void {
|
||||
@ -134,11 +137,30 @@ class FolderInformationClass extends BasePage<IPropsClass, IState> {
|
||||
private closeArchivedModal(): void {
|
||||
this.setState({ isArchivedModalOpen: false });
|
||||
}
|
||||
|
||||
private async onArchivedModalAccepted() {
|
||||
if (!this.state.selectedFolder) return;
|
||||
await Folders.getInstance().archive(this.state.selectedFolder.uid, this.state.selectedFolder);
|
||||
this.closeArchivedModal();
|
||||
this.props.router.push(Module.getInstance().get().modules.pages.Folder.props.path);
|
||||
}
|
||||
|
||||
private async getFolder(): Promise<OfficeFolder> {
|
||||
const query = {
|
||||
q: {
|
||||
deed: { include: { deed_type: "true" } },
|
||||
office: "true",
|
||||
office_folder_has_customers: { include: { customer: { include: { contact: true } } } },
|
||||
},
|
||||
};
|
||||
const folder = await Folders.getInstance().getByUid(this.props.selectedFolderUid, query);
|
||||
return folder;
|
||||
}
|
||||
}
|
||||
|
||||
export default function FolderInformation(props: IProps) {
|
||||
const router = useRouter();
|
||||
let { folderUid } = router.query;
|
||||
folderUid = folderUid as string;
|
||||
return <FolderInformationClass {...props} selectedFolderUid={folderUid} />;
|
||||
return <FolderInformationClass {...props} selectedFolderUid={folderUid} router={router} />;
|
||||
}
|
||||
|
@ -5,15 +5,20 @@ import Button, { EButtonVariant } from "@Front/Components/DesignSystem/Button";
|
||||
import FolderBoxInformation, { EFolderBoxInformationType } from "@Front/Components/DesignSystem/FolderBoxInformation";
|
||||
import QuantityProgressBar from "@Front/Components/DesignSystem/QuantityProgressBar";
|
||||
import Typography, { ITypo, ITypoColor } from "@Front/Components/DesignSystem/Typography";
|
||||
import { folders } from "@Front/Components/Layouts/DesignSystem/dummyData";
|
||||
import DefaultNotaryDashboard, { IDashBoardFolder } from "@Front/Components/LayoutTemplates/DefaultNotaryDashboard";
|
||||
import { useRouter } from "next/router";
|
||||
import { NextRouter, useRouter } from "next/router";
|
||||
|
||||
import BasePage from "../../Base";
|
||||
import classes from "./classes.module.scss";
|
||||
import ClientSection from "./ClientSection";
|
||||
import { OfficeFolder } from "le-coffre-resources/dist/Customer";
|
||||
import Folders from "@Front/Api/LeCoffreApi/SuperAdmin/Folders/Folders";
|
||||
import { EFolderStatus } from "le-coffre-resources/dist/Customer/OfficeFolder";
|
||||
import Module from "@Front/Config/Module";
|
||||
|
||||
type IProps = {};
|
||||
type IProps = {
|
||||
router: NextRouter;
|
||||
};
|
||||
|
||||
type IPropsClass = IProps & {
|
||||
selectedFolderUid: string;
|
||||
@ -33,6 +38,7 @@ class FolderInformationClass extends BasePage<IPropsClass, IState> {
|
||||
this.onSelectedFolder = this.onSelectedFolder.bind(this);
|
||||
this.openArchivedModal = this.openArchivedModal.bind(this);
|
||||
this.closeArchivedModal = this.closeArchivedModal.bind(this);
|
||||
this.restoreFolder = this.restoreFolder.bind(this);
|
||||
}
|
||||
|
||||
// TODO: Message if the user has not created any folder yet
|
||||
@ -101,13 +107,8 @@ class FolderInformationClass extends BasePage<IPropsClass, IState> {
|
||||
);
|
||||
}
|
||||
public override async componentDidMount() {
|
||||
// TODO : GET Current folder from the api
|
||||
for (const folder of folders) {
|
||||
if (folder.uid === this.props.selectedFolderUid) {
|
||||
this.setState({ selectedFolder: folder });
|
||||
break;
|
||||
}
|
||||
}
|
||||
const folder = await this.getFolder();
|
||||
this.setState({ selectedFolder: folder });
|
||||
}
|
||||
|
||||
private doesFolderHaveCustomer(): boolean {
|
||||
@ -118,8 +119,12 @@ class FolderInformationClass extends BasePage<IPropsClass, IState> {
|
||||
this.setState({ selectedFolder: folder });
|
||||
}
|
||||
|
||||
private restoreFolder() {}
|
||||
// should call the api to restore the folder
|
||||
private async restoreFolder() {
|
||||
if (!this.state.selectedFolder) return;
|
||||
await Folders.getInstance().restore(this.state.selectedFolder.uid, this.state.selectedFolder);
|
||||
this.props.router.push(Module.getInstance().get().modules.pages.Folder.pages.FolderArchived.props.path);
|
||||
}
|
||||
|
||||
private openArchivedModal(): void {
|
||||
this.setState({ isArchivedModalOpen: true });
|
||||
}
|
||||
@ -127,11 +132,23 @@ class FolderInformationClass extends BasePage<IPropsClass, IState> {
|
||||
private closeArchivedModal(): void {
|
||||
this.setState({ isArchivedModalOpen: false });
|
||||
}
|
||||
|
||||
private async getFolder(): Promise<OfficeFolder> {
|
||||
const query = {
|
||||
q: {
|
||||
deed: { include: { deed_type: "true" } },
|
||||
office: "true",
|
||||
office_folder_has_customers: { include: { customer: { include: { contact: true } } } },
|
||||
},
|
||||
};
|
||||
const folder = await Folders.getInstance().getByUid(this.props.selectedFolderUid, query);
|
||||
return folder;
|
||||
}
|
||||
}
|
||||
|
||||
export default function FolderInformation(props: IProps) {
|
||||
const router = useRouter();
|
||||
let { folderUid } = router.query;
|
||||
folderUid = folderUid as string;
|
||||
return <FolderInformationClass {...props} selectedFolderUid={folderUid} />;
|
||||
return <FolderInformationClass {...props} selectedFolderUid={folderUid} router={router} />;
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user