From 2180175040c0b6bd3384cfb94a2baba36c8e9235 Mon Sep 17 00:00:00 2001 From: Maurice Date: Mon, 16 Nov 2026 10:45:23 +0100 Subject: [PATCH] fix(mail-ingest): refuse an undo when the booking was moved in between A move keeps the entry's resolution and changes only its trip and created ids, so an undo that read them before a move landed still claimed the entry, removed nothing and left the moved booking behind. The claim is now bound to the trip and ids it was read with. --- .../MailIngestCandidates.repository.ts | 22 ++++++++++--- .../mail-ingest-actions.service.ts | 27 +++++++++------ .../MailIngestCandidates.repository.test.ts | 33 +++++++++++++++++++ .../mail-ingest/mail-ingest.pipeline.test.ts | 28 ++++++++++++++++ 4 files changed, 96 insertions(+), 14 deletions(-) diff --git a/server/src/db/repositories/MailIngestCandidates.repository.ts b/server/src/db/repositories/MailIngestCandidates.repository.ts index 591fc0443..4f3694661 100644 --- a/server/src/db/repositories/MailIngestCandidates.repository.ts +++ b/server/src/db/repositories/MailIngestCandidates.repository.ts @@ -193,9 +193,16 @@ export class MailIngestCandidatesRepository extends TrekRepository { - const result = await this.db() + async resolve( + id: number, + from: readonly string[], + patch: MailIngestResolutionPatch, + held?: { tripId: number; createdIdsJson: string | null }, + ): Promise { + let query = this.db() .updateTable('mail_ingest_candidates') .set({ resolution: patch.resolution, @@ -207,8 +214,15 @@ export class MailIngestCandidatesRepository extends TrekRepository { - const claimed = await this.candidates.resolve(entry.id, ADDED_RESOLUTIONS, { - resolution: 'undone', - reason: null, - resolvedTripId: tripId, - createdIdsJson: entry.created_ids_json, - resolvedBy: userId, - resolvedAt: dbNow(now), - undoUntil: null, - }); - if (!claimed) throw new DomainError(409, 'This booking was undone already'); + // A move keeps the resolution, so the claim also asks for the trip and ids read above: after a move + // in between, removing those would miss the booking in the new trip and leave it behind. + const claimed = await this.candidates.resolve( + entry.id, + ADDED_RESOLUTIONS, + { + resolution: 'undone', + reason: null, + resolvedTripId: tripId, + createdIdsJson: entry.created_ids_json, + resolvedBy: userId, + resolvedAt: dbNow(now), + undoUntil: null, + }, + { tripId, createdIdsJson: entry.created_ids_json }, + ); + if (!claimed) throw new DomainError(409, 'This booking was moved or undone already'); return this.apply.removeInTx(tripId, created); }); await this.apply.announceRemoved(removed, created.file_ids); diff --git a/server/tests/unit/db/repositories/MailIngestCandidates.repository.test.ts b/server/tests/unit/db/repositories/MailIngestCandidates.repository.test.ts index 7ac9d90fe..cec3cc8ca 100644 --- a/server/tests/unit/db/repositories/MailIngestCandidates.repository.test.ts +++ b/server/tests/unit/db/repositories/MailIngestCandidates.repository.test.ts @@ -130,6 +130,39 @@ describe('MailIngestCandidatesRepository: the entries', () => { }); }); + it('decides an added entry only while it still holds the trip and ids it was read with', async () => { + const { user } = await makeUser(t); + const trip = await makeTrip(t, user.id); + const other = await makeTrip(t, user.id); + const mail = await mailOf(user.id); + const id = await entry(mail, user.id); + const added = { + resolution: 'applied', + reason: null, + resolvedTripId: trip.id, + createdIdsJson: '{"reservation_ids":[1]}', + resolvedBy: user.id, + resolvedAt: DAY1, + undoUntil: at('2026-11-08T10:00:00Z'), + }; + expect(await candidates.resolve(id, ['pending'], added)).toBe(true); + const undone = { ...added, resolution: 'undone', undoUntil: null }; + + expect( + await candidates.resolve(id, ['applied'], undone, { tripId: other.id, createdIdsJson: added.createdIdsJson }), + ).toBe(false); + expect(await candidates.resolve(id, ['applied'], undone, { tripId: trip.id, createdIdsJson: '{"x":1}' })).toBe( + false, + ); + expect(await candidates.resolve(id, ['applied'], undone, { tripId: trip.id, createdIdsJson: null })).toBe(false); + expect(await candidates.findById(id)).toMatchObject({ resolution: 'applied' }); + + expect( + await candidates.resolve(id, ['applied'], undone, { tripId: trip.id, createdIdsJson: added.createdIdsJson }), + ).toBe(true); + expect(await candidates.findById(id)).toMatchObject({ resolution: 'undone', resolved_trip_id: trip.id }); + }); + it('writes an entry decided as it is written with its trip, ids and undo window', async () => { const { user } = await makeUser(t); const trip = await makeTrip(t, user.id); diff --git a/server/tests/unit/nest/mail-ingest/mail-ingest.pipeline.test.ts b/server/tests/unit/nest/mail-ingest/mail-ingest.pipeline.test.ts index 2f9b46c5e..bd7f6967d 100644 --- a/server/tests/unit/nest/mail-ingest/mail-ingest.pipeline.test.ts +++ b/server/tests/unit/nest/mail-ingest/mail-ingest.pipeline.test.ts @@ -17,6 +17,7 @@ import { MailReadService } from '../../../../src/nest/llm-parse/mail-read.servic import type { MailReading as AiReading } from '../../../../src/nest/llm-parse/mail-read.types'; import { walkMail } from '../../../../src/nest/llm-parse/mime-walker'; import { MailIngestActionsService } from '../../../../src/nest/mail-ingest/mail-ingest-actions.service'; +import { MailIngestApplyService } from '../../../../src/nest/mail-ingest/mail-ingest-apply.service'; import { MailIngestInboxService } from '../../../../src/nest/mail-ingest/mail-ingest-inbox.service'; import { MailIngestIntakeService } from '../../../../src/nest/mail-ingest/mail-ingest-intake.service'; import { MailIngestProcessJob } from '../../../../src/nest/mail-ingest/mail-ingest-process.job'; @@ -289,6 +290,33 @@ describe('a booking that passes every check', () => { expect(await countRows(orm(), Reservations, { trip: second.id })).toBe(0); }); + it('is not undone when it moves while the undo waits: the booking stays where it went, with its entry', async () => { + const user = await person(); + const first = await makeTrip(orm(), user.id, { start_date: '2030-03-01', end_date: '2030-03-10' }); + const messageId = await forward(user.id, eml('flight')); + const [entry] = await entriesOf(messageId); + const second = await makeTrip(orm(), user.id, { start_date: '2030-02-25', end_date: '2030-03-05' }); + // The move lands between the undo reading the entry and claiming it, while the undo checks for edits. + const edited = vi.spyOn(t.get(MailIngestApplyService), 'editedSince').mockImplementationOnce(async () => { + await actions().move(entry.id, user.id, second.id, true, NOW); + return false; + }); + try { + await expect(actions().undo(entry.id, user.id, false, NOW)).rejects.toThrow( + 'This booking was moved or undone already', + ); + } finally { + edited.mockRestore(); + } + expect(await countRows(orm(), Reservations, { trip: first.id })).toBe(0); + expect(await countRows(orm(), Reservations, { trip: second.id })).toBe(1); + const [after] = await entriesOf(messageId); + expect(after).toMatchObject({ resolution: 'auto_applied', resolved_trip_id: second.id }); + + await actions().undo(entry.id, user.id, false, NOW); + expect(await countRows(orm(), Reservations, { trip: second.id })).toBe(0); + }); + it('asks before moving a booking that was edited since, and refuses a stranger', async () => { const user = await person(); const { user: stranger } = await makeUser(orm());