Skip to content

Commit ec19f74

Browse files
committed
do not throw error on supression
1 parent c804fa5 commit ec19f74

1 file changed

Lines changed: 37 additions & 12 deletions

File tree

apps/web/src/server/service/email-service.ts

Lines changed: 37 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -74,21 +74,46 @@ export async function sendEmail(
7474

7575
// Check for suppressed emails before sending
7676
const emailsToCheck = Array.isArray(to) ? to : [to];
77-
const suppressedEmails = await Promise.all(
78-
emailsToCheck.map((email) =>
79-
SuppressionService.isEmailSuppressed(email, teamId)
80-
)
81-
);
8277

83-
const suppressedEmailAddresses = emailsToCheck.filter(
84-
(_, index) => suppressedEmails[index]
78+
const filteredToEmails = emailsToCheck.filter(
79+
(email) => !SuppressionService.isEmailSuppressed(email, teamId)
8580
);
8681

87-
if (suppressedEmailAddresses.length > 0) {
88-
throw new UnsendApiError({
89-
code: "BAD_REQUEST",
90-
message: `One or more recipients are suppressed: ${suppressedEmailAddresses.join(", ")}`,
82+
if (filteredToEmails.length === 0) {
83+
logger.info(
84+
{
85+
to,
86+
teamId,
87+
},
88+
"All recipients are suppressed. No emails to send."
89+
);
90+
91+
const email = await db.email.create({
92+
data: {
93+
to: emailsToCheck,
94+
from,
95+
subject: subject as string,
96+
teamId,
97+
domainId: domain.id,
98+
latestStatus: "SUPPRESSED",
99+
apiId: apiKeyId,
100+
text,
101+
html,
102+
inReplyToId,
103+
},
104+
});
105+
106+
await db.emailEvent.create({
107+
data: {
108+
emailId: email.id,
109+
status: "SUPPRESSED",
110+
data: {
111+
error: "All recipients are suppressed. No emails to send.",
112+
},
113+
},
91114
});
115+
116+
return;
92117
}
93118

94119
if (templateId) {
@@ -151,7 +176,7 @@ export async function sendEmail(
151176

152177
const email = await db.email.create({
153178
data: {
154-
to: Array.isArray(to) ? to : [to],
179+
to: filteredToEmails,
155180
from,
156181
subject: subject as string,
157182
replyTo: replyTo

0 commit comments

Comments
 (0)