mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-12-01 00:43:15 +00:00
fix(mail-ingest): keep flight-like IDs from overriding bookings
Recognize compound booking references separately from flight numbers. Require flight evidence before overriding named lodging or restaurant types so a local mail reader keeps the intended schema. Closes #3271
This commit is contained in:
@@ -17,6 +17,7 @@
|
||||
* flight take minutes on a CPU host. The flat results map into the kitinerary pipeline via the
|
||||
* existing `nuExtractToKiReservations` mapper, so nothing downstream changes.
|
||||
*/
|
||||
import { airportByCode } from '../../airports/airports.contract';
|
||||
import type { KiReservation } from '../../booking-import/kitinerary.types';
|
||||
import { nuExtractToKiReservations } from '../clients/nuextract';
|
||||
import { parseLenientJson } from '../lenient-json';
|
||||
@@ -109,6 +110,22 @@ const TYPE_KEYWORDS: [FlatType, RegExp][] = [
|
||||
*/
|
||||
const EVENT_KEYWORDS = /\b(ticket|concert|konzert|veranstaltung|eintritt|admission)\b/i;
|
||||
|
||||
const FLIGHT_NUMBER_LABEL =
|
||||
/\b(?:flight(?:[ \t]+(?:number|no\.?))?|(?:hin|rück|rueck)?flug(?:nummer|nr\.?)?)[ \t\r\n:#]*$/i;
|
||||
const BOOKING_REFERENCE_LABEL =
|
||||
/\b(?:(?:trip|booking|reservation)[ \t]+(?:id|ref(?:erence)?|code|no\.?|number)|confirmation(?:[ \t]+(?:id|code|number))?|pnr|reference|buchungs(?:code|nummer|referenz)|reservierungsnummer|reiseplan|best(?:ä|ae)tigungs[- \t]?(?:code|nummer))[ \t\r\n:#]*$/i;
|
||||
const AIRPORT_PAIR = /\b([A-Z]{3})[ \t]*(?:->|[–—/→-])[ \t]*([A-Z]{3})\b/g;
|
||||
const FLIGHT_CONTEXT = 64;
|
||||
|
||||
/** Itinerary tables may put their route beside or on the line next to the flight number. */
|
||||
function hasNearbyAirportPair(before: string, number: string, after: string): boolean {
|
||||
const context = before.split('\n').slice(-2).join('\n') + number + after.split('\n').slice(0, 2).join('\n');
|
||||
for (const [, from, to] of context.matchAll(AIRPORT_PAIR)) {
|
||||
if (from && to && from !== to && airportByCode(from) && airportByCode(to)) return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/** The transport half of the table, for the flight gate in readType. */
|
||||
const TRANSPORT_FLAT_TYPES = new Set<FlatType>(['train', 'bus', 'ferry', 'car']);
|
||||
|
||||
@@ -126,10 +143,9 @@ interface TypeReading {
|
||||
function readType(text: string): TypeReading {
|
||||
const named = TYPE_KEYWORDS.filter(([, re]) => re.test(text)).map(([type]) => type);
|
||||
// The flight path forces type 'flight' on everything it returns, so it must not
|
||||
// claim a document that names another kind of transport: a German rail ticket
|
||||
// carries numbers like "IC 2023" or "RE 4711", which the two-letters-plus-digits
|
||||
// test cannot tell from an airline code (#2076).
|
||||
if (detectFlightNumbers(text).length > 0 && !named.some((type) => TRANSPORT_FLAT_TYPES.has(type))) {
|
||||
// claim another transport booking (#2076). A code alone cannot override a
|
||||
// named non-transport type either: it may be that booking's reference (#3271).
|
||||
if (!named.some((type) => TRANSPORT_FLAT_TYPES.has(type)) && detectFlightNumbers(text, named.length > 0).length > 0) {
|
||||
return { type: 'flight', candidates: [] };
|
||||
}
|
||||
if (named.length > 1) return { type: null, candidates: named };
|
||||
@@ -137,14 +153,23 @@ function readType(text: string): TypeReading {
|
||||
return { type: EVENT_KEYWORDS.test(text) ? 'event' : null, candidates: [] };
|
||||
}
|
||||
|
||||
/** Detect flight numbers (order-preserving, deduped) — also the "is this a flight doc" test. */
|
||||
export function detectFlightNumbers(text: string): string[] {
|
||||
const out: string[] = [];
|
||||
for (const m of text.matchAll(/\b([A-Z]{2})\s?(\d{2,4})\b/g)) {
|
||||
const fn = `${m[1]}${m[2]}`;
|
||||
if (!out.includes(fn)) out.push(fn);
|
||||
/** Flight numbers in document order. When another type is named, require evidence beside each code. */
|
||||
export function detectFlightNumbers(text: string, requireDetails = false): string[] {
|
||||
const out = new Set<string>();
|
||||
for (const match of text.matchAll(/\b([A-Z]{2})\s?(\d{2,4})\b/g)) {
|
||||
const start = match.index;
|
||||
const end = start + match[0].length;
|
||||
const before = text.slice(Math.max(0, start - FLIGHT_CONTEXT), start);
|
||||
if (BOOKING_REFERENCE_LABEL.test(before)) continue;
|
||||
const labelled = FLIGHT_NUMBER_LABEL.test(before);
|
||||
const after = text.slice(end, end + FLIGHT_CONTEXT);
|
||||
// A numeric segment joined by a hyphen usually belongs to a compound reference.
|
||||
// An explicit flight label still allows a number followed by a date.
|
||||
if (!labelled && (/\d-$/.test(before) || /^-\d/.test(after))) continue;
|
||||
if (requireDetails && !labelled && !hasNearbyAirportPair(before, match[0], after)) continue;
|
||||
out.add(`${match[1]}${match[2]}`);
|
||||
}
|
||||
return out;
|
||||
return [...out];
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user