fix(airtrail): import a flight with its AirTrail link in one transaction

The import created the booking and wrote its external_* link as two commits.
A failed link left the booking in the trip while the import reported the
flight as skipped. The flights are fetched before the loop, so nothing between
the two writes waits on the network: each flight or joined connection now goes
in with its link in one transaction, and the method is marked as one
transaction per imported booking instead of being held in the lint:tx baseline.
This commit is contained in:
Maurice
2026-10-08 15:55:57 +02:00
parent 8018f10273
commit 60a42cfac1
4 changed files with 59 additions and 5 deletions
@@ -519,6 +519,7 @@ export async function createMcpTestRegistry(): Promise<McpRegistry> {
new ReservationImportMcp(new AirtrailImportService(
await createTestReservationsRepo(db), await createTestReservationEndpointsRepo(db), await createTestDaysRepo(db),
realtimeService, reservationsService, new AirtrailClient(), new AirtrailService(usersRepo, new AuditService(auditLogRepo, usersRepo), new AirtrailClient()),
await createTestUnitOfWork(db),
), await createTestTripsRepo(db), authService, guards, addonsService),
new SettingsMcp(new SettingsService(await createTestUnitOfWork(db), appSettings, await createTestSettingsRepo(db)), authService),
new HelpMcp(), new AddonsMcp(addonsService),
@@ -57,6 +57,7 @@ async function makeImportService(): Promise<AirtrailImportService> {
{
getAirtrailCredentials: () => ({ baseUrl: 'https://at.example', apiKey: 'k', allowInsecureTls: false }),
} as unknown as AirtrailService,
await createTestUnitOfWork(db),
);
}
@@ -262,3 +263,39 @@ describe('importAirtrailFlights connection joining (#1535)', () => {
expect(JSON.parse(r.metadata).airtrail_ids).toBeUndefined();
});
});
describe('importAirtrailFlights writes a booking with its link', () => {
it('rolls the booking back when the single-flight link fails, so a flight reported skipped is not in the trip', async () => {
listFlights.mockResolvedValue([legBruHel()]);
const quiet = vi.spyOn(console, 'error').mockImplementation(() => {});
const repo = await createTestReservationsRepo(db);
const spy = vi.spyOn(repo, 'linkAirtrailSingleFlight').mockRejectedValueOnce(new Error('boom'));
try {
const result = await importAirtrailFlights(tripId, userId, ['101'], undefined);
expect(result.imported).toEqual([]);
expect(result.skipped).toEqual([{ flightId: '101', reason: 'invalid', detail: 'boom' }]);
} finally {
spy.mockRestore();
quiet.mockRestore();
}
expect(tripReservations(tripId)).toEqual([]);
expect(broadcast).not.toHaveBeenCalled();
});
it('rolls a joined connection back when its link fails', async () => {
listFlights.mockResolvedValue([legBruHel(), legHelJfk()]);
const quiet = vi.spyOn(console, 'error').mockImplementation(() => {});
const repo = await createTestReservationsRepo(db);
const spy = vi.spyOn(repo, 'linkAirtrailMultiLeg').mockRejectedValueOnce(new Error('boom'));
try {
const result = await importAirtrailFlights(tripId, userId, ['101', '102'], undefined, [['101', '102']]);
expect(result.imported).toEqual([]);
expect(result.skipped.map(s => [s.flightId, s.reason])).toEqual([['101', 'invalid'], ['102', 'invalid']]);
} finally {
spy.mockRestore();
quiet.mockRestore();
}
expect(tripReservations(tripId)).toEqual([]);
expect(broadcast).not.toHaveBeenCalled();
});
});