From 2213168f8e54129d523ec80cf997b1800dcc3ad6 Mon Sep 17 00:00:00 2001 From: Sosthene Date: Wed, 10 Sep 2025 14:57:14 +0200 Subject: [PATCH] Fix DeedType and DocumentType --- .../Api/LeCoffreApi/sdk/DeedTypeService.ts | 12 +- .../LeCoffreApi/sdk/DocumentTypeService.ts | 123 +++++++++++------- .../DefaultDeedTypeDashboard/index.tsx | 4 +- .../DocumentTypesCreate/index.tsx | 54 ++++++-- .../DocumentTypes/DocumentTypesEdit/index.tsx | 9 +- .../DocumentTypesInformations/index.tsx | 32 +++-- 6 files changed, 153 insertions(+), 81 deletions(-) diff --git a/src/common/Api/LeCoffreApi/sdk/DeedTypeService.ts b/src/common/Api/LeCoffreApi/sdk/DeedTypeService.ts index eca15585..9f67c8ac 100644 --- a/src/common/Api/LeCoffreApi/sdk/DeedTypeService.ts +++ b/src/common/Api/LeCoffreApi/sdk/DeedTypeService.ts @@ -99,21 +99,23 @@ export default class DeedTypeService extends AbstractService { }); } - public static async updateDeedType(process: any, newData: any): Promise { + public static async updateDeedType(processId: string, newData: any): Promise { try { const processUpdated = await this.messageBus.updateProcess( - process.processId, + processId, { updated_at: new Date().toISOString(), ...newData }, [], null ); const newStateId: string = processUpdated.diffs[0]?.state_id; - await this.messageBus.notifyUpdate(process.processId, newStateId); - await this.messageBus.validateState(process.processId, newStateId); + await this.messageBus.notifyUpdate(processId, newStateId); + await this.messageBus.validateState(processId, newStateId); + + const processData = await this.messageBus.getProcessData(processId); // Update cache - this.setItem('_deed_types_', process); + this.setItem('_deed_types_', processData); } catch (error) { console.error('Failed to update deed type:', error); throw error; // Re-throw to allow caller to handle diff --git a/src/common/Api/LeCoffreApi/sdk/DocumentTypeService.ts b/src/common/Api/LeCoffreApi/sdk/DocumentTypeService.ts index 2d3de4d2..6bab2586 100644 --- a/src/common/Api/LeCoffreApi/sdk/DocumentTypeService.ts +++ b/src/common/Api/LeCoffreApi/sdk/DocumentTypeService.ts @@ -3,6 +3,7 @@ import { v4 as uuidv4 } from 'uuid'; import User from 'src/sdk/User'; import AbstractService from './AbstractService'; +import { DEFAULT_STORAGE_URLS } from '@Front/Config/AppConstants'; export default class DocumentTypeService extends AbstractService { @@ -10,7 +11,7 @@ export default class DocumentTypeService extends AbstractService { super(); } - public static createDocumentType(documentTypeData: any, validatorId: string): Promise { + public static createDocumentType(documentTypeData: any, validatorId: string): Promise<{ processId: string, processData: any }> { const ownerId: string = User.getInstance().getPairingId()!; const processData: any = { @@ -23,45 +24,27 @@ export default class DocumentTypeService extends AbstractService { }; const privateFields: string[] = Object.keys(processData); - privateFields.splice(privateFields.indexOf('uid'), 1); - privateFields.splice(privateFields.indexOf('utype'), 1); - privateFields.splice(privateFields.indexOf('isDeleted'), 1); + const allFields: string[] = [...privateFields, 'roles']; const roles: any = { demiurge: { - members: [...[ownerId], validatorId], + members: [ownerId], validation_rules: [], storages: [] }, owner: { - members: [ownerId], + members: [ownerId, validatorId], validation_rules: [ { - quorum: 0.5, - fields: [...privateFields, 'roles', 'uid', 'utype'], - min_sig_member: 1, + quorum: 0.01, + fields: allFields, + min_sig_member: 0.01, }, ], - storages: [] - }, - validator: { - members: [validatorId], - validation_rules: [ - { - quorum: 0.5, - fields: ['idCertified', 'roles'], - min_sig_member: 1, - }, - { - quorum: 0.0, - fields: [...privateFields], - min_sig_member: 0, - }, - ], - storages: [] + storages: [...DEFAULT_STORAGE_URLS] }, apophis: { - members: [ownerId], + members: [ownerId, validatorId], validation_rules: [], storages: [] } @@ -71,39 +54,80 @@ export default class DocumentTypeService extends AbstractService { this.messageBus.createProcess(processData, privateFields, roles).then((processCreated: any) => { this.messageBus.notifyUpdate(processCreated.processId, processCreated.process.states[0].state_id).then(() => { this.messageBus.validateState(processCreated.processId, processCreated.process.states[0].state_id).then((_stateValidated: any) => { - this.getDocumentTypeByUid(processCreated.processData.uid).then(resolve).catch(reject); + this.messageBus.getProcessData(processCreated.processId).then((processData: any) => { + resolve({ processId: processCreated.processId, processData: processData }); + }).catch(reject); }).catch(reject); }).catch(reject); }).catch(reject); }); } - public static getDocumentTypes(): Promise { + public static getDocumentTypes(callback: (processes: any[]) => void): void { // Check if we have valid cache const items: any[] = this.getItems('_document_types_'); - - return this.messageBus.getProcessesDecoded((publicValues: any) => - publicValues['uid'] && - publicValues['utype'] && - publicValues['utype'] === 'documentType' && - publicValues['isDeleted'] && - publicValues['isDeleted'] === 'false' && - !items.map((item: any) => item.processData.uid).includes(publicValues['uid']) - ).then(async (processes: any[]) => { + if (items.length > 0) { + setTimeout(() => callback([...items]), 0); + } + + this.messageBus.getProcessesDecoded((values: any) => { + return values['utype'] === 'documentType' + && values['isDeleted'] === 'false'; + }).then(async (processes: any) => { if (processes.length === 0) { - return items; - } else { - for (const process of processes) { - // Update cache - this.setItem('_document_types_', process); - - items.push(process); - } - return items; + callback([...items]); + return; } + + const updatedItems: any[] = [...items]; + + for (let processIndex = 0; processIndex < processes.length; processIndex++) { + const process = processes[processIndex]; + + // Update cache + this.setItem('_document_types_', process); + + const existingIndex: number = updatedItems.findIndex(item => item.processId === process.processId); + if (existingIndex >= 0) { + updatedItems[existingIndex] = process; + } else { + updatedItems.push(process); + } + } + + callback([...updatedItems]); }); } + public static getDocumentTypeByProcessId(processId: string): Promise { + // Check if we have valid cache + const item: any = this.getItem('_document_types_', processId); + if (item) { + return Promise.resolve(item); + } + + return new Promise((resolve: (process: any) => void, reject: (error: string) => void) => { + this.messageBus.getProcessesDecoded((publicValues: any) => { + return publicValues['utype'] === 'documentType' && publicValues['isDeleted'] === 'false'; + }).then((processes: any[]) => { + // Find the process with matching processId + const process = processes.find((p: any) => p.processId === processId); + + if (!process) { + resolve(null); + return; + } + + // Update cache + this.setItem('_document_types_', process); + + resolve(process); + }).catch(reject); + }); + } + + // Keep the old method for backward compatibility but mark as deprecated + /** @deprecated Use getDocumentTypeByProcessId instead */ public static getDocumentTypeByUid(uid: string): Promise { // Check if we have valid cache const item: any = this.getItem('_document_types_', uid); @@ -133,10 +157,9 @@ export default class DocumentTypeService extends AbstractService { const newStateId: string = processUpdated.diffs[0]?.state_id; this.messageBus.notifyUpdate(process.processId, newStateId).then(() => { this.messageBus.validateState(process.processId, newStateId).then((_stateValidated) => { - const documentTypeUid: string = process.processData.uid; - this.removeItem('_document_types_', documentTypeUid); + this.removeItem('_document_types_', process.processId); - this.getDocumentTypeByUid(documentTypeUid).then(resolve).catch(reject); + this.getDocumentTypeByProcessId(process.processId).then(resolve).catch(reject); }).catch(reject); }).catch(reject); }).catch(reject); diff --git a/src/front/Components/LayoutTemplates/DefaultDeedTypeDashboard/index.tsx b/src/front/Components/LayoutTemplates/DefaultDeedTypeDashboard/index.tsx index 018c4f63..f8440245 100644 --- a/src/front/Components/LayoutTemplates/DefaultDeedTypeDashboard/index.tsx +++ b/src/front/Components/LayoutTemplates/DefaultDeedTypeDashboard/index.tsx @@ -28,6 +28,8 @@ export default function DefaultDeedTypeDashboard(props: IProps) { deedTypes.sort((a: any, b: any) => a.name.localeCompare(b.name)); setDeedTypes(deedTypes); + } else { + console.log('[DefaultDeedTypeDashboard] No deed types found'); } }); }, []); @@ -51,7 +53,7 @@ export default function DefaultDeedTypeDashboard(props: IProps) { } bottomButton={{ link: Module.getInstance().get().modules.pages.DeedTypes.pages.Create.props.path, - text: "Créer une liste de pièces", + text: "Créer une liste de pièces", // TODO I think this is misleading, should be "Créer un type d'acte" }} /> ); diff --git a/src/front/Components/Layouts/DocumentTypes/DocumentTypesCreate/index.tsx b/src/front/Components/Layouts/DocumentTypes/DocumentTypesCreate/index.tsx index 6631e806..81b65a47 100644 --- a/src/front/Components/Layouts/DocumentTypes/DocumentTypesCreate/index.tsx +++ b/src/front/Components/Layouts/DocumentTypes/DocumentTypesCreate/index.tsx @@ -24,44 +24,74 @@ export default function DocumentTypesCreate(props: IProps) { const [validationError, setValidationError] = useState([]); const router = useRouter(); + + const handleCancel = useCallback(() => { + router.push(Module.getInstance().get().modules.pages.DocumentTypes.props.path); + }, [router]); + const onSubmitHandler = useCallback( async (e: React.FormEvent | null, values: { [key: string]: string }) => { try { const user: any = UserStore.instance.getUser(); - const officeId: string = user.office.uid; + if (!user) { + console.error("DocumentTypesCreate: User not found - user is null or undefined"); + return; + } + const office = UserStore.instance.getOffice(); + if (!office) { + console.error("DocumentTypesCreate: office not found - office is undefined or null"); + return; + } + const officeId = office.processId; + const officeIdNot = office.processData.idNot; - const documentFormModel = DocumentType.hydrate({ - ...values, - office: Office.hydrate({ - uid: officeId, - }) - }); - await validateOrReject(documentFormModel, { groups: ["createDocumentType"] }); + // const documentFormModel = DocumentType.hydrate({ + // ...values, + // office: Office.hydrate({ + // uid: officeId, + // }) + // }); + // await validateOrReject(documentFormModel, { groups: ["createDocumentType"] }); const documentTypeData: any = { ...values, office: { uid: officeId, + idNot: officeIdNot, } }; LoaderService.getInstance().show(); - DocumentTypeService.createDocumentType(documentTypeData, DEFAULT_VALIDATOR_ID).then((processCreated: any) => { + try { + const processCreated = await DocumentTypeService.createDocumentType(documentTypeData, DEFAULT_VALIDATOR_ID); ToasterService.getInstance().success({ title: "Succès !", description: "Type de document créé avec succès" }); + const documentTypeUid = processCreated.processId.split(':')[0]; + if (!documentTypeUid) { + console.error("DocumentTypesCreate: documentTypeUid is undefined - processCreated.processId is missing"); + return; + } router.push( Module.getInstance() .get() - .modules.pages.DocumentTypes.pages.DocumentTypesInformations.props.path.replace("[uid]", processCreated.processData.uid), + .modules.pages.DocumentTypes.pages.DocumentTypesInformations.props.path.replace("[uid]", documentTypeUid), ); + } catch (apiError) { + ToasterService.getInstance().error({ + title: "Erreur !", + description: "Une erreur est survenue lors de la création du type de document" + }); + console.error("Document type creation error:", apiError); + } finally { LoaderService.getInstance().hide(); - }); + } } catch (e) { if (e instanceof Array) { setValidationError(e); } + LoaderService.getInstance().hide(); } }, [router], @@ -90,7 +120,7 @@ export default function DocumentTypesCreate(props: IProps) { validationError={validationError.find((error) => error.property === "public_description")} />
- diff --git a/src/front/Components/Layouts/DocumentTypes/DocumentTypesEdit/index.tsx b/src/front/Components/Layouts/DocumentTypes/DocumentTypesEdit/index.tsx index bf17ef91..8eac9e3b 100644 --- a/src/front/Components/Layouts/DocumentTypes/DocumentTypesEdit/index.tsx +++ b/src/front/Components/Layouts/DocumentTypes/DocumentTypesEdit/index.tsx @@ -26,9 +26,12 @@ export default function DocumentTypesEdit() { async function getDocumentType() { if (!documentTypeUid) return; LoaderService.getInstance().show(); - DocumentTypeService.getDocumentTypeByUid(documentTypeUid as string).then((process: any) => { + DocumentTypeService.getDocumentTypeByProcessId(documentTypeUid as string).then((process: any) => { if (process) { - const documentType: any = process.processData; + const documentType: any = { + ...process.processData, + processId: process.processId + }; setDocumentTypeSelected(documentType); } LoaderService.getInstance().hide(); @@ -53,7 +56,7 @@ export default function DocumentTypesEdit() { return; } LoaderService.getInstance().show(); - DocumentTypeService.getDocumentTypeByUid(documentTypeUid as string).then((process: any) => { + DocumentTypeService.getDocumentTypeByProcessId(documentTypeUid as string).then((process: any) => { if (process) { DocumentTypeService.updateDocumentType(process, values).then(() => { router.push( diff --git a/src/front/Components/Layouts/DocumentTypes/DocumentTypesInformations/index.tsx b/src/front/Components/Layouts/DocumentTypes/DocumentTypesInformations/index.tsx index 892ee1c0..b5669140 100644 --- a/src/front/Components/Layouts/DocumentTypes/DocumentTypesInformations/index.tsx +++ b/src/front/Components/Layouts/DocumentTypes/DocumentTypesInformations/index.tsx @@ -22,13 +22,23 @@ export default function DocumentTypesInformations() { useEffect(() => { async function getDocument() { - if (!documentTypeUid) return; + if (!documentTypeUid) { + console.log('DocumentTypesInformations: documentTypeUid is not available yet'); + return; + } - DocumentTypeService.getDocumentTypeByUid(documentTypeUid as string).then((process: any) => { + DocumentTypeService.getDocumentTypeByProcessId(documentTypeUid as string).then((process: any) => { if (process) { - const document: any = process.processData; + const document: any = { + ...process.processData, + processId: process.processId + }; setDocumentSelected(document); + } else { + console.log('DocumentTypesInformations: No process found for processId:', documentTypeUid); } + }).catch((error) => { + console.error('DocumentTypesInformations: Error fetching document:', error); }); } @@ -69,13 +79,15 @@ export default function DocumentTypesInformations() {
- - edit informations - + {(documentSelected as any)?.processId && ( + + edit informations + + )}