Skip to content

armgit / dub

Public Imported from GitHub
0

The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.

Implement active campaign checks for email domain updates and deletions (#3097)

Kiran K8c095147fb3c
diff --git a/apps/web/app/(ee)/api/email-domains/[domain]/route.ts b/apps/web/app/(ee)/api/email-domains/[domain]/route.ts
index 070dc2ae4d..c91b6ab5c0 100644
--- a/apps/web/app/(ee)/api/email-domains/[domain]/route.ts
+++ b/apps/web/app/(ee)/api/email-domains/[domain]/route.ts
@@ -1,3 +1,4 @@
+import { CAMPAIGN_ACTIVE_STATUSES } from "@/lib/api/campaigns/constants";
import { getEmailDomainOrThrow } from "@/lib/api/domains/get-email-domain-or-throw";
import { DubApiError } from "@/lib/api/errors";
import { getDefaultProgramIdOrThrow } from "@/lib/api/programs/get-default-program-id-or-throw";
@@ -32,6 +33,28 @@ export const PATCH = withWorkspace(
const domainChanged = slug && slug !== emailDomain.slug;
+ // Prevent updating verified domains that have active campaigns
+ if (domainChanged) {
+ const activeCampaignsCount = await prisma.campaign.count({
+ where: {
+ programId,
+ status: {
+ in: CAMPAIGN_ACTIVE_STATUSES,
+ },
+ from: {
+ endsWith: `@${emailDomain.slug}`,
+ },
+ },
+ });
+
+ if (activeCampaignsCount > 0) {
+ throw new DubApiError({
+ code: "bad_request",
+ message: `There are active campaigns using this email domain. You can not update it until all campaigns are completed or paused.`,
+ });
+ }
+ }
+
let resendDomainId: string | undefined;
if (domainChanged) {
@@ -141,7 +164,7 @@ export const DELETE = withWorkspace(
where: {
programId,
status: {
- in: ["active", "scheduled", "sending"],
+ in: CAMPAIGN_ACTIVE_STATUSES,
},
from: {
endsWith: `@${emailDomain.slug}`,
diff --git a/apps/web/lib/api/campaigns/constants.ts b/apps/web/lib/api/campaigns/constants.ts
index 4fe24f865b..9403113305 100644
--- a/apps/web/lib/api/campaigns/constants.ts
+++ b/apps/web/lib/api/campaigns/constants.ts
@@ -34,3 +34,9 @@ export const CAMPAIGN_READONLY_STATUSES: CampaignStatus[] = [
"sent",
"canceled",
];
+
+export const CAMPAIGN_ACTIVE_STATUSES: CampaignStatus[] = [
+ "active",
+ "scheduled",
+ "sending",
+];