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
@@ -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>;