From a90c52167a84d47dae44d8d091757737c0e41368 Mon Sep 17 00:00:00 2001 From: Hugo Lextrait Date: Thu, 27 Apr 2023 11:41:55 +0200 Subject: [PATCH] Get, getbyUid, restore folder and archive folder --- .../LeCoffreApi/SuperAdmin/Folders/Folders.ts | 127 ++++++++++++++++++ .../FolderBoxInformation/index.tsx | 1 + .../DefaultNotaryDashboard/index.tsx | 32 +++-- .../FolderInformation/ClientSection/index.tsx | 3 +- .../Folder/FolderInformation/index.tsx | 46 +++++-- .../FolderInformation/index.tsx | 43 ++++-- 6 files changed, 213 insertions(+), 39 deletions(-) create mode 100644 src/front/Api/LeCoffreApi/SuperAdmin/Folders/Folders.ts diff --git a/src/front/Api/LeCoffreApi/SuperAdmin/Folders/Folders.ts b/src/front/Api/LeCoffreApi/SuperAdmin/Folders/Folders.ts new file mode 100644 index 00000000..5e0e41c9 --- /dev/null +++ b/src/front/Api/LeCoffreApi/SuperAdmin/Folders/Folders.ts @@ -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 { + const url = new URL(this.baseURl); + Object.entries(q).forEach(([key, value]) => url.searchParams.set(key, JSON.stringify(value))); + try { + return await this.getRequest(url); + } catch (err) { + this.onError(err); + return Promise.reject(err); + } + } + + /** + * @description : Get a folder by uid + */ + public async getByUid(uid: string, q?: any): Promise { + 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(url); + } catch (err) { + this.onError(err); + return Promise.reject(err); + } + } + + /** + * @description : Create a folder + */ + // public async post(body: IPostFoldersParams): Promise { + // const url = new URL(this.baseURl); + // try { + // return await this.postRequest(url, body); + // } catch (err) { + // this.onError(err); + // return Promise.reject(err); + // } + // } + + /** + * @description : Update the folder description + */ + public async put(uid: string, body: IPutFoldersParams): Promise { + const url = new URL(this.baseURl.concat(`/${uid}`)); + try { + return await this.putRequest(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 { + 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(url); + } catch (err) { + this.onError(err); + return Promise.reject(err); + } + } + + public async archive(uid: string, body: IPutFoldersParams): Promise { + 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 { + body.status = EFolderStatus.LIVE; + try { + return await this.put(uid, body); + } catch (err) { + this.onError(err); + return Promise.reject(err); + } + } +} diff --git a/src/front/Components/DesignSystem/FolderBoxInformation/index.tsx b/src/front/Components/DesignSystem/FolderBoxInformation/index.tsx index de2e85d5..ca63f75d 100644 --- a/src/front/Components/DesignSystem/FolderBoxInformation/index.tsx +++ b/src/front/Components/DesignSystem/FolderBoxInformation/index.tsx @@ -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", diff --git a/src/front/Components/LayoutTemplates/DefaultNotaryDashboard/index.tsx b/src/front/Components/LayoutTemplates/DefaultNotaryDashboard/index.tsx index 1b20db23..8170b030 100644 --- a/src/front/Components/LayoutTemplates/DefaultNotaryDashboard/index.tsx +++ b/src/front/Components/LayoutTemplates/DefaultNotaryDashboard/index.tsx @@ -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 { @@ -108,17 +108,23 @@ export default class DefaultNotaryDashboard extends React.Component 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(); diff --git a/src/front/Components/Layouts/Folder/FolderInformation/ClientSection/index.tsx b/src/front/Components/Layouts/Folder/FolderInformation/ClientSection/index.tsx index b0d4b830..6b6f6ac8 100644 --- a/src/front/Components/Layouts/Folder/FolderInformation/ClientSection/index.tsx +++ b/src/front/Components/Layouts/Folder/FolderInformation/ClientSection/index.tsx @@ -57,6 +57,7 @@ export default class ClientSection extends React.Component { } 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; } } diff --git a/src/front/Components/Layouts/Folder/FolderInformation/index.tsx b/src/front/Components/Layouts/Folder/FolderInformation/index.tsx index bc2ef367..e1ad4324 100644 --- a/src/front/Components/Layouts/Folder/FolderInformation/index.tsx +++ b/src/front/Components/Layouts/Folder/FolderInformation/index.tsx @@ -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 { 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 { { ); } 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 { 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 { + 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 ; + return ; } diff --git a/src/front/Components/Layouts/FolderArchived/FolderInformation/index.tsx b/src/front/Components/Layouts/FolderArchived/FolderInformation/index.tsx index 1e249bfb..2f8812c7 100644 --- a/src/front/Components/Layouts/FolderArchived/FolderInformation/index.tsx +++ b/src/front/Components/Layouts/FolderArchived/FolderInformation/index.tsx @@ -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 { 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 { ); } 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 { 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 { private closeArchivedModal(): void { this.setState({ isArchivedModalOpen: false }); } + + private async getFolder(): Promise { + 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 ; + return ; }