From 5e8cc0b90af323e554a564233c79cd67abdc3003 Mon Sep 17 00:00:00 2001 From: Maurice Date: Mon, 23 Nov 2026 15:39:36 +0100 Subject: [PATCH] fix(mail-ingest): read a transfer booking with a numeric code instead of filing it as a forwarding confirmation Outside Gmail and Outlook senders, only a subject naming mail forwarding counts, so a bare 'transfert' in a shuttle or airport transfer confirmation no longer hides the booking. --- server/src/nest/mail-ingest/mail-classify.ts | 9 ++++++--- .../unit/nest/mail-ingest/mail-ingest-helpers.test.ts | 7 +++++++ 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/server/src/nest/mail-ingest/mail-classify.ts b/server/src/nest/mail-ingest/mail-classify.ts index 32be0b2c8..336d27720 100644 --- a/server/src/nest/mail-ingest/mail-classify.ts +++ b/server/src/nest/mail-ingest/mail-classify.ts @@ -13,6 +13,9 @@ type MailClass = const FORWARDING_SENDERS = /forwarding-noreply@google\.com|@accountprotection\.microsoft\.com|@microsoft\.com/i; const FORWARDING_WORD = /forwarding|weiterleitung|transfert|reenv[ií]o|inoltro|doorsturen/i; +/** The forwarding words that name mail forwarding alone: a bare "transfert" is as often an airport transfer. */ +const MAIL_FORWARDING_WORD = + /forwarding|weiterleitung|transfert (?:de |des |du )?(?:courriers?|messages?|e-?mails?|mails?)|transfert automatique|reenv[ií]o|inoltro|doorsturen/i; const CONFIRM_WORD = /confirm|bestätig|verif|conferma|bevestig/i; const CODE_RE = /(?:code|código|codice|kód)\D{0,40}?(\d{6,10})\b/i; const AUTO_REPLY_SUBJECT = @@ -33,7 +36,7 @@ export function classifyMail(mail: WalkedMail): MailClass { const code = CODE_RE.exec(mail.body)?.[1] ?? null; // A subject alone is no proof: "Votre transfert aéroport est confirmé" is a booking. const fromProvider = FORWARDING_SENDERS.test(envelope.from ?? ''); - const subjectSays = asksToConfirmForwarding(subject); + const subjectSays = asksToConfirmForwarding(subject, fromProvider ? FORWARDING_WORD : MAIL_FORWARDING_WORD); if (fromProvider ? subjectSays || code !== null : subjectSays && code !== null) { return { kind: 'forwarding_confirmation', code }; } @@ -53,8 +56,8 @@ export function classifyMail(mail: WalkedMail): MailClass { } /** A forwarding word with a confirm word after it, found without rescanning the subject from every forwarding word. */ -function asksToConfirmForwarding(subject: string): boolean { - const word = FORWARDING_WORD.exec(subject); +function asksToConfirmForwarding(subject: string, forwardingWord: RegExp): boolean { + const word = forwardingWord.exec(subject); return word !== null && CONFIRM_WORD.test(subject.slice(word.index + word[0].length)); } diff --git a/server/tests/unit/nest/mail-ingest/mail-ingest-helpers.test.ts b/server/tests/unit/nest/mail-ingest/mail-ingest-helpers.test.ts index 89e059a56..9236ccbd1 100644 --- a/server/tests/unit/nest/mail-ingest/mail-ingest-helpers.test.ts +++ b/server/tests/unit/nest/mail-ingest/mail-ingest-helpers.test.ts @@ -51,6 +51,13 @@ describe('classifyMail', () => { ]) { expect(classifyMail(mail({ Subject: subject }, body)), subject).toEqual({ kind: 'read' }); } + const coded = mail( + { From: 'resa@navette.example', Subject: 'Transfert Orly - Paris : confirmation de réservation' }, + 'Votre code de réservation : 58213467. Rendez-vous le 2030-03-02 à 10:00.', + ); + expect(classifyMail(coded)).toEqual({ kind: 'read' }); + const relayed = mail({ Subject: 'Transfert de courrier : confirmez' }, 'Votre code : 612345678'); + expect(classifyMail(relayed)).toEqual({ kind: 'forwarding_confirmation', code: '612345678' }); }); it('reads a long subject and From header in linear time', () => {