diff --git a/server/src/nest/booking-import/booking-import.service.ts b/server/src/nest/booking-import/booking-import.service.ts index a17cc007d..8220087db 100644 --- a/server/src/nest/booking-import/booking-import.service.ts +++ b/server/src/nest/booking-import/booking-import.service.ts @@ -17,7 +17,7 @@ import { PlacesService } from '../places/places.service'; import { RealtimeService } from '../realtime/realtime.service'; import { withoutOrigin } from '../reservations/reservation-origin.contract'; import { ReservationsService, type CreateAccommodation } from '../reservations/reservations.service'; -import { IMPORT_SOURCE, importExternalId } from './booking-origin'; +import { IMPORT_SOURCE, IMPORTED_SOURCES, importExternalId } from './booking-origin'; import { KitineraryExtractorService } from './kitinerary-extractor.service'; import { mapReservations } from './kitinerary-mapper'; import type { ParsedBookingItem, KiReservation } from './kitinerary.types'; @@ -406,10 +406,13 @@ export class BookingImportService { } } - /** Whether an earlier import already wrote the booking this key names into the trip. */ + /** Whether an earlier import, from a file or a forwarded mail, already wrote the booking this key names into the trip. */ private async alreadyImported(tripId: string, externalId: string): Promise { - const id = await this.reservationsRepo.findIdByExternal(this.rowIdNum(tripId), IMPORT_SOURCE, externalId); - return id !== undefined; + const trip = this.rowIdNum(tripId); + const ids = await Promise.all( + IMPORTED_SOURCES.map((source) => this.reservationsRepo.findIdByExternal(trip, source, externalId)), + ); + return ids.some((id) => id !== undefined); } /** The place row a venue booking brings, geocoded first so the broadcast carries coordinates. */ diff --git a/server/src/nest/booking-import/booking-origin.ts b/server/src/nest/booking-import/booking-origin.ts index 46cbe3f5b..f55119875 100644 --- a/server/src/nest/booking-import/booking-origin.ts +++ b/server/src/nest/booking-import/booking-origin.ts @@ -3,6 +3,9 @@ import type { BookingImportPreviewItem } from '@trek/shared'; /** The `external_source` of a booking written by the file import. */ export const IMPORT_SOURCE = 'import'; +/** The sources whose keys say a booking is in a trip already: the file import's and the mail import's (`mail`). */ +export const IMPORTED_SOURCES: readonly string[] = [IMPORT_SOURCE, 'mail']; + /** `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; diff --git a/server/tests/unit/nest/booking-import/booking-import.service.test.ts b/server/tests/unit/nest/booking-import/booking-import.service.test.ts index 1a025c716..db3a205b5 100644 --- a/server/tests/unit/nest/booking-import/booking-import.service.test.ts +++ b/server/tests/unit/nest/booking-import/booking-import.service.test.ts @@ -675,6 +675,17 @@ describe('BookingImportService.confirm reports every item it did not create', () expect(guardsStub.hasTripPermission).toHaveBeenLastCalledWith('reservation_edit', '5', 2); expect(reservations.createWithCostInTx).not.toHaveBeenCalled(); }); + + it('BOOKING-IMPORT-REPORT-005: a booking a forwarded mail already wrote into the trip is a duplicate too', async () => { + const { svc, reservations } = makeReport(async () => ({})); + findIdByExternal.mockResolvedValueOnce(undefined).mockResolvedValueOnce(4); + + const res = await svc.confirm('5', 1, [flight as never], undefined); + + expect(res).toEqual({ created: [], failed: [{ index: 0, reason: 'duplicate' }] }); + expect(findIdByExternal).toHaveBeenCalledWith(5, 'mail', 'ABC123|flight|2026-09-01|fra-nrt'); + expect(reservations.createWithCostInTx).not.toHaveBeenCalled(); + }); }); describe('BookingImportService: reading a forwarded mail', () => {