From 4cef885e12b4419a59e0683f0a3d300b62d07db4 Mon Sep 17 00:00:00 2001 From: Maurice Date: Mon, 23 Nov 2026 14:56:19 +0100 Subject: [PATCH] fix(mail-ingest): never queue a mail again for a failure after its commit An error in the announcements, documents or notices after a mail was written sent it back to the queue, which duplicated its entries or marked it failed. --- .../mail-ingest-process.service.ts | 21 +++++++++++++++---- .../mail-ingest/mail-ingest.pipeline.test.ts | 17 +++++++++++++++ 2 files changed, 34 insertions(+), 4 deletions(-) diff --git a/server/src/nest/mail-ingest/mail-ingest-process.service.ts b/server/src/nest/mail-ingest/mail-ingest-process.service.ts index 54dafd1fe..88b49c03b 100644 --- a/server/src/nest/mail-ingest/mail-ingest-process.service.ts +++ b/server/src/nest/mail-ingest/mail-ingest-process.service.ts @@ -142,7 +142,7 @@ export class MailIngestProcessService { } if (kind.kind === 'forwarding_confirmation') { await this.keepForInbox(message, { ...outcome, forwardingCode: kind.code }, 'forwarding_confirmation', now); - await this.notify.bookingsPending(person.id, await this.candidates.countPending(person.id)); + await this.afterRead(message.id, () => this.pendingNotice(person.id)); return; } await this.readBookings(message, raw, mail, person, outcome, now); @@ -171,7 +171,7 @@ export class MailIngestProcessService { return; } await this.keepForInbox(message, done, read.emptyReason, now); - await this.notify.bookingsPending(person.id, await this.candidates.countPending(person.id)); + await this.afterRead(message.id, () => this.pendingNotice(person.id)); return; } const context: CheckContext = { @@ -184,7 +184,20 @@ export class MailIngestProcessService { }; const planned = await this.plan(read.drafts, context, person, prefs); const added = await this.write(message, planned, done, now); - await this.afterCommit(added, planned, mail, person); + await this.afterRead(message.id, () => this.afterCommit(added, planned, mail, person)); + } + + /** What follows a mail's commit fails on its own: the mail is read, so it is never queued again for it. */ + private async afterRead(messageId: number, step: () => Promise): Promise { + try { + await step(); + } catch (err) { + logError(`[mail-ingest] After reading mail ${messageId}: ${err instanceof Error ? err.message : String(err)}`); + } + } + + private async pendingNotice(userId: number): Promise { + await this.notify.bookingsPending(userId, await this.candidates.countPending(userId)); } /** The trips a booking of this mail may go to: the address's trip alone, or the person's editable ones. */ @@ -376,7 +389,7 @@ export class MailIngestProcessService { }); }, Promise.resolve()); if (planned.some((booking) => booking.resolution === 'pending' && booking.prepared === null)) { - await this.notify.bookingsPending(person.id, await this.candidates.countPending(person.id)); + await this.pendingNotice(person.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 98f0dd08e..2ba1a5e02 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 @@ -602,6 +602,23 @@ describe('what a mail names', () => { }); describe('a mail that keeps failing', () => { + it('is not read again when what follows its commit fails', async () => { + const user = await person(); + const trip = await makeTrip(orm(), user.id, { start_date: '2030-03-01', end_date: '2030-03-10' }); + const announce = vi + .spyOn(t.get(MailIngestApplyService), 'announce') + .mockRejectedValueOnce(new Error('connection lost')); + try { + const messageId = await forward(user.id, eml('flight')); + expect(await findRow(orm(), MailIngestMessages, { id: messageId })).toMatchObject({ status: 'processed' }); + expect(await job().tick(new Date(NOW.getTime() + 60 * 60 * 1000))).toBe(0); + expect(await entriesOf(messageId)).toHaveLength(1); + expect(await countRows(orm(), Reservations, { trip: trip.id })).toBe(1); + } finally { + announce.mockRestore(); + } + }); + it('is tried again later and given up after its last try', async () => { const user = await person(); await makeTrip(orm(), user.id, { start_date: '2030-03-01', end_date: '2030-03-10' });