From ff3ae64e96cc38250c8a3c4c9e6afb6a5758fe3b Mon Sep 17 00:00:00 2001 From: Vincent Alamelle Date: Thu, 4 May 2023 12:08:27 +0200 Subject: [PATCH] Add all office users specific user to the folder + remove them --- .../SuperAdmin/Customers/Customers.ts | 3 +- .../LeCoffreApi/SuperAdmin/Folders/Folders.ts | 2 + .../Api/LeCoffreApi/SuperAdmin/Users/Users.ts | 7 +- .../UpdateFolderCollaborators/index.tsx | 108 ++++++++++++++++-- 4 files changed, 106 insertions(+), 14 deletions(-) diff --git a/src/front/Api/LeCoffreApi/SuperAdmin/Customers/Customers.ts b/src/front/Api/LeCoffreApi/SuperAdmin/Customers/Customers.ts index 065b0a47..9d89b518 100644 --- a/src/front/Api/LeCoffreApi/SuperAdmin/Customers/Customers.ts +++ b/src/front/Api/LeCoffreApi/SuperAdmin/Customers/Customers.ts @@ -5,7 +5,8 @@ import BaseSuperAdmin from "../BaseSuperAdmin"; // TODO Type get query params -> Where + inclue + orderby export interface IGetCustomersparams { - q?: {}; + where?:{}, + include?:{}, } // TODO Type getbyuid query params diff --git a/src/front/Api/LeCoffreApi/SuperAdmin/Folders/Folders.ts b/src/front/Api/LeCoffreApi/SuperAdmin/Folders/Folders.ts index 00f987f1..f99993b1 100644 --- a/src/front/Api/LeCoffreApi/SuperAdmin/Folders/Folders.ts +++ b/src/front/Api/LeCoffreApi/SuperAdmin/Folders/Folders.ts @@ -6,6 +6,7 @@ import { EFolderStatus } from "le-coffre-resources/dist/Customer/OfficeFolder"; // TODO Type get query params -> Where + inclue + orderby export interface IGetFoldersParams { q?: { + select?: {}; where?: {}; include?: {}; }; @@ -37,6 +38,7 @@ export type IPutFoldersParams = { description?: OfficeFolder["description"]; archived_description?: OfficeFolder["archived_description"]; status?: OfficeFolder["status"]; + office_folder_has_stakeholder?: OfficeFolder["office_folder_has_stakeholder"]; }; @Service() diff --git a/src/front/Api/LeCoffreApi/SuperAdmin/Users/Users.ts b/src/front/Api/LeCoffreApi/SuperAdmin/Users/Users.ts index 94db6c0b..f08f26f1 100644 --- a/src/front/Api/LeCoffreApi/SuperAdmin/Users/Users.ts +++ b/src/front/Api/LeCoffreApi/SuperAdmin/Users/Users.ts @@ -4,7 +4,9 @@ import BaseSuperAdmin from "../BaseSuperAdmin"; // TODO Type get query params -> Where + inclue + orderby export interface IGetUsersparams { - q?: {}; + where?:{}, + include?:{}, + select?:{}, } // TODO Type getbyuid query params @@ -40,7 +42,8 @@ export default class Users extends BaseSuperAdmin { */ public async get(q: IGetUsersparams): Promise { const url = new URL(this.baseURl); - Object.entries(q).forEach(([key, value]) => url.searchParams.set(key, JSON.stringify(value))); + const query = { q }; + Object.entries(query).forEach(([key, value]) => url.searchParams.set(key, JSON.stringify(value))); try { return await this.getRequest(url); } catch (err) { diff --git a/src/front/Components/Layouts/Folder/UpdateFolderCollaborators/index.tsx b/src/front/Components/Layouts/Folder/UpdateFolderCollaborators/index.tsx index b7d726be..426528aa 100644 --- a/src/front/Components/Layouts/Folder/UpdateFolderCollaborators/index.tsx +++ b/src/front/Components/Layouts/Folder/UpdateFolderCollaborators/index.tsx @@ -3,21 +3,28 @@ import Form from "@Front/Components/DesignSystem/Form"; import Typography, { ITypo } from "@Front/Components/DesignSystem/Typography"; import BackArrow from "@Front/Components/Elements/BackArrow"; import DefaultNotaryDashboard, { IDashBoardFolder } from "@Front/Components/LayoutTemplates/DefaultNotaryDashboard"; - import BasePage from "../../Base"; import classes from "./classes.module.scss"; import Link from "next/link"; -import { useRouter } from "next/router"; +import { NextRouter, useRouter } from "next/router"; import RadioBox from "@Front/Components/DesignSystem/RadioBox"; import MultiSelect from "@Front/Components/DesignSystem/MultiSelect"; import Module from "@Front/Config/Module"; +import Users, { IGetUsersparams } from "@Front/Api/LeCoffreApi/SuperAdmin/Users/Users"; +import Folders from "@Front/Api/LeCoffreApi/SuperAdmin/Folders/Folders"; +import { OfficeFolderHasStakeholder } from "le-coffre-resources/dist/Customer"; +import { IOption } from "@Front/Components/DesignSystem/Select"; +import User from "le-coffre-resources/dist/Notary"; type IPropsClass = { selectedFolderUid: string; + router: NextRouter; }; type IState = { selectedFolder: IDashBoardFolder | null; selectedOption?: ERadioBoxValue; + availableCollaborators: User[]; + selectedCollaborators: readonly IOption[]; }; enum ERadioBoxValue { @@ -30,19 +37,24 @@ class UpdateFolderCollaboratorsClass extends BasePage { super(props); this.state = { selectedFolder: null, + availableCollaborators: [], + selectedCollaborators: [], }; this.onSelectedFolder = this.onSelectedFolder.bind(this); this.onSelectedOptionAllOffice = this.onSelectedOptionAllOffice.bind(this); this.onSelectedOptionSpecific = this.onSelectedOptionSpecific.bind(this); + this.onFormSubmit = this.onFormSubmit.bind(this); + this.onChangeSelectedCollaborators = this.onChangeSelectedCollaborators.bind(this); } - public override render(): JSX.Element { + public override render(): JSX.Element { const foldersInformationPath = Module.getInstance().get().modules.pages.Folder.pages.FolderInformation.props.path; const backwardPath = foldersInformationPath.replace("[folderUid]", this.props.selectedFolderUid); - const selectOptions = [ - { value: "adazzdsqaad", label: "John Doe" }, - { value: "azdzafzad", label: "Jahn Doe" }, - { value: "azdazkdazoaz", label: "Marcelino Doe" }, - ]; + const selectOptions : IOption[]= this.state.availableCollaborators.map((collaborator) => { + return { + label: collaborator.contact.first_name + " " + collaborator.contact.last_name, + value: collaborator.uid, + } + }) return (
@@ -51,7 +63,7 @@ class UpdateFolderCollaboratorsClass extends BasePage {
Modifier les collaborateurs -
+
Tout l'office @@ -63,7 +75,7 @@ class UpdateFolderCollaboratorsClass extends BasePage { {this.state.selectedOption === ERadioBoxValue.SELECTION && (
- +
)} @@ -79,6 +91,59 @@ class UpdateFolderCollaboratorsClass extends BasePage { ); } + public override async componentDidMount(){ + await this.getFolderAvailableCollaborators(this.props.selectedFolderUid); + } + + private async getFolderAvailableCollaborators(folderUid: string){ + const query = { + q: { + office: true, + office_folder_has_stakeholder: { + include: { + user_stakeholder: { + include: { + contact: true, + } + } + } + }, + }, + }; + + let folder = null; + try { + folder = await Folders.getInstance().getByUid(folderUid, query); + const preSelectedCollaborators : IOption[]= folder.office_folder_has_stakeholder!.map((collaborator) => { + return { + label: collaborator.user_stakeholder.contact.first_name + " " + collaborator.user_stakeholder.contact.last_name, + value: collaborator.user_stakeholder.uid, + } + }) + this.setState({selectedCollaborators: preSelectedCollaborators}) + } catch (error) { + this.props.router.push(Module.getInstance().get().modules.pages["404"].props.path); + return; + } + + const userQuery: IGetUsersparams = { + where: { + office_uid: folder.office.uid, + }, + include:{ + contact: { + select:{ + first_name:true, + last_name:true, + } + } + } + }; + + const availableCollaborators = await Users.getInstance().get(userQuery); + this.setState({availableCollaborators}); + } + private onSelectedOptionAllOffice(event: React.ChangeEvent) { if (event.target.value !== ERadioBoxValue.ALL) return; this.setState({ @@ -86,6 +151,10 @@ class UpdateFolderCollaboratorsClass extends BasePage { }); } + private onChangeSelectedCollaborators(selectedCollaborators: readonly IOption[]) { + this.setState({ selectedCollaborators }); + } + private onSelectedOptionSpecific(event: React.ChangeEvent) { if (event.target.value !== ERadioBoxValue.SELECTION) return; this.setState({ @@ -96,11 +165,28 @@ class UpdateFolderCollaboratorsClass extends BasePage { private onSelectedFolder(folder: IDashBoardFolder): void { this.setState({ selectedFolder: folder }); } + + private async onFormSubmit(e: React.FormEvent | null, values: { [key: string]: string }) { + try { + let collaboratorsUid : OfficeFolderHasStakeholder[]; + if(this.state.selectedOption === ERadioBoxValue.SELECTION){ + collaboratorsUid = this.state.selectedCollaborators.map((collaborator) => ({user_stakeholder: {uid: collaborator.value}} as OfficeFolderHasStakeholder)); + } + else{ + collaboratorsUid = this.state.availableCollaborators.map((collaborator) => ({user_stakeholder: {uid: collaborator.uid}} as OfficeFolderHasStakeholder)); + } + await Folders.getInstance().put(this.props.selectedFolderUid, {office_folder_has_stakeholder: collaboratorsUid}); + this.props.router.push(Module.getInstance().get().modules.pages.Folder.pages.FolderInformation.props.path.replace("[folderUid]", this.props.selectedFolderUid)); + + } catch (error) { + console.error(error) + } + } } export default function UpdateFolderCollaborators() { const router = useRouter(); let { folderUid } = router.query; folderUid = folderUid as string; - return ; + return ; }