From 18c7404020d4d09565cab199adc6e5a71f3502ef Mon Sep 17 00:00:00 2001 From: vmaubert Date: Wed, 18 Dec 2024 10:39:26 +0100 Subject: [PATCH] ajoute de nouvelle var d'env --- .env.example | 3 ++ README.md | 2 + server/services/imapService/index.ts | 72 +++++++++++++++++--------- server/utils/config.ts | 12 +++++ shared/referential/Laboratory.ts | 5 ++ shared/schema/Laboratory/Laboratory.ts | 5 +- 6 files changed, 73 insertions(+), 26 deletions(-) create mode 100644 shared/referential/Laboratory.ts diff --git a/.env.example b/.env.example index 8ca1a6fb..754cdda0 100644 --- a/.env.example +++ b/.env.example @@ -14,6 +14,9 @@ MAILER_PORT=1025 MAILER_USER= MAILER_PASSWORD= + +INBOX_MAILBOX_NAME=INBOX +INBOX_TRASHBOX_NAME=INBOX.INBOX.Trash INBOX_HOST= INBOX_PORT= INBOX_USER= diff --git a/README.md b/README.md index 8e4d85b5..c305b9f2 100644 --- a/README.md +++ b/README.md @@ -61,6 +61,8 @@ S3_ACCESS_KEY_ID S3_SECRET_ACCESS_KEY S3_BUCKET S3_FILE_PATH +INBOX_MAILBOX_NAME +INBOX_TRASHBOX_NAME INBOX_HOST INBOX_PORT INBOX_USER diff --git a/server/services/imapService/index.ts b/server/services/imapService/index.ts index 06251dd9..eb43184a 100644 --- a/server/services/imapService/index.ts +++ b/server/services/imapService/index.ts @@ -3,6 +3,7 @@ import { createWriteStream } from 'node:fs'; import { simpleParser } from 'mailparser'; import config from '../../utils/config'; import { isNull } from 'lodash'; +import { LaboratoryName } from '../../../shared/referential/Laboratory'; const run =async () => { @@ -28,7 +29,7 @@ const run =async () => { }); await client.connect() // Select and lock a mailbox. Throws if mailbox does not exist - const lock = await client.getMailboxLock('INBOX'); + const lock = await client.getMailboxLock(config.inbox.mailboxName); try { // fetch latest message source // client.mailbox includes information about currently selected mailbox @@ -37,32 +38,53 @@ const run =async () => { // const message = await client.fetchOne(`${client.mailbox.exists}`, { source: true }); // console.log(message.source.toString()); - // let pj = null + const messagesToRead: {messageUid: number, laboratoryName: LaboratoryName}[] = [] // list subjects for all messages // uid value is always included in FETCH response, envelope strings are in unicode. - // for await (const message of client.fetch('1:*', { envelope: true, bodyStructure: true })) { - // - // if( message.uid === 4767){ - // - // - // if (message.bodyStructure.childNodes.length) { - // for (const node of message.bodyStructure.childNodes){ - // - // if( node.type === 'application/vnd.oasis.opendocument.text'){ - // pj = { messageUid: `${message.uid}` } - // } - // } - // } - // } - // } - - - //null permet de récupérer tout l'email - //@ts-expect-error TS2345 - const downloadObject = await client.download('4767',null, {uid: true}) - - const parsed = await simpleParser(downloadObject.content) - createWriteStream(parsed.attachments[2].filename ?? '').write(parsed.attachments[2].content) + for await (const message of client.fetch('1:*', { envelope: true, bodyStructure: true })) { + + console.log('Email reçu', message.envelope.sender[0].address, message.envelope.subject) + + let laboratoryName: LaboratoryName | null = null + + //FIXME check sender email en plus de l'objet pour trouver le bon labo + //FIXME mettre le bon objet + //FIXME ajouter un test + if (message.envelope.subject === 'TEST LABO') { + laboratoryName = 'GIR 49' + } + + if (laboratoryName !== null) { + messagesToRead.push({messageUid: message.uid, laboratoryName}) + console.log(' =====> ', laboratoryName) + }else { + console.log(' =====> IGNORÉ') + } + // if (message.bodyStructure.childNodes.length) { + // for (const node of message.bodyStructure.childNodes){ + // + // if( node.type === 'application/vnd.oasis.opendocument.text'){ + // messageIdsToRead.push(message.uid) + // } + // } + } + for (const messagesToReadElement of messagesToRead) { + //null permet de récupérer tout l'email + //@ts-expect-error TS2345 + const downloadObject = await client.download(messagesToReadElement.messageUid,null, {uid: true}) + + const parsed = await simpleParser(downloadObject.content) + // console.log(`Email reçu pour ${messagesToReadElement.laboratoryName}: ${parsed}`) + + await client.messageMove(`${messagesToReadElement.messageUid}`, config.inbox.trashboxName, {uid: true}) + // createWriteStream(parsed.attachments[2].filename ?? '').write(parsed.attachments[2].content) + + + + } + + + } } catch(e){ console.error(e) diff --git a/server/utils/config.ts b/server/utils/config.ts index 07bb996f..bcc286a3 100644 --- a/server/utils/config.ts +++ b/server/utils/config.ts @@ -79,6 +79,8 @@ interface Config { }; }; inbox: { + mailboxName: string + trashboxName: string host: string | null; user: string | null; password: string | null; @@ -252,6 +254,16 @@ const config = convict({ } }, inbox: { + mailboxName: { + env: 'INBOX_MAILBOX_NAME', + format: String, + default: 'Inbox' + }, + trashboxName: { + env: 'INBOX_TRASHBOX_NAME', + format: String, + default: 'Trash' + }, host: { env: 'INBOX_HOST', format: String, diff --git a/shared/referential/Laboratory.ts b/shared/referential/Laboratory.ts new file mode 100644 index 00000000..2eeebcee --- /dev/null +++ b/shared/referential/Laboratory.ts @@ -0,0 +1,5 @@ +import { z } from 'zod'; + +export const laboratoryNames = ['LDA 72', 'GIR 49'] as const satisfies string[] +export const laboratoryNameValidator = z.enum(laboratoryNames) +export type LaboratoryName = z.infer diff --git a/shared/schema/Laboratory/Laboratory.ts b/shared/schema/Laboratory/Laboratory.ts index 10791d8a..0d1f1996 100644 --- a/shared/schema/Laboratory/Laboratory.ts +++ b/shared/schema/Laboratory/Laboratory.ts @@ -1,7 +1,10 @@ import { z } from 'zod'; +import { laboratoryNameValidator } from '../../referential/Laboratory'; + + export const Laboratory = z.object({ id: z.string().uuid(), - name: z.string(), + name: laboratoryNameValidator, email: z.string().email(), });