diff --git a/src/common/Api/LeCoffreApi/sdk/DeedTypeService.ts b/src/common/Api/LeCoffreApi/sdk/DeedTypeService.ts index 6917cc0c..7fecfd5c 100644 --- a/src/common/Api/LeCoffreApi/sdk/DeedTypeService.ts +++ b/src/common/Api/LeCoffreApi/sdk/DeedTypeService.ts @@ -176,18 +176,18 @@ export default class DeedTypeService extends AbstractService { } public static updateDeedType(process: any, newData: any): Promise { - return new Promise((resolve: () => void, reject: (error: string) => void) => { + // Update cache + this.setItem('_deed_types_', process); + + return new Promise((resolve: () => void) => { this.messageBus.updateProcess(process.processId, { updated_at: new Date().toISOString(), ...newData }, [], null).then((processUpdated: any) => { const newStateId: string = processUpdated.diffs[0]?.state_id; this.messageBus.notifyUpdate(process.processId, newStateId).then(() => { - this.messageBus.validateState(process.processId, newStateId).then((_stateValidated) => { - const deedTypeUid: string = process.processData.uid; - this.removeItem('_deed_types_', deedTypeUid); - - this.getDeedTypeByUid(deedTypeUid).then(resolve).catch(reject); - }).catch(reject); - }).catch(reject); - }).catch(reject); + this.messageBus.validateState(process.processId, newStateId).then(() => { + resolve(); + }).catch(() => console.error('Failed to validate state')); + }).catch(() => console.error('Failed to notify update')); + }).catch(() => console.error('Failed to update')); }); } diff --git a/src/common/Api/LeCoffreApi/sdk/RoleService.ts b/src/common/Api/LeCoffreApi/sdk/RoleService.ts index 3604ecf1..0572e393 100644 --- a/src/common/Api/LeCoffreApi/sdk/RoleService.ts +++ b/src/common/Api/LeCoffreApi/sdk/RoleService.ts @@ -4,8 +4,6 @@ import User from 'src/sdk/User'; import AbstractService from './AbstractService'; -import RuleService from './RuleService'; - export default class RoleService extends AbstractService { private constructor() { @@ -95,9 +93,7 @@ export default class RoleService extends AbstractService { if (processes.length === 0) { return items; } else { - for (let process of processes) { - process = await this.completeRole(process); - + for (const process of processes) { // Update cache this.setItem('_roles_', process); @@ -127,8 +123,7 @@ export default class RoleService extends AbstractService { if (processes.length === 0) { resolve(null); } else { - let process: any = processes[0]; - process = await this.completeRole(process); + const process: any = processes[0]; // Update cache this.setItem('_roles_', process); @@ -154,20 +149,4 @@ export default class RoleService extends AbstractService { }).catch(reject); }); } - - private static async completeRole(process: any): Promise { - /* - if (process.processData.rules && process.processData.rules.length > 0) { - process.processData.rules = await new Promise(async (resolve: (rules: any[]) => void) => { - const rules: any[] = []; - for (const rule of process.processData.rules) { - rules.push((await RuleService.getRuleByUid(rule.uid)).processData); - } - resolve(rules); - }); - } - */ - - return process; - } } diff --git a/src/front/Components/Layouts/DeedTypes/DeedTypesEdit/index.tsx b/src/front/Components/Layouts/DeedTypes/DeedTypesEdit/index.tsx index be9d649d..10d85b8f 100644 --- a/src/front/Components/Layouts/DeedTypes/DeedTypesEdit/index.tsx +++ b/src/front/Components/Layouts/DeedTypes/DeedTypesEdit/index.tsx @@ -63,16 +63,25 @@ export default function DeedTypesEdit() { } try { LoaderService.getInstance().show(); - DeedTypeService.getDeedTypeByUid(deedTypeUid as string).then((process: any) => { + DeedTypeService.getDeedTypeByUid(deedTypeUid as string).then(async (process: any) => { if (process) { - DeedTypeService.updateDeedType(process, { name: values["name"], description: values["description"] }).then(() => { - router.push( - Module.getInstance() - .get() - .modules.pages.DeedTypes.pages.DeedTypesInformations.props.path.replace("[uid]", deedTypeUid as string), - ); - LoaderService.getInstance().hide(); - }); + // New data + const newData: any = { + name: values["name"], + description: values["description"] + }; + + // Merge process data with new data & update process + process.processData.name = newData.name; + process.processData.description = newData.description; + await DeedTypeService.updateDeedType(process, newData); + + router.push( + Module.getInstance() + .get() + .modules.pages.DeedTypes.pages.DeedTypesInformations.props.path.replace("[uid]", deedTypeUid as string), + ); + LoaderService.getInstance().hide(); } }); } catch (validationErrors) { diff --git a/src/front/Components/Layouts/DeedTypes/DeedTypesInformations/index.tsx b/src/front/Components/Layouts/DeedTypes/DeedTypesInformations/index.tsx index 8b017bb3..d718768f 100644 --- a/src/front/Components/Layouts/DeedTypes/DeedTypesInformations/index.tsx +++ b/src/front/Components/Layouts/DeedTypes/DeedTypesInformations/index.tsx @@ -52,12 +52,25 @@ export default function DeedTypesInformations(props: IProps) { const deleteDeedType = useCallback(async () => { LoaderService.getInstance().show(); - DeedTypeService.getDeedTypeByUid(deedTypeUid as string).then((process: any) => { + DeedTypeService.getDeedTypeByUid(deedTypeUid as string).then(async (process: any) => { if (process) { - DeedTypeService.updateDeedType(process, { isDeleted: 'true', archived_at: new Date().toISOString() }).then(() => { - router.push(Module.getInstance().get().modules.pages.DeedTypes.props.path); - LoaderService.getInstance().hide(); - }); + // New data + const newData: any = { + isDeleted: 'true', + archived_at: new Date().toISOString() + }; + + // Merge process data with new data & update process + process.processData.isDeleted = newData.isDeleted; + process.processData.archived_at = newData.archived_at; + await DeedTypeService.updateDeedType(process, newData); + + router.push( + Module.getInstance() + .get() + .modules.pages.DeedTypes.props.path + ); + LoaderService.getInstance().hide(); } }); }, [deedTypeUid, router]); @@ -107,7 +120,7 @@ export default function DeedTypesInformations(props: IProps) { const saveDocumentTypes = useCallback(() => { LoaderService.getInstance().show(); - DeedTypeService.getDeedTypeByUid(deedTypeUid as string).then((process: any) => { + DeedTypeService.getDeedTypeByUid(deedTypeUid as string).then(async (process: any) => { if (process) { const deedType: any = process.processData; @@ -118,10 +131,17 @@ export default function DeedTypesInformations(props: IProps) { selectedDocuments.map((selectedDocument: any) => ({ uid: selectedDocument.id as string })) .forEach((selectedDocument: any) => document_types.push(selectedDocument)); - DeedTypeService.updateDeedType(process, { document_types: document_types }).then(() => { - LoaderService.getInstance().hide(); - closeSaveModal(); - }); + // New data + const newData: any = { + document_types: document_types + }; + + // Merge process data with new data & update process + process.processData.document_types = newData.document_types; + await DeedTypeService.updateDeedType(process, newData); + + LoaderService.getInstance().hide(); + closeSaveModal(); } }); }, [closeSaveModal, deedTypeUid, selectedDocuments]); diff --git a/src/front/Components/Layouts/Folder/AskDocuments/ParameterDocuments/index.tsx b/src/front/Components/Layouts/Folder/AskDocuments/ParameterDocuments/index.tsx index 56512953..05d90cae 100644 --- a/src/front/Components/Layouts/Folder/AskDocuments/ParameterDocuments/index.tsx +++ b/src/front/Components/Layouts/Folder/AskDocuments/ParameterDocuments/index.tsx @@ -97,14 +97,21 @@ export default function ParameterDocuments(props: IProps) { const oldDocumentsType = props.folder.deed?.document_types!; await new Promise((resolve: () => void) => { - DeedTypeService.getDeedTypeByUid(props.folder.deed?.deed_type?.uid!).then((process: any) => { + DeedTypeService.getDeedTypeByUid(props.folder.deed?.deed_type?.uid!).then(async (process: any) => { if (process) { - DeedTypeService.updateDeedType(process, { + // New data + const newData: any = { document_types: [ ...oldDocumentsType.map((document: any) => ({ uid: document.uid })), { uid: documentType.uid } ] - }).then(() => resolve()); + }; + + // Merge process data with new data & update process + process.processData.document_types = newData.document_types; + await DeedTypeService.updateDeedType(process, newData); + + resolve(); } }); }); @@ -131,14 +138,21 @@ export default function ParameterDocuments(props: IProps) { const oldDocumentsType = props.folder.deed?.document_types!; await new Promise((resolve: () => void) => { - DeedTypeService.getDeedTypeByUid(props.folder.deed?.deed_type?.uid!).then((process: any) => { + DeedTypeService.getDeedTypeByUid(props.folder.deed?.deed_type?.uid!).then(async (process: any) => { if (process) { - DeedTypeService.updateDeedType(process, { + // New data + const newData: any = { document_types: [ ...oldDocumentsType.map((document: any) => ({ uid: document.uid })), ...selectedDocuments.map((document: any) => ({ uid: document.id as string })) ] - }).then(() => resolve()); + }; + + // Merge process data with new data & update process + process.processData.document_types = newData.document_types; + await DeedTypeService.updateDeedType(process, newData); + + resolve(); } }); });