Skip to content

Commit

Permalink
gère mieux les messages d'erreur
Browse files Browse the repository at this point in the history
  • Loading branch information
vmaubert committed Dec 24, 2024
1 parent 8fed2cc commit c61d4be
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 28 deletions.
29 changes: 8 additions & 21 deletions server/services/imapService/girpa.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
ExportDataFromEmail,
ExportDataSubstance,
ExportResidue,
ExportSample,
ExportSample, ExtractError,
IsSender,
LaboratoryConf
} from './index';
Expand Down Expand Up @@ -89,7 +89,7 @@ export const analyseXmlValidator = z.object({
Substance_active_anglais: residueEnglishNameValidator
});
// Visible for testing
export const extractSample = (obj: unknown): Omit<ExportSample, 'pdfFile'>[] | null => {
export const extractSample = (obj: unknown): Omit<ExportSample, 'pdfFile'>[] => {
const echantillonValidator = z.object({
Code_échantillon: z.string(),
Commentaire: z.string(),
Expand All @@ -104,10 +104,9 @@ export const extractSample = (obj: unknown): Omit<ExportSample, 'pdfFile'>[] | 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 === '-' ||
Expand All @@ -120,9 +119,7 @@ export const extractSample = (obj: unknown): Omit<ExportSample, 'pdfFile'>[] | 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 === '-'
Expand All @@ -140,10 +137,7 @@ export const extractSample = (obj: unknown): Omit<ExportSample, 'pdfFile'>[] | n
notes: echantillon.Commentaire,
substances
};
});
}
console.log('Erreur: ', result.error);
return null;
});;
};

const exportDataFromEmail: ExportDataFromEmail = (email) => {
Expand All @@ -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 ?? '');
Expand All @@ -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')
}

};
Expand Down
26 changes: 19 additions & 7 deletions server/services/imapService/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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' }
Expand All @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
}
}
}
Expand Down

0 comments on commit c61d4be

Please sign in to comment.