From 303d161ecf3be427b3eecb9709269a0159aa5987 Mon Sep 17 00:00:00 2001 From: Maurice Date: Mon, 23 Nov 2026 14:56:19 +0100 Subject: [PATCH] fix(mail-ingest): keep a booking that fails the item shape in the inbox Such a booking made the whole mail fail and retry instead of waiting to be filled in by hand. A reference read as a number is now read as its text. --- .../mail-ingest-process.service.ts | 3 ++- .../mail-ingest-reading.service.ts | 11 +++++++-- .../mail-ingest/mail-ingest.pipeline.test.ts | 24 +++++++++++++++++++ 3 files changed, 35 insertions(+), 3 deletions(-) diff --git a/server/src/nest/mail-ingest/mail-ingest-process.service.ts b/server/src/nest/mail-ingest/mail-ingest-process.service.ts index adfe5586e..54dafd1fe 100644 --- a/server/src/nest/mail-ingest/mail-ingest-process.service.ts +++ b/server/src/nest/mail-ingest/mail-ingest-process.service.ts @@ -295,7 +295,8 @@ export class MailIngestProcessService { messageId: message.id, userId: message.user_id, position: booking.position, - itemJson: encodeJson(MAIL_INGEST_ITEM, booking.draft.item), + // A booking that fails the shape waits as the mail alone, to be filled in by hand. + itemJson: booking.verdict.checks.schema === 'fail' ? null : encodeJson(MAIL_INGEST_ITEM, booking.draft.item), source: booking.draft.source, checksJson: encodeJson(MAIL_INGEST_CHECKS, booking.verdict.checks), uncertainFields: encodeJson( diff --git a/server/src/nest/mail-ingest/mail-ingest-reading.service.ts b/server/src/nest/mail-ingest/mail-ingest-reading.service.ts index 1e9f555ac..b423b8369 100644 --- a/server/src/nest/mail-ingest/mail-ingest-reading.service.ts +++ b/server/src/nest/mail-ingest/mail-ingest-reading.service.ts @@ -74,7 +74,8 @@ export class MailIngestReadingService { trips: readonly TripOption[]; now: Date; }): Promise { - const { mail, machine, person, prefs, trips, now } = args; + const { mail, person, prefs, trips, now } = args; + const machine = args.machine.map(withTextReference); const skipped = await this.aiSkipped(person.id, prefs, now); const refs = trips.map((trip, i) => ({ trip, ref: `t${i + 1}` })); const reading = skipped @@ -134,7 +135,7 @@ export class MailIngestReadingService { bookings.map((booking) => this.bookingImport.mapAiReservations(booking.reservations, 'mail')), ); return bookings - .flatMap((booking, i) => mapped[i].map((item) => ({ booking, item }))) + .flatMap((booking, i) => mapped[i].map((item) => ({ booking, item: withTextReference(item) }))) .slice(0, MAIL_INGEST_LIMITS.storedBookings) .map(({ booking, item }) => { const match = machine.find((candidate) => sameBooking(candidate, item)); @@ -166,6 +167,12 @@ function fromMachine(item: BookingImportPreviewItem): MailDraft { }; } +/** A reference a reader gave as a number, as the text it is. */ +function withTextReference(item: BookingImportPreviewItem): BookingImportPreviewItem { + const reference: unknown = item.confirmation_number; + return typeof reference === 'number' ? { ...item, confirmation_number: String(reference) } : item; +} + /** A booking as a hint for the AI: what it is, when, its reference and route, no more. */ function hintOf(item: BookingImportPreviewItem): Record { return { diff --git a/server/tests/unit/nest/mail-ingest/mail-ingest.pipeline.test.ts b/server/tests/unit/nest/mail-ingest/mail-ingest.pipeline.test.ts index a5671cb26..98f0dd08e 100644 --- a/server/tests/unit/nest/mail-ingest/mail-ingest.pipeline.test.ts +++ b/server/tests/unit/nest/mail-ingest/mail-ingest.pipeline.test.ts @@ -575,6 +575,30 @@ describe('what a mail names', () => { } }); + it('keeps a booking that fails the shape in the inbox, and adds the others of the mail', async () => { + const user = await person(); + const trip = await makeTrip(orm(), user.id, { start_date: '2030-03-01', end_date: '2030-03-10' }); + machine.mockResolvedValue([FLIGHT, { ...RETURN, title: '' }]); + + const messageId = await forward(user.id, eml('half-read', 'Fwd: Your flights', BOTH)); + expect(await findRow(orm(), MailIngestMessages, { id: messageId })).toMatchObject({ status: 'processed' }); + const entries = await entriesOf(messageId); + expect(entries.map((entry) => [entry.resolution, entry.reason, entry.item_json === null])).toEqual([ + ['auto_applied', null, false], + ['pending', 'missing_critical_field', true], + ]); + expect(await countRows(orm(), Reservations, { trip: trip.id })).toBe(1); + }); + + it('reads a reference given as a number as its text', async () => { + const user = await person(); + const trip = await makeTrip(orm(), user.id, { start_date: '2030-03-01', end_date: '2030-03-10' }); + machine.mockResolvedValue([{ ...FLIGHT, confirmation_number: 482913 as unknown as string }]); + + const [entry] = await entriesOf(await forward(user.id, eml('numbered', 'Fwd: Booking 482913', BOTH))); + expect(entry).toMatchObject({ resolution: 'auto_applied' }); + expect(await findRow(orm(), Reservations, { trip: trip.id })).toMatchObject({ confirmation_number: '482913' }); + }); }); describe('a mail that keeps failing', () => {