fix(mail-ingest): keep bookings the data names that the AI left out

When the AI returned fewer bookings than the machine-readable data, the others were dropped without an entry.
This commit is contained in:
Maurice
2026-11-23 14:56:20 +01:00
parent 8f5988ee33
commit 626e19c534
2 changed files with 22 additions and 3 deletions
@@ -134,15 +134,17 @@ export class MailIngestReadingService {
const mapped = await Promise.all(
bookings.map((booking) => this.bookingImport.mapAiReservations(booking.reservations, 'mail')),
);
return bookings
const matched = new Set<BookingImportPreviewItem>();
const drafts = bookings
.flatMap((booking, i) => mapped[i].map((item) => ({ booking, item: withTextReference(item) })))
.slice(0, MAIL_INGEST_LIMITS.storedBookings)
.map(({ booking, item }) => {
.map(({ booking, item }): MailDraft => {
const match = machineMatch(machine, item);
if (match) matched.add(match);
const disagrees = match !== undefined && criticalDiffers(match, item);
return {
item: match && !disagrees ? match : item,
source: 'ai' as const,
source: 'ai',
kind: booking.kind,
aiTripId: booking.tripRef === null ? null : (tripIdOf.get(booking.tripRef) ?? null),
uncertainFields: booking.uncertainFields,
@@ -151,6 +153,9 @@ export class MailIngestReadingService {
disagrees,
};
});
// A booking the machine-readable data names and the AI left out still reaches the trip or the inbox.
const unread = machine.filter((item) => !matched.has(item)).map(fromMachine);
return [...drafts, ...unread].slice(0, MAIL_INGEST_LIMITS.storedBookings);
}
}
@@ -560,6 +560,20 @@ describe('what a mail names', () => {
expect(await countRows(orm(), Reservations, { trip: trip.id })).toBe(2);
});
it('keeps a booking the data names and the AI left out', 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, DINNER]);
aiReads([FLIGHT]);
const entries = await entriesOf(await forward(user.id, eml('flight-and-dinner', 'Fwd: Your trip', BOTH)));
expect(entries.map((entry) => [entry.source, entry.resolution])).toEqual([
['ai', 'auto_applied'],
['kitinerary', 'auto_applied'],
]);
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' });