Skip to content

Commit

Permalink
ajoute de nouvelle var d'env
Browse files Browse the repository at this point in the history
  • Loading branch information
vmaubert committed Dec 18, 2024
1 parent 691b9a2 commit 18c7404
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 26 deletions.
3 changes: 3 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
72 changes: 47 additions & 25 deletions server/services/imapService/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand All @@ -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
Expand All @@ -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)

Check failure on line 76 in server/services/imapService/index.ts

View workflow job for this annotation

GitHub Actions / build_test (22.x)

'parsed' is declared but its value is never read.
// 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)
Expand Down
12 changes: 12 additions & 0 deletions server/utils/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ interface Config {
};
};
inbox: {
mailboxName: string
trashboxName: string
host: string | null;
user: string | null;
password: string | null;
Expand Down Expand Up @@ -252,6 +254,16 @@ const config = convict<Config>({
}
},
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,
Expand Down
5 changes: 5 additions & 0 deletions shared/referential/Laboratory.ts
Original file line number Diff line number Diff line change
@@ -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<typeof laboratoryNameValidator>
5 changes: 4 additions & 1 deletion shared/schema/Laboratory/Laboratory.ts
Original file line number Diff line number Diff line change
@@ -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(),
});

Expand Down

0 comments on commit 18c7404

Please sign in to comment.