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.

Add campaign workflow schedules and update cron scheduling logic in route handler

Kiran Kb9e8d1c2af41
diff --git a/apps/web/app/(ee)/api/campaigns/[campaignId]/route.ts b/apps/web/app/(ee)/api/campaigns/[campaignId]/route.ts
index 7fb9549232..12ab20a72c 100644
--- a/apps/web/app/(ee)/api/campaigns/[campaignId]/route.ts
+++ b/apps/web/app/(ee)/api/campaigns/[campaignId]/route.ts
@@ -6,6 +6,7 @@ import { parseWorkflowConfig } from "@/lib/api/workflows/parse-workflow-config";
import { withWorkspace } from "@/lib/auth";
import { qstash } from "@/lib/cron";
import {
+ CAMPAIGN_WORKFLOW_SCHEDULES,
CampaignSchema,
updateCampaignSchema,
} from "@/lib/zod/schemas/campaigns";
@@ -147,10 +148,19 @@ export const PATCH = withWorkspace(
const shouldDeleteSchedule =
campaign.status === "active" && updatedCampaign.status === "paused";
+ const cronSchedule =
+ CAMPAIGN_WORKFLOW_SCHEDULES[updatedCampaign.workflow.trigger];
+
+ if (!cronSchedule) {
+ throw new Error(
+ `Cron schedule not found for trigger ${updatedCampaign.workflow.trigger}`,
+ );
+ }
+
if (shouldSchedule) {
await qstash.schedules.create({
destination: `${APP_DOMAIN_WITH_NGROK}/api/cron/workflows/${updatedCampaign.workflow.id}`,
- cron: "0 */12 * * *", // Every 12 hours
+ cron: cronSchedule,
scheduleId: updatedCampaign.workflow.id,
});
} else if (shouldDeleteSchedule) {
diff --git a/apps/web/lib/zod/schemas/campaigns.ts b/apps/web/lib/zod/schemas/campaigns.ts
index 49aaa9adb0..a4fb946772 100644
--- a/apps/web/lib/zod/schemas/campaigns.ts
+++ b/apps/web/lib/zod/schemas/campaigns.ts
@@ -1,8 +1,12 @@
import {
- WorkflowAttribute,
CampaignWorkflowAttributeConfig,
+ WorkflowAttribute,
} from "@/lib/types";
-import { CampaignStatus, CampaignType } from "@dub/prisma/client";
+import {
+ CampaignStatus,
+ CampaignType,
+ WorkflowTrigger,
+} from "@dub/prisma/client";
import { z } from "zod";
import { GroupSchema } from "./groups";
import { getPaginationQuerySchema } from "./misc";
@@ -53,6 +57,16 @@ export const CAMPAIGN_WORKFLOW_ATTRIBUTE_CONFIG: Record<
},
};
+export const CAMPAIGN_WORKFLOW_SCHEDULES: Partial<
+ Record<WorkflowTrigger, string>
+> = {
+ clickRecorded: "*/5 * * * *", // every 5 minutes
+ leadRecorded: "*/5 * * * *", // every 5 minutes
+ saleRecorded: "*/5 * * * *", // every 5 minutes
+ commissionEarned: "*/5 * * * *", // every 5 minutes
+ partnerEnrolled: "0 */12 * * *", // every 12 hours
+};
+
export const CampaignSchema = z.object({
id: z.string(),
name: z.string(),