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.
This commit is contained in:
Maurice
2026-11-23 14:56:20 +01:00
parent 4cef885e12
commit 8f5988ee33
2 changed files with 24 additions and 1 deletions
@@ -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,
@@ -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' });