Skip to content

Commit 7705d31

Browse files
committed
fix(mailer): batch degrades isUnsubscribed errors to per-entry failures
A transient DB error in isUnsubscribed used to abort the whole batch because the call sat outside the per-email try/catch in prepareBatch. Wrap the unsubscribe check inside the same catch so a rejection becomes a per-recipient failure, matching sendEmail's behavior. Lock it in with a regression test.
1 parent 111aae0 commit 7705d31

2 files changed

Lines changed: 18 additions & 4 deletions

File tree

apps/sim/lib/messaging/email/mailer.test.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,5 +251,19 @@ describe('mailer', () => {
251251

252252
expect(isUnsubscribed).not.toHaveBeenCalled()
253253
})
254+
255+
it('should degrade isUnsubscribed rejections to per-entry failures', async () => {
256+
;(isUnsubscribed as Mock).mockRejectedValue(new Error('Database connection failed'))
257+
258+
const result = await sendBatchEmails({
259+
emails: [
260+
{ ...testEmailOptions, to: 'user1@example.com', emailType: 'marketing' as EmailType },
261+
{ ...testEmailOptions, to: 'user2@example.com', emailType: 'marketing' as EmailType },
262+
],
263+
})
264+
265+
expect(result.results).toHaveLength(2)
266+
expect(result.results.every((r) => r.success === false)).toBe(true)
267+
})
254268
})
255269
})

apps/sim/lib/messaging/email/mailer.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -94,11 +94,11 @@ interface PreparedBatchEntry {
9494

9595
async function prepareBatch(emails: EmailOptions[]): Promise<PreparedBatchEntry[]> {
9696
return Promise.all(
97-
emails.map(async (email, index) => {
98-
if (await shouldSkipForUnsubscribe(email)) {
99-
return { index, data: null, skippedResult: SKIPPED_UNSUBSCRIBED_RESULT }
100-
}
97+
emails.map(async (email, index): Promise<PreparedBatchEntry> => {
10198
try {
99+
if (await shouldSkipForUnsubscribe(email)) {
100+
return { index, data: null, skippedResult: SKIPPED_UNSUBSCRIBED_RESULT }
101+
}
102102
return { index, data: processEmailData(email), skippedResult: null }
103103
} catch (error) {
104104
return {

0 commit comments

Comments
 (0)