feat(booking-import): report each unconfirmed item and check the right in confirm()

confirm() now answers { created, failed: [{ index, reason }] } instead of logging
and dropping the items it could not write, and checks the trip and reservation_edit
itself so the import route and the assistant get one answer. An item with a booking
reference records it as its origin (sync off) and a repeat import is a duplicate.
This commit is contained in:
Maurice
2026-11-13 14:57:17 +01:00
parent d2b12cd3dd
commit 5dc1de0e5b
12 changed files with 507 additions and 142 deletions
@@ -20,6 +20,7 @@ const PHOTO_EXTENSIONS: ReadonlySet<string> = new Set(LLM_PHOTO_EXTENSIONS);
const NOT_ALLOWED = 'You are not allowed to add bookings to this trip.';
const NOT_ADDED = 'The booking could not be added.';
const ALREADY_THERE = 'This booking is already in the trip.';
const BROKEN = 'This booking can no longer be read.';
/**
@@ -129,9 +130,9 @@ export class AssistantBookingImportService implements AttachedFileReader {
}
// No socket to spare from the echo: the user's own planner learns of the
// booking like every other member's.
const { created } = await this.bookingImport.confirm(String(tripId), [parsed.data], undefined);
const { created, failed } = await this.bookingImport.confirm(String(tripId), userId, [parsed.data], undefined);
const reservation = created[0];
if (!reservation) return { ok: false, value: NOT_ADDED };
if (!reservation) return { ok: false, value: failed[0]?.reason === 'duplicate' ? ALREADY_THERE : NOT_ADDED };
return {
ok: true,
value: {
@@ -1,5 +1,6 @@
import { Days } from '../../db/entities/Days.entity';
import { Reservations } from '../../db/entities/Reservations.entity';
import { Users } from '../../db/entities/Users.entity';
import { AddonsModule } from '../addons/addons.module';
import { BudgetModule } from '../budget/budget.module';
import { LlmParseModule } from '../llm-parse/llm-parse.module';
@@ -14,9 +15,11 @@ import { MikroOrmModule } from '@mikro-orm/nestjs';
import { Module } from '@nestjs/common';
/**
* `MikroOrmModule.forFeature([Days, Reservations])` (Plan 3h Task 4, BI1/BI2):
* `MikroOrmModule.forFeature([Days, Reservations, Users])` (Plan 3h Task 4, BI1/BI2):
* `BookingImportService.resolveDayId` reaches `DaysRepository.findByTripAndDate`
* and `ReservationsRepository.findNearestDayId`.
* and `ReservationsRepository.findNearestDayId`; confirm() reads the duplicate
* key through `ReservationsRepository` and the importing person's role through
* `UsersRepository` for its own permission check.
*/
@Module({
imports: [
@@ -28,7 +31,7 @@ import { Module } from '@nestjs/common';
AddonsModule,
MapsModule,
PlacesModule,
MikroOrmModule.forFeature([Days, Reservations]),
MikroOrmModule.forFeature([Days, Reservations, Users]),
],
providers: [BookingImportService, ImportJobsService],
// The HTTP surface lives in reservation-import/, which shares the prefix with
@@ -1,9 +1,10 @@
import { ADDON_IDS } from '../../addons';
import { Days } from '../../db/entities/Days.entity';
import { Reservations } from '../../db/entities/Reservations.entity';
import { Users } from '../../db/entities/Users.entity';
import { DaysRepository } from '../../db/repositories/Days.repository';
import { ReservationsRepository } from '../../db/repositories/Reservations.repository';
import type { User } from '../../types';
import type { UsersRepository } from '../../db/repositories/Users.repository';
import { AddonsService } from '../addons/addons.service';
import { BudgetService } from '../budget/budget.service';
import { DomainError } from '../common/domain-error';
@@ -15,10 +16,12 @@ import { PermissionsService } from '../permissions/permissions.service';
import { PlaceOriginsService } from '../places/place-origins.service';
import { PlacesService } from '../places/places.service';
import { RealtimeService } from '../realtime/realtime.service';
import { ReservationsService } from '../reservations/reservations.service';
import { ReservationsService, withoutOrigin, type CreateAccommodation } from '../reservations/reservations.service';
import { IMPORT_SOURCE, importExternalId } from './booking-origin';
import { KitineraryExtractorService } from './kitinerary-extractor.service';
import { mapReservations } from './kitinerary-mapper';
import type { ParsedBookingItem, KiReservation } from './kitinerary.types';
import { UniqueConstraintViolationException } from '@mikro-orm/core';
import { InjectRepository } from '@mikro-orm/nestjs';
import { Injectable } from '@nestjs/common';
import { normalizePlaceWebsite, typeToCostCategory } from '@trek/shared';
@@ -26,6 +29,7 @@ import type {
BookingImportPreviewItem,
BookingImportPreviewResponse,
BookingImportConfirmResponse,
BookingImportFailureReason,
BookingImportMode,
BookingImportFileReport,
Reservation,
@@ -47,6 +51,7 @@ export class BookingImportService {
private readonly places: PlacesService,
private readonly uow: UnitOfWork,
private readonly origins: PlaceOriginsService,
@InjectRepository(Users) private readonly usersRepo: UsersRepository,
) {}
/**
@@ -298,148 +303,203 @@ export class BookingImportService {
}
/**
* Persist a confirmed list of parsed items.
* Persist a confirmed list of parsed items for a person who may edit the
* trip's bookings; the check is made here, so every caller (the import route,
* the assistant) gets the same answer.
*
* Per item, the network work runs first (venue and endpoint geocoding, the cost's
* frozen rate). Then the venue's place row, the booking and its linked cost are one
* transaction, so a booking that fails leaves no stray place behind. place:created,
* reservation:created, accommodation:created and the cost go out after the commit.
*
* An item with a booking reference records it as its origin, and one the trip
* already holds under the same key is reported as a duplicate instead of
* being written twice. Every item that is not created is named in `failed`
* by its position, so the caller can say which one and why.
*
* @txIndependent one transaction per imported item: each booking lands whole with its
* venue and cost, and one that fails is skipped without undoing the ones before it.
*/
async confirm(
tripId: string,
userId: number,
items: BookingImportPreviewItem[],
socketId: string | undefined,
): Promise<BookingImportConfirmResponse> {
await this.requireBookingEdit(tripId, userId);
const created: Reservation[] = [];
const confirmGeoCache = new Map<string, { lat: number; lng: number } | null>();
const failed: { index: number; reason: BookingImportFailureReason }[] = [];
const geoCache = new Map<string, { lat: number; lng: number } | null>();
const venues: number[] = [];
for (const item of items) {
for (const [index, item] of items.entries()) {
const externalId = importExternalId(item);
if (externalId && (await this.alreadyImported(tripId, externalId))) {
failed.push({ index, reason: 'duplicate' });
continue;
}
try {
const { _venue, _accommodation, source: _src, ...reservationData } = item;
// A place row for venue-based reservations, written with the booking below.
let venue: Parameters<PlacesService['create']>[1] | undefined;
if (_venue?.name) {
// Geocode before creating so the broadcast carries the coordinates
let lat = _venue.lat;
let lng = _venue.lng;
if (lat == null && (_venue.address || _venue.name)) {
try {
const queries = [
_venue.address ? `${_venue.name} ${_venue.address}` : null,
_venue.address ?? null,
_venue.name,
].filter((q): q is string => !!q);
for (const q of queries) {
const hit = await this.maps.geocodeQuery(q);
if (hit) {
lat = hit.lat;
lng = hit.lng;
break;
}
}
} catch {
// geocoding failure is non-fatal
}
}
venue = {
name: _venue.name,
lat,
lng,
address: _venue.address,
// A booking mail gives the venue's site however its sender wrote it;
// it lands as https or not at all (#2483).
website: normalizePlaceWebsite(_venue.website) ?? undefined,
phone: _venue.phone,
};
}
// The same lookup preview() runs, through the same helper. On anything
// that came from a preview this is a no-op, since the endpoints already
// carry coordinates; it stays because this route can also be called
// with items that never went through one.
if (Array.isArray(reservationData.endpoints)) {
await this.geocodeEndpoints(
reservationData.endpoints,
{ location: (reservationData as { location?: string | null }).location, address: _venue?.address },
confirmGeoCache,
);
// Persist only coord'd endpoints (reservation_endpoints needs lat/lng);
// ungeocodable ones still appeared in the preview's From→To.
reservationData.endpoints = reservationData.endpoints.filter((ep) => ep.lat != null && ep.lng != null);
}
// Build create_accommodation for hotel reservations.
// start_day_id / end_day_id are resolved from check-in/out ISO dates so
// the accommodation row is actually inserted (createReservation gates on them).
let createAccommodation:
| {
place_id?: number;
start_day_id?: number;
end_day_id?: number;
check_in?: string;
check_out?: string;
confirmation?: string;
}
| undefined;
if (item.type === 'hotel' && _accommodation) {
const startDayId = await this.resolveDayId(tripId, _accommodation.check_in);
const endDayId = await this.resolveDayId(tripId, _accommodation.check_out);
createAccommodation = {
start_day_id: startDayId ?? undefined,
end_day_id: endDayId ?? undefined,
check_in: _accommodation.check_in,
check_out: _accommodation.check_out,
confirmation: _accommodation.confirmation,
};
}
// The venue, the booking and its linked cost are one write; the booking goes
// through the same service core REST, MCP and the plugin RPC take. The cost's
// rate is frozen first, outside that write, since it can fetch rates over the network.
const cost = await this.linkedCost(tripId, item);
const { place, reservation, accommodationCreated, costEvents, stayMirror } = await this.uow.transactional(
async () => {
const placeRow = venue ? await this.places.create(tripId, venue) : null;
const placeId = placeRow?.id;
const written = await this.reservations.createWithCostInTx(
tripId,
{
...reservationData,
place_id: placeId,
create_accommodation: createAccommodation && { ...createAccommodation, place_id: placeId },
} as never,
cost,
);
return { place: placeRow, ...written };
},
);
if (place) venues.push(place.id);
if (place) this.realtime.broadcast(tripId, 'place:created', { place }, socketId);
this.realtime.broadcast(tripId, 'reservation:created', { reservation }, socketId);
if (accommodationCreated) {
this.realtime.broadcast(tripId, 'accommodation:created', {}, socketId);
}
await this.reservations.announceStayMirror(tripId, stayMirror);
this.reservations.announceCost(tripId, costEvents, socketId);
const { reservation, placeId } = await this.createItem(tripId, item, externalId, socketId, geoCache);
if (placeId !== null) venues.push(placeId);
created.push(reservation);
} catch (err) {
console.error(
`[booking-import] Failed to create reservation "${item.title}":`,
err instanceof Error ? err.message : err,
);
failed.push({ index, reason: failureReason(err, item) });
}
}
// The venues' websites and phones come from the bookings, not from a person: an import, never shared.
if (venues.length > 0) await this.origins.recordImported(venues);
return { created };
return { created, failed };
}
/** 404 when the person cannot reach the trip, 403 when they may not edit its bookings. */
private async requireBookingEdit(tripId: string, userId: number): Promise<void> {
const trip = await this.reservations.verifyTripAccess(tripId, userId);
if (!trip) throw new DomainError(404, 'Trip not found');
const role = (await this.usersRepo.getRole(userId)) ?? 'user';
const isMember = trip.user_id !== userId;
if (!(await this.permissions.checkPermission('reservation_edit', role, trip.user_id, userId, isMember))) {
throw new DomainError(403, 'No permission');
}
}
/** Whether an earlier import already wrote the booking this key names into the trip. */
private async alreadyImported(tripId: string, externalId: string): Promise<boolean> {
const id = await this.reservationsRepo.findIdByExternal(this.rowIdNum(tripId), IMPORT_SOURCE, externalId);
return id !== undefined;
}
/** The place row a venue booking brings, geocoded first so the broadcast carries coordinates. */
private async venuePlace(
venue: BookingImportPreviewItem['_venue'],
): Promise<Parameters<PlacesService['create']>[1] | undefined> {
if (!venue?.name) return undefined;
let lat = venue.lat;
let lng = venue.lng;
if (lat == null) {
const queries = [venue.address ? `${venue.name} ${venue.address}` : null, venue.address ?? null, venue.name];
try {
for (const q of queries) {
if (!q) continue;
const hit = await this.maps.geocodeQuery(q);
if (hit) {
lat = hit.lat;
lng = hit.lng;
break;
}
}
} catch {
// geocoding failure is non-fatal
}
}
return {
name: venue.name,
lat,
lng,
address: venue.address,
// A booking mail gives the venue's site however its sender wrote it;
// it lands as https or not at all (#2483).
website: normalizePlaceWebsite(venue.website) ?? undefined,
phone: venue.phone,
};
}
/**
* The stay a hotel booking brings. start_day_id / end_day_id are resolved from
* the check-in/out dates so the accommodation row is actually inserted
* (createReservation gates on them).
*/
private async accommodationFor(
tripId: string,
item: BookingImportPreviewItem,
): Promise<CreateAccommodation | undefined> {
const stay = item._accommodation;
if (item.type !== 'hotel' || !stay) return undefined;
const startDayId = await this.resolveDayId(tripId, stay.check_in);
const endDayId = await this.resolveDayId(tripId, stay.check_out);
return {
start_day_id: startDayId ?? undefined,
end_day_id: endDayId ?? undefined,
check_in: stay.check_in,
check_out: stay.check_out,
confirmation: stay.confirmation,
};
}
/** One confirmed item written with its venue and cost, and announced once that committed. */
private async createItem(
tripId: string,
item: BookingImportPreviewItem,
externalId: string | null,
socketId: string | undefined,
geoCache: Map<string, { lat: number; lng: number } | null>,
): Promise<{ reservation: Reservation; placeId: number | null }> {
// The origin is the import's to set, never the item's: the confirm body comes from the client.
const { _venue, _accommodation, source: _src, ...fields } = item;
const reservationData = withoutOrigin(fields) as typeof fields;
const venue = await this.venuePlace(_venue);
// The same lookup preview() runs, through the same helper. On anything
// that came from a preview this is a no-op, since the endpoints already
// carry coordinates; it stays because this route can also be called
// with items that never went through one.
if (Array.isArray(reservationData.endpoints)) {
await this.geocodeEndpoints(
reservationData.endpoints,
{ location: reservationData.location, address: _venue?.address },
geoCache,
);
// Persist only coord'd endpoints (reservation_endpoints needs lat/lng);
// ungeocodable ones still appeared in the preview's From→To.
reservationData.endpoints = reservationData.endpoints.filter((ep) => ep.lat != null && ep.lng != null);
}
const createAccommodation = await this.accommodationFor(tripId, item);
const origin = externalId ? { external_source: IMPORT_SOURCE, external_id: externalId, sync_enabled: false } : {};
// The venue, the booking and its linked cost are one write; the booking goes
// through the same service core REST, MCP and the plugin RPC take. The cost's
// rate is frozen first, outside that write, since it can fetch rates over the network.
const cost = await this.linkedCost(tripId, item);
const { place, reservation, accommodationCreated, costEvents, stayMirror } = await this.uow.transactional(
async () => {
const placeRow = venue ? await this.places.create(tripId, venue) : null;
const placeId = placeRow?.id;
const written = await this.reservations.createWithCostInTx(
tripId,
{
...reservationData,
...origin,
place_id: placeId,
create_accommodation: createAccommodation && { ...createAccommodation, place_id: placeId },
} as never,
cost,
);
return { place: placeRow, ...written };
},
);
if (place) this.realtime.broadcast(tripId, 'place:created', { place }, socketId);
this.realtime.broadcast(tripId, 'reservation:created', { reservation }, socketId);
if (accommodationCreated) {
this.realtime.broadcast(tripId, 'accommodation:created', {}, socketId);
}
await this.reservations.announceStayMirror(tripId, stayMirror);
this.reservations.announceCost(tripId, costEvents, socketId);
return { reservation, placeId: place ? place.id : null };
}
}
/**
* Why an item's write failed. Two imports of one booking at once both pass the
* duplicate read, and the unique index refuses the second: that is a
* duplicate too, not a failure worth a log line.
*/
function failureReason(err: unknown, item: BookingImportPreviewItem): BookingImportFailureReason {
if (err instanceof UniqueConstraintViolationException) return 'duplicate';
console.error(
`[booking-import] Failed to create reservation "${item.title}":`,
err instanceof Error ? err.message : err,
);
return 'write_failed';
}
@@ -0,0 +1,35 @@
import type { BookingImportPreviewItem } from '@trek/shared';
/** The `external_source` of a booking written by the file import. */
export const IMPORT_SOURCE = 'import';
/** `external_id` is free text, but a key past this length is a document that is not a booking reference. */
const MAX_EXTERNAL_ID = 200;
/** A booking reference as an operator prints it, without the spacing and case a mail adds. */
function normalizeReference(value: string | null | undefined): string {
return (value ?? '').replace(/\s+/g, '').toUpperCase();
}
/** An endpoint as the key names it: its code where it has one, else its name. */
function endpointKey(endpoint: { code?: string | null; name?: string | null } | undefined): string {
if (!endpoint) return '';
return (endpoint.code || endpoint.name || '').trim().toLowerCase();
}
/**
* The key one booking of a reference is stored under, or null without a
* reference. One reference covers several bookings (an outbound and a return
* flight, two hotels of one trip), so the key adds what tells them apart: the
* type, the day it starts (the check-in for a stay) and, for a transport, where
* it runs from and to. The same booking imported twice gives the same key, and
* `idx_reservations_external` holds one booking per key and trip.
*/
export function importExternalId(item: BookingImportPreviewItem): string | null {
const reference = normalizeReference(item.confirmation_number);
if (!reference) return null;
const start = (item.reservation_time ?? item._accommodation?.check_in ?? '').slice(0, 10);
const endpoints = item.endpoints ?? [];
const route = endpoints.length > 0 ? `${endpointKey(endpoints[0])}-${endpointKey(endpoints.at(-1))}` : '';
return [reference, item.type, start, route].join('|').slice(0, MAX_EXTERNAL_ID);
}
@@ -224,6 +224,6 @@ export class ReservationImportController {
// The DTO asserts an array of objects and stops there, so the per-item shape
// is still the service's to interpret — as it was before the contract landed.
return this.bookingImport.confirm(tripId, items as BookingImportPreviewItem[], socketId);
return this.bookingImport.confirm(tripId, user.id, items as BookingImportPreviewItem[], socketId);
}
}
@@ -94,7 +94,7 @@ export type ReservationRow = Record<string, unknown> & {
travelers?: ReservationTraveler[];
};
interface CreateAccommodation {
export interface CreateAccommodation {
place_id?: number;
start_day_id?: number;
end_day_id?: number;
@@ -250,4 +250,34 @@ describe('Booking import e2e (#2477): a schema-bound provider on the upload rout
expect(res.body.items[0].metadata.priceCurrency).toBe('EUR');
expect(res.body.items[0].source.fileName).toBe('Bestätigung_2.pdf');
});
it('confirm writes a referenced booking once: a second copy, in the same request or a later one, is a duplicate', async () => {
const train = {
type: 'train',
title: 'ICE 123',
confirmation_number: 'ab 12c',
reservation_time: '2026-09-06T10:00',
source: { fileName: 'ticket.pdf', index: 0 },
};
const confirm = (items: unknown[]) =>
request(server)
.post(`/api/trips/${tripId}/reservations/import/booking/confirm`)
.set('Cookie', sessionCookie(1))
.send({ items });
const first = await confirm([train, { ...train, confirmation_number: 'AB12C' }]);
expect(first.status).toBe(201);
expect(first.body.created).toHaveLength(1);
expect(first.body.created[0]).toMatchObject({
title: 'ICE 123',
external_source: 'import',
external_id: 'AB12C|train|2026-09-06|',
sync_enabled: 0,
});
expect(first.body.failed).toEqual([{ index: 1, reason: 'duplicate' }]);
const again = await confirm([train]);
expect(again.status).toBe(201);
expect(again.body).toEqual({ created: [], failed: [{ index: 0, reason: 'duplicate' }] });
});
});
@@ -48,7 +48,12 @@ async function make(opts: Opts = {}) {
items: Array.from({ length: MAX_WRITES_PER_STEP + 2 }, (_, index) => ({ ...ITEM, title: `Leg ${index}` })),
warnings: [],
})),
confirm: vi.fn(async () => ({ created: [{ id: 81, title: 'ICE 123', type: 'train', day_id: 31 }] })),
confirm: vi.fn(
async (): Promise<{ created: unknown[]; failed: { index: number; reason: string }[] }> => ({
created: [{ id: 81, title: 'ICE 123', type: 'train', day_id: 31 }],
failed: [],
}),
),
};
const trips = { findAccessible: vi.fn(async () => (opts.trip === false ? undefined : { id: 5, user_id: 7 })) };
const guards = { hasTripPermission: vi.fn(async () => opts.right !== false) };
@@ -126,7 +131,7 @@ describe('AssistantBookingImportService', () => {
const run = await t.service.apply(7, 5, { ...ITEM });
const reservation = { id: 81, title: 'ICE 123', type: 'train', day_id: 31 };
expect(run).toEqual({ ok: true, value: { reservation } });
expect(t.bookingImport.confirm).toHaveBeenCalledWith('5', [ITEM], undefined);
expect(t.bookingImport.confirm).toHaveBeenCalledWith('5', 7, [ITEM], undefined);
expect(t.trips.findAccessible).toHaveBeenCalledWith(5, 7);
expect(t.guards.hasTripPermission).toHaveBeenCalledWith('reservation_edit', 5, 7);
});
@@ -147,8 +152,14 @@ describe('AssistantBookingImportService', () => {
value: 'This booking can no longer be read.',
});
const none = await make();
none.bookingImport.confirm.mockResolvedValueOnce({ created: [] });
none.bookingImport.confirm.mockResolvedValueOnce({ created: [], failed: [{ index: 0, reason: 'write_failed' }] });
const notAdded = await none.service.apply(7, 5, { ...ITEM });
expect(notAdded).toEqual({ ok: false, value: 'The booking could not be added.' });
// A booking an earlier import already wrote is named as such, not as a failure.
none.bookingImport.confirm.mockResolvedValueOnce({ created: [], failed: [{ index: 0, reason: 'duplicate' }] });
expect(await none.service.apply(7, 5, { ...ITEM })).toEqual({
ok: false,
value: 'This booking is already in the trip.',
});
});
});
@@ -15,7 +15,15 @@ vi.mock('../../../../src/db/database', () => ({
isOwner: () => false,
getPlaceWithTags: () => null,
}));
const permissionsStub = { checkPermission: vi.fn(() => true) };
const permissionsStub = { checkPermission: vi.fn(async () => true) };
// confirm() checks the trip and the person's right itself: the trip is reachable, the role a plain user's.
const usersStub = { getRole: vi.fn(async () => 'user') };
const tripAccess = vi.fn(
async (): Promise<{ id: number; user_id: number } | undefined> => ({ id: 5, user_id: 1 }),
);
// No earlier import holds any key unless a test says so.
const findIdByExternal = vi.fn(async (): Promise<number | undefined> => undefined);
const reservationsRepoStub = { findIdByExternal };
// The origin bookkeeping of the venues a confirm creates; its own suite is place-origins.service.test.ts.
const origins = { recordImported: vi.fn(async () => undefined) };
// Runs the callback in place; the tests that care about the boundary wrap it to record it.
@@ -61,6 +69,7 @@ function make(opts: { kit?: boolean; ai?: boolean; extract?: any; parse?: any })
places as never,
uowStub as never,
origins as never,
usersStub as never,
),
extractor,
llmParse,
@@ -251,6 +260,7 @@ describe('BookingImportService.confirm venue website (#2483)', () => {
})),
announceStayMirror: vi.fn(),
announceCost: vi.fn(),
verifyTripAccess: tripAccess,
};
const places = { create: vi.fn((_tripId: string, _input: { website?: string }) => ({ id: 7 })) };
// No dates on a restaurant, so the day repositories are never reached.
@@ -258,7 +268,7 @@ describe('BookingImportService.confirm venue website (#2483)', () => {
{} as never,
{} as never,
undefined as never,
undefined as never,
reservationsRepoStub as never,
reservations as never,
permissionsStub as never,
undefined as never,
@@ -268,6 +278,7 @@ describe('BookingImportService.confirm venue website (#2483)', () => {
places as never,
uowStub as never,
origins as never,
usersStub as never,
);
const item = (website?: string) => ({
type: 'restaurant',
@@ -278,6 +289,7 @@ describe('BookingImportService.confirm venue website (#2483)', () => {
const res = await svc.confirm(
'5',
1,
[item('www.creperie.example/carte'), item('javascript:alert(1)'), item()],
undefined,
);
@@ -317,6 +329,7 @@ describe('BookingImportService.confirm writes the booking with its cost', () =>
announceCost: vi.fn(() => {
order.push('announceCost');
}),
verifyTripAccess: tripAccess,
};
const places = {
create: vi.fn(async () => {
@@ -352,7 +365,7 @@ describe('BookingImportService.confirm writes the booking with its cost', () =>
{} as never,
{} as never,
undefined as never,
undefined as never,
reservationsRepoStub as never,
reservations as never,
permissionsStub as never,
budget as never,
@@ -362,6 +375,7 @@ describe('BookingImportService.confirm writes the booking with its cost', () =>
places as never,
uow as never,
origins as never,
usersStub as never,
);
return { svc, reservations, budget, places, realtime, order, costEvents };
}
@@ -375,7 +389,7 @@ describe('BookingImportService.confirm writes the booking with its cost', () =>
it('BOOKING-IMPORT-TX-001: freezes the rate first, writes booking and cost in one call, announces the cost after the booking', async () => {
const { svc, reservations, order, costEvents } = makeConfirm();
const res = await svc.confirm('5', [priced as never], 'sock');
const res = await svc.confirm('5', 1, [priced as never], 'sock');
expect(res.created).toEqual([{ id: 1 }]);
expect(reservations.createWithCostInTx).toHaveBeenCalledWith(
@@ -428,7 +442,7 @@ describe('BookingImportService.confirm writes the booking with its cost', () =>
expect(preview.items).toHaveLength(1);
const { svc, reservations } = makeConfirm({ freeze: async () => {} });
await svc.confirm('5', preview.items as never, undefined);
await svc.confirm('5', 1, preview.items as never, undefined);
expect(reservations.createWithCostInTx).toHaveBeenCalledTimes(1);
expect(reservations.createWithCostInTx).toHaveBeenCalledWith(
@@ -440,12 +454,12 @@ describe('BookingImportService.confirm writes the booking with its cost', () =>
it('BOOKING-IMPORT-TX-002: no cost without the Costs addon or without a price', async () => {
const off = makeConfirm({ budget: false });
await off.svc.confirm('5', [priced as never], undefined);
await off.svc.confirm('5', 1, [priced as never], undefined);
expect(off.budget.freezeForeignRate).not.toHaveBeenCalled();
expect(off.reservations.createWithCostInTx).toHaveBeenCalledWith('5', expect.anything(), undefined);
const free = makeConfirm();
await free.svc.confirm('5', [{ ...priced, metadata: { price: '0' } } as never], undefined);
await free.svc.confirm('5', 1, [{ ...priced, metadata: { price: '0' } } as never], undefined);
expect(free.budget.freezeForeignRate).not.toHaveBeenCalled();
expect(free.reservations.createWithCostInTx).toHaveBeenCalledWith('5', expect.anything(), undefined);
});
@@ -460,7 +474,7 @@ describe('BookingImportService.confirm writes the booking with its cost', () =>
it('BOOKING-IMPORT-TX-004: writes the venue in the booking transaction and announces it after the commit', async () => {
const { svc, places, reservations, order } = makeConfirm({ budget: false });
await svc.confirm('5', [atVenue as never], 'sock');
await svc.confirm('5', 1, [atVenue as never], 'sock');
expect(order).toEqual([
'begin',
@@ -488,7 +502,7 @@ describe('BookingImportService.confirm writes the booking with its cost', () =>
try {
const { svc, realtime, order } = makeConfirm({ budget: false, failWrite: true });
const res = await svc.confirm('5', [atVenue as never], 'sock');
const res = await svc.confirm('5', 1, [atVenue as never], 'sock');
expect(res.created).toEqual([]);
// The place insert ran inside the transaction the failure rolls back, never on its own.
@@ -507,7 +521,7 @@ describe('BookingImportService.confirm writes the booking with its cost', () =>
throw new Error('rates down');
},
});
const res = await svc.confirm('5', [priced as never], undefined);
const res = await svc.confirm('5', 1, [priced as never], undefined);
expect(res.created).toEqual([{ id: 1 }]);
expect(reservations.createWithCostInTx).toHaveBeenCalledWith('5', expect.anything(), undefined);
} finally {
@@ -515,3 +529,130 @@ describe('BookingImportService.confirm writes the booking with its cost', () =>
}
});
});
describe('BookingImportService.confirm reports every item it did not create', () => {
function makeReport(write: () => Promise<unknown>) {
const reservations = {
createWithCostInTx: vi.fn(write),
announceStayMirror: vi.fn(),
announceCost: vi.fn(),
verifyTripAccess: tripAccess,
};
const svc = new BookingImportService(
{} as never,
{} as never,
undefined as never,
reservationsRepoStub as never,
reservations as never,
permissionsStub as never,
undefined as never,
{ isAddonEnabled: async () => false } as never,
{ broadcast: vi.fn() } as never,
{ geocodeQuery: vi.fn() } as never,
{ create: vi.fn() } as never,
uowStub as never,
origins as never,
usersStub as never,
);
return { svc, reservations };
}
const NO_LOCAL_TIME = { timezone: null, local_time: null, local_date: null };
const flight = {
type: 'flight',
title: 'LH 716',
confirmation_number: ' abc 123 ',
reservation_time: '2026-09-01T10:00',
endpoints: [
{ role: 'from', name: 'Frankfurt', code: 'FRA', lat: 50, lng: 8.5, ...NO_LOCAL_TIME },
{ role: 'to', name: 'Tokyo Narita', code: 'NRT', lat: 35.7, lng: 140.4, ...NO_LOCAL_TIME },
],
source: { fileName: 'ticket.pdf', index: 0 },
};
it('BOOKING-IMPORT-REPORT-001: a referenced booking is written with its origin, sync off', async () => {
const { svc, reservations } = makeReport(async () => ({
reservation: { id: 1 },
accommodationCreated: false,
costEvents: [],
stayMirror: {},
}));
const res = await svc.confirm('5', 1, [flight as never], undefined);
expect(res).toEqual({ created: [{ id: 1 }], failed: [] });
expect(findIdByExternal).toHaveBeenCalledWith(5, 'import', 'ABC123|flight|2026-09-01|fra-nrt');
expect(reservations.createWithCostInTx).toHaveBeenCalledWith(
'5',
expect.objectContaining({
external_source: 'import',
external_id: 'ABC123|flight|2026-09-01|fra-nrt',
sync_enabled: false,
}),
undefined,
);
});
it('BOOKING-IMPORT-REPORT-002: the origin an item names itself is ignored, and an item without a reference has none', async () => {
const { svc, reservations } = makeReport(async () => ({
reservation: { id: 2 },
accommodationCreated: false,
costEvents: [],
stayMirror: {},
}));
const posing = { ...flight, confirmation_number: null, external_source: 'airtrail', external_id: '42' };
await svc.confirm('5', 1, [posing as never], undefined);
expect(findIdByExternal).not.toHaveBeenCalled();
const written = (reservations.createWithCostInTx.mock.calls[0] as unknown[])[1] as Record<string, unknown>;
expect(written).not.toHaveProperty('external_source');
expect(written).not.toHaveProperty('external_id');
});
it('BOOKING-IMPORT-REPORT-003: a booking already imported, one the index refuses and one that fails are each named', async () => {
const quiet = vi.spyOn(console, 'error').mockImplementation(() => {});
try {
const { UniqueConstraintViolationException } = await import('@mikro-orm/core');
const writes = [
() => Promise.reject(new UniqueConstraintViolationException(new Error('UNIQUE constraint failed'))),
() => Promise.reject(new Error('disk full')),
];
const { svc } = makeReport(async () => writes.shift()!());
findIdByExternal.mockResolvedValueOnce(9);
const res = await svc.confirm(
'5',
1,
[
flight,
{ ...flight, reservation_time: '2026-09-02T10:00' },
{ ...flight, confirmation_number: null },
] as never,
undefined,
);
expect(res).toEqual({
created: [],
failed: [
{ index: 0, reason: 'duplicate' },
{ index: 1, reason: 'duplicate' },
{ index: 2, reason: 'write_failed' },
],
});
} finally {
quiet.mockRestore();
}
});
it('BOOKING-IMPORT-REPORT-004: refuses a trip the person cannot reach, and one whose bookings they may not edit', async () => {
const { svc, reservations } = makeReport(async () => ({}));
tripAccess.mockResolvedValueOnce(undefined);
await expect(svc.confirm('5', 1, [flight as never], undefined)).rejects.toMatchObject({ status: 404 });
permissionsStub.checkPermission.mockResolvedValueOnce(false);
await expect(svc.confirm('5', 2, [flight as never], undefined)).rejects.toMatchObject({ status: 403 });
// A member (not the owner) is checked as one, with the role read for them.
expect(permissionsStub.checkPermission).toHaveBeenLastCalledWith('reservation_edit', 'user', 1, 2, true);
expect(reservations.createWithCostInTx).not.toHaveBeenCalled();
});
});
@@ -0,0 +1,55 @@
import { IMPORT_SOURCE, importExternalId } from '../../../../src/nest/booking-import/booking-origin';
import type { BookingImportPreviewItem } from '@trek/shared';
import { describe, expect, it } from 'vitest';
const item = (fields: Partial<BookingImportPreviewItem>): BookingImportPreviewItem => ({
type: 'flight',
title: 'LH 716',
source: { fileName: 'ticket.pdf', index: 0 },
...fields,
});
const endpoint = (role: string, name: string, code: string | null) => ({
role,
name,
code,
lat: 0,
lng: 0,
timezone: null,
local_time: null,
local_date: null,
});
describe('importExternalId', () => {
it('BOOKING-ORIGIN-001: no reference, no key', () => {
expect(importExternalId(item({}))).toBeNull();
expect(importExternalId(item({ confirmation_number: ' ' }))).toBeNull();
expect(IMPORT_SOURCE).toBe('import');
});
it('BOOKING-ORIGIN-002: the reference is read without spacing and case, the route by code before name', () => {
const outbound = item({
confirmation_number: ' abc 123',
reservation_time: '2026-09-01T10:00',
endpoints: [endpoint('from', 'Frankfurt', 'FRA'), endpoint('to', 'Tokyo Narita', null)],
});
expect(importExternalId(outbound)).toBe('ABC123|flight|2026-09-01|fra-tokyo narita');
});
it('BOOKING-ORIGIN-003: the legs of one reference get keys of their own', () => {
const there = item({ confirmation_number: 'ABC123', reservation_time: '2026-09-01T10:00' });
const back = item({ confirmation_number: 'ABC123', reservation_time: '2026-09-15T10:00' });
expect(importExternalId(there)).not.toBe(importExternalId(back));
});
it('BOOKING-ORIGIN-004: a stay is keyed by its check-in, and an overlong key is cut', () => {
const stay = item({
type: 'hotel',
confirmation_number: 'H-77',
_accommodation: { check_in: '2026-09-02T15:00', check_out: '2026-09-04T11:00' },
});
expect(importExternalId(stay)).toBe('H-77|hotel|2026-09-02|');
expect(importExternalId(item({ confirmation_number: 'X'.repeat(500) }))).toHaveLength(200);
});
});
@@ -1,4 +1,5 @@
import {
bookingImportConfirmResponseSchema,
reservationCreateRequestSchema,
reservationUpdateRequestSchema,
reservationPositionsRequestSchema,
@@ -153,3 +154,22 @@ describe('transportLegInputSchema', () => {
expect(transportLegsInputSchema.safeParse([{ dep_day_id: -1 }]).success).toBe(false);
});
});
describe('bookingImportConfirmResponseSchema', () => {
it('names every item that was not created by its index and a known reason', () => {
expect(
bookingImportConfirmResponseSchema.safeParse({
created: [],
failed: [
{ index: 0, reason: 'duplicate' },
{ index: 2, reason: 'write_failed' },
],
}).success,
).toBe(true);
const failed = (index: number, reason: string) =>
bookingImportConfirmResponseSchema.safeParse({ created: [], failed: [{ index, reason }] }).success;
expect(failed(-1, 'duplicate')).toBe(false);
expect(failed(0, 'other')).toBe(false);
expect(bookingImportConfirmResponseSchema.safeParse({ created: [] }).success).toBe(false);
});
});
@@ -402,8 +402,17 @@ export const bookingImportConfirmRequestSchema = z.object({
});
export type BookingImportConfirmRequest = z.infer<typeof bookingImportConfirmRequestSchema>;
/**
* Why one confirmed item was not created: the trip already holds the booking
* an earlier import wrote under the same reference, or writing it failed.
*/
export const bookingImportFailureReasonSchema = z.enum(['duplicate', 'write_failed']);
export type BookingImportFailureReason = z.infer<typeof bookingImportFailureReasonSchema>;
export const bookingImportConfirmResponseSchema = z.object({
created: z.array(reservationSchema),
/** The items that were not created, by their position in the request, each with its reason. */
failed: z.array(z.object({ index: z.number().int().nonnegative(), reason: bookingImportFailureReasonSchema })),
});
export type BookingImportConfirmResponse = z.infer<typeof bookingImportConfirmResponseSchema>;