Skip to content

Commit 8c09514

Browse files
authored
Implement active campaign checks for email domain updates and deletions (dubinc#3097)
1 parent a0d43e5 commit 8c09514

2 files changed

Lines changed: 30 additions & 1 deletion

File tree

apps/web/app/(ee)/api/email-domains/[domain]/route.ts

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { CAMPAIGN_ACTIVE_STATUSES } from "@/lib/api/campaigns/constants";
12
import { getEmailDomainOrThrow } from "@/lib/api/domains/get-email-domain-or-throw";
23
import { DubApiError } from "@/lib/api/errors";
34
import { getDefaultProgramIdOrThrow } from "@/lib/api/programs/get-default-program-id-or-throw";
@@ -32,6 +33,28 @@ export const PATCH = withWorkspace(
3233

3334
const domainChanged = slug && slug !== emailDomain.slug;
3435

36+
// Prevent updating verified domains that have active campaigns
37+
if (domainChanged) {
38+
const activeCampaignsCount = await prisma.campaign.count({
39+
where: {
40+
programId,
41+
status: {
42+
in: CAMPAIGN_ACTIVE_STATUSES,
43+
},
44+
from: {
45+
endsWith: `@${emailDomain.slug}`,
46+
},
47+
},
48+
});
49+
50+
if (activeCampaignsCount > 0) {
51+
throw new DubApiError({
52+
code: "bad_request",
53+
message: `There are active campaigns using this email domain. You can not update it until all campaigns are completed or paused.`,
54+
});
55+
}
56+
}
57+
3558
let resendDomainId: string | undefined;
3659

3760
if (domainChanged) {
@@ -141,7 +164,7 @@ export const DELETE = withWorkspace(
141164
where: {
142165
programId,
143166
status: {
144-
in: ["active", "scheduled", "sending"],
167+
in: CAMPAIGN_ACTIVE_STATUSES,
145168
},
146169
from: {
147170
endsWith: `@${emailDomain.slug}`,

apps/web/lib/api/campaigns/constants.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,9 @@ export const CAMPAIGN_READONLY_STATUSES: CampaignStatus[] = [
3434
"sent",
3535
"canceled",
3636
];
37+
38+
export const CAMPAIGN_ACTIVE_STATUSES: CampaignStatus[] = [
39+
"active",
40+
"scheduled",
41+
"sending",
42+
];

0 commit comments

Comments
 (0)