From 8f5988ee33576061665f68b2bfeffec8ef19ce5b Mon Sep 17 00:00:00 2001 From: Maurice Date: Mon, 23 Nov 2026 14:56:20 +0100 Subject: [PATCH] fix(mail-ingest): match each AI leg to the data leg it agrees with Under one booking reference every leg after the first was compared with the first and went to the inbox as a disagreement. --- .../mail-ingest/mail-ingest-reading.service.ts | 11 ++++++++++- .../nest/mail-ingest/mail-ingest.pipeline.test.ts | 14 ++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) 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 b423b8369..17af0fc8f 100644 --- a/server/src/nest/mail-ingest/mail-ingest-reading.service.ts +++ b/server/src/nest/mail-ingest/mail-ingest-reading.service.ts @@ -138,7 +138,7 @@ export class MailIngestReadingService { .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)); + const match = machineMatch(machine, item); const disagrees = match !== undefined && criticalDiffers(match, item); return { item: match && !disagrees ? match : item, @@ -154,6 +154,15 @@ export class MailIngestReadingService { } } +/** The machine-read booking an AI item stands for: of several under one reference, the one it agrees with. */ +function machineMatch( + machine: readonly BookingImportPreviewItem[], + item: BookingImportPreviewItem, +): BookingImportPreviewItem | undefined { + const same = machine.filter((candidate) => sameBooking(candidate, item)); + return same.find((candidate) => !criticalDiffers(candidate, item)) ?? same[0]; +} + function fromMachine(item: BookingImportPreviewItem): MailDraft { return { item, 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 2ba1a5e02..7fb399c22 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 @@ -546,6 +546,20 @@ describe('what a mail names', () => { } as AiReading); } + it('adds both legs of a round trip under one reference the AI and the data agree on', 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]); + aiReads([FLIGHT, RETURN]); + + const entries = await entriesOf(await forward(user.id, eml('round-trip', 'Fwd: Your flights', BOTH))); + expect(entries.map((entry) => [entry.resolution, entry.reason])).toEqual([ + ['auto_applied', null], + ['auto_applied', null], + ]); + expect(await countRows(orm(), Reservations, { trip: trip.id })).toBe(2); + }); + it('announces the place it created as the place routes do, with its name and position', async () => { const user = await person(); const trip = await makeTrip(orm(), user.id, { start_date: '2030-03-01', end_date: '2030-03-10' });