From 6ed668282401e7e63db6682d57fe3b108da821ce Mon Sep 17 00:00:00 2001 From: Anthony Janin Date: Fri, 27 Jun 2025 13:19:05 +0200 Subject: [PATCH] Fix some bugs - continue --- src/common/Api/LeCoffreApi/sdk/FileService.ts | 15 +---- .../DepositDocumentComponent/index.tsx | 66 +++++++++---------- .../ViewDocumentsNotary/index.tsx | 12 ++-- .../Layouts/ClientDashboard/index.tsx | 15 +++-- .../DocumentTables/FilePreviewModal/index.tsx | 5 +- .../ClientView/DocumentTables/index.tsx | 24 +++---- .../Folder/FolderInformation/index.tsx | 37 +++++++---- .../Layouts/Folder/ViewDocuments/index.tsx | 47 ++++--------- src/sdk/MessageBus.ts | 7 +- 9 files changed, 104 insertions(+), 124 deletions(-) diff --git a/src/common/Api/LeCoffreApi/sdk/FileService.ts b/src/common/Api/LeCoffreApi/sdk/FileService.ts index 317de209..e013ae20 100644 --- a/src/common/Api/LeCoffreApi/sdk/FileService.ts +++ b/src/common/Api/LeCoffreApi/sdk/FileService.ts @@ -78,21 +78,8 @@ export default class FileService { }); } - public static getFiles(): Promise { - return this.messageBus.getProcessesDecoded((publicValues: any) => publicValues['uid'] && publicValues['utype'] && publicValues['utype'] === 'file' && publicValues['isDeleted'] && publicValues['isDeleted'] === 'false'); - } - 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' && publicValues['isDeleted'] && publicValues['isDeleted'] === 'false').then(async (processes: any[]) => { - if (processes.length === 0) { - resolve(null); - } else { - const process: any = processes[0]; - resolve(process); - } - }).catch(reject); - }); + return this.messageBus.getFileByUid(uid); } public static updateFile(process: any, newData: any): Promise { diff --git a/src/front/Components/Layouts/ClientDashboard/DepositDocumentComponent/index.tsx b/src/front/Components/Layouts/ClientDashboard/DepositDocumentComponent/index.tsx index 3c2d6118..a967c7c3 100644 --- a/src/front/Components/Layouts/ClientDashboard/DepositDocumentComponent/index.tsx +++ b/src/front/Components/Layouts/ClientDashboard/DepositDocumentComponent/index.tsx @@ -1,6 +1,5 @@ import DragAndDrop, { IDocumentFileWithUid } from "@Front/Components/DesignSystem/DragAndDrop"; import Typography, { ETypo, ETypoColor } from "@Front/Components/DesignSystem/Typography"; -import { Document } from "le-coffre-resources/dist/Customer"; import { useCallback, useMemo, useState } from "react"; import classes from "./classes.module.scss"; @@ -12,7 +11,7 @@ import FileService from "src/common/Api/LeCoffreApi/sdk/FileService"; import DocumentService from "src/common/Api/LeCoffreApi/sdk/DocumentService"; type IProps = { - document: Document; + document: any; onChange: () => void; }; @@ -22,29 +21,16 @@ export default function DepositDocumentComponent(props: IProps) { const [refused_reason, setRefusedReason] = useState(null); const defaultFiles: IDocumentFileWithUid[] = useMemo(() => { - const filesNotArchived = document.files?.filter((file) => !file.archived_at) ?? []; - return filesNotArchived.map((file) => ({ + const filesNotArchived = document.files?.filter((file: any) => !file.archived_at) ?? []; + return filesNotArchived.map((file: any) => ({ id: file.uid!, - file: new File([""], file.file_name!, { type: file.mimetype }), + file: new File([""], file.file_name!, { type: file.file_blob.type }), uid: file.uid!, })); }, [document.files]); 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); - const query = JSON.stringify({ document: { uid: document.uid } }); - formData.append("q", query); - return Files.getInstance() - .post(formData) - .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(); @@ -53,25 +39,31 @@ export default function DepositDocumentComponent(props: IProps) { const arrayBuffer = event.target.result as ArrayBuffer; const uint8Array = new Uint8Array(arrayBuffer); - const fileBlob = { + const fileBlob: any = { type: file.type, data: uint8Array }; - const fileData = { - document: { - uid: document.uid! - }, - fileBlob: fileBlob, - file_name: file.name, - mimetype: file.type - } + const fileData: any = { + file_blob: fileBlob, + file_name: file.name + }; const validatorId: string = '884cb36a346a79af8697559f16940141f068bdf1656f88fa0df0e9ecd7311fb8:0'; - FileService.createFile(fileData, validatorId).then(() => { + FileService.createFile(fileData, validatorId).then((processCreated: any) => { + const fileUid: string = processCreated.processData.uid; + DocumentService.getDocumentByUid(document.uid!).then((process: any) => { if (process) { - DocumentService.updateDocument(process, { document_status: EDocumentStatus.DEPOSITED }).then(() => resolve()); + const document: any = process.processData; + + let files: any[] = document.files; + if (!files) { + files = []; + } + files.push({ uid: fileUid }); + + DocumentService.updateDocument(process, { files: files, document_status: EDocumentStatus.DEPOSITED }).then(() => resolve()); } }); }); @@ -87,15 +79,23 @@ export default function DepositDocumentComponent(props: IProps) { ); const deleteFile = useCallback( - (filedUid: string) => { + (fileUid: string) => { return new Promise( (resolve: () => void) => { - FileService.getFileByUid(filedUid).then((process: any) => { + FileService.getFileByUid(fileUid).then((process: any) => { if (process) { FileService.updateFile(process, { isDeleted: 'true' }).then(() => { DocumentService.getDocumentByUid(document.uid!).then((process: any) => { if (process) { - DocumentService.updateDocument(process, { document_status: EDocumentStatus.ASKED }).then(() => resolve()); + const document: any = process.processData; + + let files: any[] = document.files; + if (!files) { + files = []; + } + files = files.filter((file: any) => file.uid !== fileUid); + + DocumentService.updateDocument(process, { files: files, document_status: EDocumentStatus.ASKED }).then(() => resolve()); } }); }); @@ -110,7 +110,7 @@ export default function DepositDocumentComponent(props: IProps) { ); const onOpenModal = useCallback(async () => { - const refused_reason = document.document_history?.find((history) => history.document_status === "REFUSED")?.refused_reason; + const refused_reason = document.document_history?.find((history: any) => history.document_status === "REFUSED")?.refused_reason; if (!refused_reason) return; setRefusedReason(refused_reason); setIsModalOpen(true); diff --git a/src/front/Components/Layouts/ClientDashboard/ViewDocumentsNotary/index.tsx b/src/front/Components/Layouts/ClientDashboard/ViewDocumentsNotary/index.tsx index 937d5db0..843ded9f 100644 --- a/src/front/Components/Layouts/ClientDashboard/ViewDocumentsNotary/index.tsx +++ b/src/front/Components/Layouts/ClientDashboard/ViewDocumentsNotary/index.tsx @@ -3,7 +3,7 @@ import RightArrowIcon from "@Assets/Icons/right-arrow.svg"; import Button from "@Front/Components/DesignSystem/Button"; import FilePreview from "@Front/Components/DesignSystem/FilePreview"; import Typography, { ETypo, ETypoColor } from "@Front/Components/DesignSystem/Typography"; -import { DocumentNotary, File } from "le-coffre-resources/dist/Notary"; +import { DocumentNotary } from "le-coffre-resources/dist/Notary"; import Image from "next/image"; import { NextRouter, useRouter } from "next/router"; import React from "react"; @@ -25,7 +25,7 @@ type IState = { isValidateModalVisible: boolean; refuseText: string; selectedFileIndex: number; - selectedFile: File | null; + selectedFile: any; documentNotary: DocumentNotary | null; fileBlob: Blob | null; isLoading: boolean; @@ -71,10 +71,10 @@ class ViewDocumentsNotaryClass extends BasePage { )}
- {this.state.selectedFile.mimetype === "application/pdf" || - this.state.selectedFile.mimetype === "image/jpeg" || - this.state.selectedFile.mimetype === "image/png" || - this.state.selectedFile.mimetype === "image/jpg" ? ( + {this.state.selectedFile.file_blob.type === "application/pdf" || + this.state.selectedFile.file_blob.type === "image/jpeg" || + this.state.selectedFile.file_blob.type === "image/png" || + this.state.selectedFile.file_blob.type === "image/jpg" ? ( { - /* + /* TODO: review const query: IGetDocumentsparams = { where: { depositor: { uid: customerUid }, folder_uid: folderUid as string }, include: { @@ -136,8 +136,6 @@ export default function ClientDashboard(props: IProps) { .then((documents) => setDocuments(documents)); */ - const files: any[] = (await FileService.getFiles()).map((p: any) => p.processData); - return DocumentService.getDocuments().then(async (processes: any[]) => { if (processes.length > 0) { let documents: any[] = processes.map((process: any) => process.processData); @@ -146,11 +144,14 @@ export default function ClientDashboard(props: IProps) { documents = documents.filter((document: any) => document.folder.uid === folderUid); for (const document of documents) { - const p: any = await DocumentTypeService.getDocumentTypeByUid(document.document_type.uid); - document.document_type = p.processData; + document.document_type = (await DocumentTypeService.getDocumentTypeByUid(document.document_type.uid)).processData; - if (files.length > 0) { - document.files = files.filter((file: any) => file.document.uid === document.uid); + if (document.files && document.files.length > 0) { + const files: any[] = []; + for (const file of document.files) { + files.push((await FileService.getFileByUid(file.uid)).processData); + } + document.files = files; } } diff --git a/src/front/Components/Layouts/Folder/FolderInformation/ClientView/DocumentTables/FilePreviewModal/index.tsx b/src/front/Components/Layouts/Folder/FolderInformation/ClientView/DocumentTables/FilePreviewModal/index.tsx index 6ef96822..65da2374 100644 --- a/src/front/Components/Layouts/Folder/FolderInformation/ClientView/DocumentTables/FilePreviewModal/index.tsx +++ b/src/front/Components/Layouts/Folder/FolderInformation/ClientView/DocumentTables/FilePreviewModal/index.tsx @@ -1,9 +1,8 @@ import Modal from "@Front/Components/DesignSystem/Modal"; -import { File } from "le-coffre-resources/dist/Customer"; import React from "react"; type IProps = { - file: File; + file: any; url: string; isOpen: boolean; onClose?: () => void; @@ -14,7 +13,7 @@ export default function FilePreviewModal(props: IProps) { return ( - + ); } diff --git a/src/front/Components/Layouts/Folder/FolderInformation/ClientView/DocumentTables/index.tsx b/src/front/Components/Layouts/Folder/FolderInformation/ClientView/DocumentTables/index.tsx index 421d3cfe..f38d5e35 100644 --- a/src/front/Components/Layouts/Folder/FolderInformation/ClientView/DocumentTables/index.tsx +++ b/src/front/Components/Layouts/Folder/FolderInformation/ClientView/DocumentTables/index.tsx @@ -1,5 +1,4 @@ import DocumentsNotary from "@Front/Api/LeCoffreApi/Notary/DocumentsNotary/DocumentsNotary"; -import Files from "@Front/Api/LeCoffreApi/Notary/Files/Files"; import FilesNotary from "@Front/Api/LeCoffreApi/Notary/FilesNotary/Files"; import CircleProgress from "@Front/Components/DesignSystem/CircleProgress"; import IconButton from "@Front/Components/DesignSystem/IconButton"; @@ -24,8 +23,7 @@ import DeleteSentDocumentModal from "./DeleteSentDocumentModal"; import DocumentService from "src/common/Api/LeCoffreApi/sdk/DocumentService"; import DocumentTypeService from "src/common/Api/LeCoffreApi/sdk/DocumentTypeService"; - -import MessageBus from "src/sdk/MessageBus"; +import FileService from "src/common/Api/LeCoffreApi/sdk/FileService"; type IProps = { customerUid: string; @@ -66,17 +64,15 @@ export default function DocumentTables(props: IProps) { // FilterBy folder.uid & depositor.uid documents = documents.filter((document: any) => document.folder.uid === folderUid && document.depositor.uid === customerUid); - const ps: any[] = await MessageBus.getInstance().getFiles(); - for (const document of documents) { - const documentTypeUid: string = document.document_type.uid; + document.document_type = (await DocumentTypeService.getDocumentTypeByUid(document.document_type.uid)).processData; - const p1: any = await DocumentTypeService.getDocumentTypeByUid(documentTypeUid); - document.document_type = p1.processData; - - const p2: any = ps.find((p2: any) => p2.processData.document.get('uid') === document.uid); - if (p2) { - document.files = [p2.processData]; + if (document.files && document.files.length > 0) { + const files: any[] = []; + for (const file of document.files) { + files.push((await FileService.getFileByUid(file.uid)).processData); + } + document.files = files; } } @@ -122,12 +118,12 @@ export default function DocumentTables(props: IProps) { [deleteSentDocumentModal], ); - const onDownload = useCallback((doc: any /* Document */) => { + const onDownload = useCallback((doc: any) => { const file = doc.files?.[0]; if (!file) return; return new Promise((resolve: () => void) => { - const blob = new Blob([file.fileBlob.data], { type: file.fileBlob.type }); + const blob = new Blob([file.file_blob.data], { type: file.file_blob.type }); const url = URL.createObjectURL(blob); const a = document.createElement('a'); a.href = url; diff --git a/src/front/Components/Layouts/Folder/FolderInformation/index.tsx b/src/front/Components/Layouts/Folder/FolderInformation/index.tsx index d08183bc..aee4d974 100644 --- a/src/front/Components/Layouts/Folder/FolderInformation/index.tsx +++ b/src/front/Components/Layouts/Folder/FolderInformation/index.tsx @@ -23,6 +23,7 @@ import AnchoringProcessingInfo from "./elements/AnchoringProcessingInfo"; import FolderService from "src/common/Api/LeCoffreApi/sdk/FolderService"; import NoteService from "src/common/Api/LeCoffreApi/sdk/NoteService"; +import DocumentService from "src/common/Api/LeCoffreApi/sdk/DocumentService"; export enum AnchorStatus { "VERIFIED_ON_CHAIN" = "VERIFIED_ON_CHAIN", @@ -107,25 +108,39 @@ export default function FolderInformation(props: IProps) { */ // TODO: review - return FolderService.getFolderByUid(folderUid).then((process: any) => { + return FolderService.getFolderByUid(folderUid).then(async (process: any) => { if (process) { const folder: any = process.processData; - NoteService.getNotes().then((processes: any) => { - if (processes.length > 0) { - let notes: any[] = processes.map((process: any) => process.processData); + await new Promise((resolve: () => void) => { + NoteService.getNotes().then((processes: any) => { + if (processes.length > 0) { + let notes: any[] = processes.map((process: any) => process.processData); - // FilterBy folder.uid - notes = notes.filter((note: any) => note.folder.uid === folderUid); + // FilterBy folder.uid + notes = notes.filter((note: any) => note.folder.uid === folderUid); - if (notes.length > 0) { - folder.notes = notes; + if (notes.length > 0) { + folder.notes = notes; + } } - } - - setFolder(folder); + resolve(); + }); }); + await new Promise((resolve: () => void) => { + DocumentService.getDocuments().then((processes: any[]) => { + if (processes.length > 0) { + const documents: any[] = processes.map((process: any) => process.processData); + for (const customer of folder.customers) { + customer.documents = documents.filter((document: any) => document.depositor.uid === customer.uid); + } + } + resolve(); + }); + }); + + setFolder(folder); } }); }, [folderUid]); diff --git a/src/front/Components/Layouts/Folder/ViewDocuments/index.tsx b/src/front/Components/Layouts/Folder/ViewDocuments/index.tsx index 6b661a2b..59ad2998 100644 --- a/src/front/Components/Layouts/Folder/ViewDocuments/index.tsx +++ b/src/front/Components/Layouts/Folder/ViewDocuments/index.tsx @@ -18,8 +18,7 @@ import TextAreaField from "@Front/Components/DesignSystem/Form/TextareaField"; import MessageBox from "@Front/Components/Elements/MessageBox"; import DocumentService from "src/common/Api/LeCoffreApi/sdk/DocumentService"; - -import MessageBus from "src/sdk/MessageBus"; +import FileService from "src/common/Api/LeCoffreApi/sdk/FileService"; type IProps = {}; type IPropsClass = { @@ -91,10 +90,10 @@ class ViewDocumentsClass extends BasePage { )}
- {this.state.selectedFile.mimetype === "application/pdf" || - this.state.selectedFile.mimetype === "image/jpeg" || - this.state.selectedFile.mimetype === "image/png" || - this.state.selectedFile.mimetype === "image/jpg" ? ( + {this.state.selectedFile.file_blob.type === "application/pdf" || + this.state.selectedFile.file_blob.type === "image/jpeg" || + this.state.selectedFile.file_blob.type === "image/png" || + this.state.selectedFile.file_blob.type === "image/jpg" ? ( { override async componentDidMount() { try { - /* TODO: review - const document = await Documents.getInstance().getByUid(this.props.documentUid, { - files: { - where: { archived_at: null }, - }, - document_type: true, - folder: true, - depositor: true, - }); - */ - const document: any = await new Promise((resolve: (document: any) => void) => { DocumentService.getDocumentByUid(this.props.documentUid).then(async (process: any) => { if (process) { const document: any = process.processData; - const ps: any[] = await MessageBus.getInstance().getFiles(); - const p: any = ps.find((p: any) => p.processData.document.get('uid') === document.uid); - if (p) { - - document.files = [p.processData]; + if (document.files && document.files.length > 0) { + const files: any[] = []; + for (const file of document.files) { + files.push((await FileService.getFileByUid(file.uid)).processData); + } + document.files = files; } resolve(document); @@ -248,12 +237,7 @@ class ViewDocumentsClass extends BasePage { private async getFilePreview(): Promise { try { - // TODO: review - const file: any = this.state.selectedFile.fileBlob; - - const fileBlob: Blob = new Blob([file.data], { type: file.type }); - //const fileBlob: Blob = await Files.getInstance().download(this.state.selectedFile?.uid as string); - + const fileBlob: Blob = new Blob([this.state.selectedFile.file_blob.data], { type: this.state.selectedFile.file_blob.type }); this.setState({ fileBlob, }); @@ -263,12 +247,9 @@ class ViewDocumentsClass extends BasePage { } private downloadFile() { - // TODO: review - //if (!this.state.fileBlob) return; - const file: any = this.state.selectedFile.fileBlob; + if (!this.state.fileBlob) return; - const blob = new Blob([file.data], { type: file.type }); - const url = URL.createObjectURL(blob); + const url = URL.createObjectURL(this.state.fileBlob); const a = document.createElement('a'); a.href = url; a.download = this.state.selectedFile.file_name; diff --git a/src/sdk/MessageBus.ts b/src/sdk/MessageBus.ts index fa491596..f97e2755 100644 --- a/src/sdk/MessageBus.ts +++ b/src/sdk/MessageBus.ts @@ -100,7 +100,7 @@ export default class MessageBus { }); } - public getFiles(): Promise { + public getFileByUid(uid: string): Promise { return new Promise((resolve: (files: any[]) => void, reject: (error: string) => void) => { this.getProcesses().then(async (processes: any) => { const files: any[] = []; @@ -129,7 +129,7 @@ export default class MessageBus { } } - if (publicDataDecoded['utype'] !== 'file') { + if (!(publicDataDecoded['uid'] && publicDataDecoded['uid'] === uid && publicDataDecoded['utype'] && publicDataDecoded['utype'] === 'file')) { continue; } @@ -171,9 +171,10 @@ export default class MessageBus { } files.push(file); + break; } - resolve(files); + resolve(files[0]); }).catch(reject); }); }