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,