diff --git a/src/common/Api/LeCoffreApi/sdk/CustomerService.ts b/src/common/Api/LeCoffreApi/sdk/CustomerService.ts index 006ed49b..dd427f48 100644 --- a/src/common/Api/LeCoffreApi/sdk/CustomerService.ts +++ b/src/common/Api/LeCoffreApi/sdk/CustomerService.ts @@ -83,11 +83,12 @@ export default class CustomerService { public static getCustomerByUid(uid: string): Promise { return new Promise((resolve: (process: any) => void, reject: (error: string) => void) => { - this.messageBus.getProcessesDecoded((publicValues: any) => publicValues['uid'] && publicValues['uid'] === uid && publicValues['utype'] && publicValues['utype'] === 'customer').then((profiles: any[]) => { - if (profiles.length === 0) { + this.messageBus.getProcessesDecoded((publicValues: any) => publicValues['uid'] && publicValues['uid'] === uid && publicValues['utype'] && publicValues['utype'] === 'customer').then((processes: any[]) => { + if (processes.length === 0) { resolve(null); } else { - resolve(profiles[0]); + const process: any = processes[0]; + resolve(process); } }).catch(reject); }); diff --git a/src/common/Api/LeCoffreApi/sdk/DocumentService.ts b/src/common/Api/LeCoffreApi/sdk/DocumentService.ts index 35e47aa4..13a195cc 100644 --- a/src/common/Api/LeCoffreApi/sdk/DocumentService.ts +++ b/src/common/Api/LeCoffreApi/sdk/DocumentService.ts @@ -87,7 +87,8 @@ export default class DocumentService { if (processes.length === 0) { resolve(null); } else { - resolve(processes[0]); + const process: any = processes[0]; + resolve(process); } }).catch(reject); }); diff --git a/src/common/Api/LeCoffreApi/sdk/DocumentTypeService.ts b/src/common/Api/LeCoffreApi/sdk/DocumentTypeService.ts index bab7577a..7c167cc4 100644 --- a/src/common/Api/LeCoffreApi/sdk/DocumentTypeService.ts +++ b/src/common/Api/LeCoffreApi/sdk/DocumentTypeService.ts @@ -87,7 +87,8 @@ export default class DocumentTypeService { if (processes.length === 0) { resolve(null); } else { - resolve(processes[0]); + const process: any = processes[0]; + resolve(process); } }).catch(reject); }); diff --git a/src/common/Api/LeCoffreApi/sdk/FileService.ts b/src/common/Api/LeCoffreApi/sdk/FileService.ts new file mode 100644 index 00000000..cd9cdcd0 --- /dev/null +++ b/src/common/Api/LeCoffreApi/sdk/FileService.ts @@ -0,0 +1,109 @@ + +import { v4 as uuidv4 } from 'uuid'; + +import MessageBus from 'src/sdk/MessageBus'; +import User from 'src/sdk/User'; + +export default class FileService { + + private static readonly messageBus: MessageBus = MessageBus.getInstance(); + + private constructor() { } + + public static createFile(fileData: any, validatorId: string): Promise { + const ownerId = User.getInstance().getPairingId()!; + + const processData: any = { + uid: uuidv4(), + utype: 'file', + isDeleted: 'false', + created_at: new Date().toISOString(), + updated_at: new Date().toISOString(), + ...fileData, + }; + + const privateFields: string[] = Object.keys(processData); + privateFields.splice(privateFields.indexOf('uid'), 1); + privateFields.splice(privateFields.indexOf('utype'), 1); + + const roles: any = { + demiurge: { + members: [...[ownerId], validatorId], + validation_rules: [], + storages: [] + }, + owner: { + members: [ownerId], + validation_rules: [ + { + quorum: 0.5, + fields: [...privateFields, 'roles', 'uid', 'utype'], + min_sig_member: 1, + }, + ], + 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: [] + }, + apophis: { + members: [ownerId], + validation_rules: [], + storages: [] + } + }; + + return new Promise((resolve: (processCreated: any) => void, reject: (error: string) => void) => { + 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) => { + resolve(processCreated); + }).catch(reject); + }).catch(reject); + }).catch(reject); + }); + } + + public static getFiles(): Promise { + return this.messageBus.getProcessesDecoded((publicValues: any) => publicValues['uid'] && publicValues['utype'] && publicValues['utype'] === 'file'); + } + + public static getFileByUid(uid: string): Promise { + return new Promise((resolve: (process: any) => void, reject: (error: string) => void) => { + this.messageBus.getProcessesDecoded((publicValues: any) => publicValues['uid'] && publicValues['uid'] === uid && publicValues['utype'] && publicValues['utype'] === 'file').then(async (processes: any[]) => { + if (processes.length === 0) { + resolve(null); + } else { + const process: any = processes[0]; + resolve(process); + } + }).catch(reject); + }); + } + + public static updateFile(process: any, newData: any): Promise { + return new Promise((resolve: () => void, reject: (error: string) => void) => { + this.messageBus.updateProcess(process.processId, 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) => { + resolve(); + }).catch(reject); + }).catch(reject); + }).catch(reject); + }); + } +} diff --git a/src/front/Components/Layouts/ClientDashboard/DepositDocumentComponent/index.tsx b/src/front/Components/Layouts/ClientDashboard/DepositDocumentComponent/index.tsx index 3856673e..2a71600c 100644 --- a/src/front/Components/Layouts/ClientDashboard/DepositDocumentComponent/index.tsx +++ b/src/front/Components/Layouts/ClientDashboard/DepositDocumentComponent/index.tsx @@ -8,6 +8,8 @@ import Files from "@Front/Api/LeCoffreApi/Customer/Files/Files"; import { ToasterService } from "@Front/Components/DesignSystem/Toaster"; import Confirm from "@Front/Components/DesignSystem/OldModal/Confirm"; +import FileService from "src/common/Api/LeCoffreApi/sdk/FileService"; + type IProps = { document: Document; onChange: () => void; @@ -29,6 +31,7 @@ export default function DepositDocumentComponent(props: IProps) { const addFile = useCallback( (file: File) => { + /* TODO: review const formData = new FormData(); const safeFileName = file.name.normalize("NFD").replace(/[\u0300-\u036f]/g, ""); formData.append("file", file, safeFileName); @@ -39,17 +42,64 @@ export default function DepositDocumentComponent(props: IProps) { .then(onChange) .then(() => ToasterService.getInstance().success({ title: "Succès !", description: "Fichier uploadé avec succès!" })) .catch((error) => ToasterService.getInstance().error({ title: "Erreur !", description: error.message })); + */ + + return new Promise( + (resolve: () => void) => { + const reader = new FileReader(); + reader.onload = (event) => { + if (event.target?.result) { + const arrayBuffer = event.target.result as ArrayBuffer; + const uint8Array = new Uint8Array(arrayBuffer); + + const fileBlob = { + type: file.type, + data: uint8Array + }; + + const fileData = { + document: { + uid: document.uid + }, + file: fileBlob, + file_name: file.name, + mimetype: file.type + } + const validatorId: string = '884cb36a346a79af8697559f16940141f068bdf1656f88fa0df0e9ecd7311fb8:0'; + + FileService.createFile(fileData, validatorId).then(() => resolve()); + } + }; + reader.readAsArrayBuffer(file); + }) + .then(onChange) + .then(() => ToasterService.getInstance().success({ title: "Succès !", description: "Fichier uploadé avec succès!" })) + .catch((error) => ToasterService.getInstance().error({ title: "Erreur !", description: error.message })); }, [document.uid, onChange], ); const deleteFile = useCallback( (filedUid: string) => { + return new Promise( + (resolve: () => void) => { + FileService.getFileByUid(filedUid).then((process: any) => { + if (process) { + FileService.updateFile(process, { isDeleted: 'true' }).then(() => resolve()); + } + }); + }) + .then(onChange) + .then(() => ToasterService.getInstance().success({ title: "Succès !", description: "Fichier supprimé avec succès!" })) + .catch((error) => ToasterService.getInstance().error({ title: "Erreur !", description: error.message })); + + /* TODO: review return Files.getInstance() .delete(filedUid) .then(onChange) .then(() => ToasterService.getInstance().success({ title: "Succès !", description: "Fichier supprimé avec succès!" })) .catch((error) => ToasterService.getInstance().error({ title: "Erreur !", description: error.message })); + */ }, [onChange], ); diff --git a/src/front/Components/Layouts/ClientDashboard/index.tsx b/src/front/Components/Layouts/ClientDashboard/index.tsx index 0fd3be33..bdadb662 100644 --- a/src/front/Components/Layouts/ClientDashboard/index.tsx +++ b/src/front/Components/Layouts/ClientDashboard/index.tsx @@ -25,14 +25,13 @@ import DocumentsNotary from "@Front/Api/LeCoffreApi/Customer/DocumentsNotary/Doc import { EDocumentNotaryStatus } from "le-coffre-resources/dist/Notary/DocumentNotary"; import DepositOtherDocument from "@Front/Components/DesignSystem/DepositOtherDocument"; -import { v4 as uuidv4 } from "uuid"; - import AuthModal from "src/sdk/AuthModal"; //import FormModal from "./FormModal"; import FolderService from "src/common/Api/LeCoffreApi/sdk/FolderService"; import DocumentService from "src/common/Api/LeCoffreApi/sdk/DocumentService"; import DocumentTypeService from "src/common/Api/LeCoffreApi/sdk/DocumentTypeService"; +import FileService from "src/common/Api/LeCoffreApi/sdk/FileService"; type IProps = {}; @@ -61,9 +60,7 @@ export default function ClientDashboard(props: IProps) { const customers: any[] = folder.customers; const customer: any = customers.find((customer: any) => customer.uid === profileUid as string); if (customer) { - - - resolve({ folder: folder, customer, file: null }); + resolve({ folder: folder, customer }); } } }); @@ -177,9 +174,17 @@ export default function ClientDashboard(props: IProps) { if (processes.length > 0) { const documents: any[] = processes.map((process: any) => process.processData); + const files: any[] = (await FileService.getFiles()) + .map((p: any) => p.processData) + .filter((file: any) => file.isDeleted === 'false'); + for (const document of documents) { const p: any = await DocumentTypeService.getDocumentTypeByUid(document.document_type.uid); document.document_type = p.processData; + + if (files.length > 0) { + document.files = files.filter((file: any) => file.document.uid === document.uid); + } } setDocuments(documents);