From c61d4be93a5ba37e8b1e0df22a818e540159e4e2 Mon Sep 17 00:00:00 2001 From: vmaubert Date: Tue, 24 Dec 2024 13:57:52 +0100 Subject: [PATCH] =?UTF-8?q?g=C3=A8re=20mieux=20les=20messages=20d'erreur?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/services/imapService/girpa.ts | 29 ++++++++-------------------- server/services/imapService/index.ts | 26 ++++++++++++++++++------- 2 files changed, 27 insertions(+), 28 deletions(-) diff --git a/server/services/imapService/girpa.ts b/server/services/imapService/girpa.ts index 719198e2..edf34eb1 100644 --- a/server/services/imapService/girpa.ts +++ b/server/services/imapService/girpa.ts @@ -11,7 +11,7 @@ import { ExportDataFromEmail, ExportDataSubstance, ExportResidue, - ExportSample, + ExportSample, ExtractError, IsSender, LaboratoryConf } from './index'; @@ -89,7 +89,7 @@ export const analyseXmlValidator = z.object({ Substance_active_anglais: residueEnglishNameValidator }); // Visible for testing -export const extractSample = (obj: unknown): Omit[] | null => { +export const extractSample = (obj: unknown): Omit[] => { const echantillonValidator = z.object({ Code_échantillon: z.string(), Commentaire: z.string(), @@ -104,10 +104,9 @@ export const extractSample = (obj: unknown): Omit[] | n }) }); - const result = validator.safeParse(obj); + const result = validator.parse(obj); - if (result.success) { - return result.data.Rapport.Echantillon.map((echantillon) => { + return result.Rapport.Echantillon.map((echantillon) => { const substances: ExportDataSubstance[] = echantillon.Analyse.filter( (a) => a.LMR === '-' || @@ -120,9 +119,7 @@ export const extractSample = (obj: unknown): Omit[] | n a.Substance_active_anglais ); if (substance === null) { - //FIXME comment gérer les erreurs ?! - console.error('Résidu non trouvé:', a.Substance_active_CAS, a.Substance_active_anglais) - return null; + throw new ExtractError(`Résidu non trouvé:, ${a.Substance_active_CAS}, ${a.Substance_active_anglais}`) } return a.LMR === '-' @@ -140,10 +137,7 @@ export const extractSample = (obj: unknown): Omit[] | n notes: echantillon.Commentaire, substances }; - }); - } - console.log('Erreur: ', result.error); - return null; + });; }; const exportDataFromEmail: ExportDataFromEmail = (email) => { @@ -158,19 +152,13 @@ const exportDataFromEmail: ExportDataFromEmail = (email) => { const extractAnalyse = extractSample(obj) - if( extractAnalyse === null ){ - //FIXME error - return null - } - const analyseWithPdf: ExportSample[] = [] for (const analyse of extractAnalyse) { const pdfAttachment = email.attachments.find(({ contentType, filename }) => contentType === 'application/pdf' && filename?.startsWith(analyse.sampleReference)) if (pdfAttachment === undefined) { - //FIXME error - return null + throw new ExtractError(`Aucun fichier pdf pour ${analyse.sampleReference}`) } const pdfFile: File = new File([pdfAttachment.content], pdfAttachment.filename ?? ''); @@ -180,8 +168,7 @@ const exportDataFromEmail: ExportDataFromEmail = (email) => { return analyseWithPdf } else { - console.log('Aucun XML', email.attachments); - return null + throw new ExtractError('Pas de fichier XML dans les pièces jointes') } }; diff --git a/server/services/imapService/index.ts b/server/services/imapService/index.ts index d558bfde..df73a701 100644 --- a/server/services/imapService/index.ts +++ b/server/services/imapService/index.ts @@ -14,6 +14,12 @@ import { girpaConf } from './girpa'; const laboratoriesWithConf = ['GIR 49'] as const satisfies LaboratoryName[]; type LaboratoryWithConf = (typeof laboratoriesWithConf)[number]; +export class ExtractError extends Error { + constructor(message: string) { + super(message); + } +} + export type ExportResidue = | { value: SimpleResidue; kind: 'SimpleResidue' } | { value: ComplexResidue; kind: 'ComplexResidue' } @@ -34,7 +40,7 @@ export type ExportSample = { pdfFile: File; substances: ExportDataSubstance[]; }; -export type ExportDataFromEmail = (email: ParsedMail) => null | ExportSample[]; +export type ExportDataFromEmail = (email: ParsedMail) => ExportSample[]; export type LaboratoryConf = { isSender: IsSender; @@ -124,11 +130,13 @@ const run = async () => { //FIXME trash // await client.messageMove(messageUid, config.inbox.trashboxName, {uid: true}) - const data = - laboratoriesConf[message.laboratoryName].exportDataFromEmail(parsed); + try { + const data = + laboratoriesConf[message.laboratoryName].exportDataFromEmail( + parsed + ); - if (data !== null) { - initKysely(config.databaseUrl) + initKysely(config.databaseUrl); for (const analyse of data) { const { url, documentId } = await getUploadSignedUrlS3( analyse.pdfFile.name @@ -162,8 +170,12 @@ const run = async () => { console.log(JSON.stringify(data, null, 4)); // createWriteStream(parsed.attachments[2].filename ?? '').write(parsed.attachments[2].content) } - } else { - //FIXME + } catch (e: any) { + console.error( + `Email "${parsed.subject}" from "${parsed.from?.value[0].address}" ignoré => `, + e.message + ); + //FIXME envoyer une notification (mattermost ? email ?) aux devs } } }