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.
This commit is contained in:
Maurice
2026-11-23 14:56:19 +01:00
parent eaa557343e
commit 303d161ecf
3 changed files with 35 additions and 3 deletions
@@ -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(
@@ -74,7 +74,8 @@ export class MailIngestReadingService {
trips: readonly TripOption[];
now: Date;
}): Promise<MailReading> {
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<string, unknown> {
return {
@@ -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', () => {