diff --git a/src/app/api/donations/dropoff/__tests__/route.test.ts b/src/app/api/donations/dropoff/__tests__/route.test.ts index 8f49025fe..c392ed323 100644 --- a/src/app/api/donations/dropoff/__tests__/route.test.ts +++ b/src/app/api/donations/dropoff/__tests__/route.test.ts @@ -143,4 +143,25 @@ describe('POST /api/donations/dropoff', () => { const res = await POST(makeRequest(VALID_BODY)) expect(res.status).toBe(200) }) + + it('still returns 200 when sendCustomEmail resolves { success: false } — but logs the resolved failure', async () => { + // sendCustomEmail RESOLVES with {success:false} on SMTP failure rather + // than throwing. A bare `.catch()` would miss this — the route uses a + // `.then()` to detect resolved-failure and log it. Without this lock + // a refactor could silently revert to the bare-catch shape (the bug + // I just shipped in a8cc473c and fixed adjacent). + const { logger } = jest.requireMock('@/lib/logger') as { logger: { warn: jest.Mock } } + mockSendCustomEmail.mockResolvedValue({ success: false, error: 'Listmonk 500' }) + + const res = await POST(makeRequest(VALID_BODY)) + expect(res.status).toBe(200) + + // Give the fire-and-forget .then() chain a tick to flush + await new Promise(r => setImmediate(r)) + + const resolvedFailureLogs = logger.warn.mock.calls.filter( + (call: unknown[]) => typeof call[0] === 'string' && call[0].includes('(resolved)'), + ) + expect(resolvedFailureLogs.length).toBeGreaterThanOrEqual(2) + }) }) diff --git a/src/app/api/donations/dropoff/route.ts b/src/app/api/donations/dropoff/route.ts index ab1a820f1..5dfcb969a 100644 --- a/src/app/api/donations/dropoff/route.ts +++ b/src/app/api/donations/dropoff/route.ts @@ -44,15 +44,29 @@ export async function POST(request: NextRequest) { // Both emails fire-and-forget so a transient SMTP failure doesn't // surface as a 500 to a donor who just filled out the form correctly. - sendCustomEmail( - CONTACT.email, - donationDropoffNotification(fields), - ).catch(err => logger.warn('Failed to send donation drop-off notification', { error: err, donorEmail: fields.email })) + // + // sendCustomEmail() resolves `{ success: false, error }` on SMTP / + // Listmonk failure rather than throwing — a bare `.catch()` only + // catches actual rejections, so resolved-failures slip through + // silently. Inspect each result in a `.then()` and log the resolved + // failure mode separately so ops can grep for both shapes. Same + // pattern as src/lib/it-hilfe/notifications.ts and the 11+ prior + // swallow fixes in this codebase. + sendCustomEmail(CONTACT.email, donationDropoffNotification(fields)) + .then(r => { + if (!r.success) { + logger.warn('Failed to send donation drop-off notification (resolved)', { error: r.error, donorEmail: fields.email }) + } + }) + .catch(err => logger.warn('Failed to send donation drop-off notification (rejected)', { error: err, donorEmail: fields.email })) - sendCustomEmail( - fields.email, - donationDropoffConfirmation(fields), - ).catch(err => logger.warn('Failed to send donation drop-off confirmation', { error: err, donorEmail: fields.email })) + sendCustomEmail(fields.email, donationDropoffConfirmation(fields)) + .then(r => { + if (!r.success) { + logger.warn('Failed to send donation drop-off confirmation (resolved)', { error: r.error, donorEmail: fields.email }) + } + }) + .catch(err => logger.warn('Failed to send donation drop-off confirmation (rejected)', { error: err, donorEmail: fields.email })) logger.info('Donation drop-off announced', { donorEmail: fields.email, hasPreferredDate: !!fields.preferredDate })