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.

Integrate executeWorkflows function for partner enrollment trigger and enhance campaign workflow logic

Kiran Kb21df8dd7bc4
diff --git a/apps/web/app/(ee)/api/workflows/partner-approved/route.ts b/apps/web/app/(ee)/api/workflows/partner-approved/route.ts
index 408741b301..c63dbc775a 100644
--- a/apps/web/app/(ee)/api/workflows/partner-approved/route.ts
+++ b/apps/web/app/(ee)/api/workflows/partner-approved/route.ts
@@ -2,6 +2,7 @@ import { triggerDraftBountySubmissionCreation } from "@/lib/api/bounties/trigger
import { getGroupOrThrow } from "@/lib/api/groups/get-group-or-throw";
import { createPartnerDefaultLinks } from "@/lib/api/partners/create-partner-default-links";
import { getProgramEnrollmentOrThrow } from "@/lib/api/programs/get-program-enrollment-or-throw";
+import { executeWorkflows } from "@/lib/api/workflows/execute-workflows";
import { createWorkflowLogger } from "@/lib/cron/qstash-workflow-logger";
import { PlanProps, RewardProps } from "@/lib/types";
import { sendWorkspaceWebhook } from "@/lib/webhook/publish";
@@ -310,18 +311,18 @@ export const { POST } = serve<Payload>(
// Step 5: Execute Dub workflows using the “partnerEnrolled” trigger.
await context.run("execute-workflows", async () => {
logger.info({
- message: "Started executing workflow step 'execute-workflows'.",
+ message:
+ "Started executing workflow step 'execute-workflows' for the trigger 'partnerEnrolled'.",
data: input,
});
- // await triggerDraftBountySubmissionCreation({
- // programId,
- // partnerIds: [partnerId],
- // });
-
- // logger.info({
- // message: `Triggered draft bounty submission creation for partner ${partnerId} in program ${programId}.`,
- // });
+ await executeWorkflows({
+ trigger: "partnerEnrolled",
+ context: {
+ programId,
+ partnerId,
+ },
+ });
});
},
{
diff --git a/apps/web/lib/api/workflows/execute-send-campaign-workflow.ts b/apps/web/lib/api/workflows/execute-send-campaign-workflow.ts
index a7f37de275..86e26ab0cf 100644
--- a/apps/web/lib/api/workflows/execute-send-campaign-workflow.ts
+++ b/apps/web/lib/api/workflows/execute-send-campaign-workflow.ts
@@ -10,7 +10,7 @@ import { parseWorkflowConfig } from "./parse-workflow-config";
export const executeSendCampaignWorkflow = async ({
workflow,
- context
+ context,
}: {
workflow: Workflow;
context?: WorkflowContext;
@@ -27,15 +27,28 @@ export const executeSendCampaignWorkflow = async ({
return;
}
- const where = buildEnrollmentWhere({
+ const { campaignId } = action.data;
+ const { programId, partnerId } = context || {
programId: workflow.programId,
- condition,
+ partnerId: undefined,
+ };
+
+ const campaign = await prisma.campaign.findUnique({
+ where: {
+ id: campaignId,
+ },
});
+ if (!campaign) {
+ return;
+ }
+
const programEnrollments = await prisma.programEnrollment.findMany({
where: {
- ...where,
+ programId,
+ partnerId,
status: "approved",
+ ...buildEnrollmentWhere(condition),
},
include: {
partner: {
@@ -50,21 +63,10 @@ export const executeSendCampaignWorkflow = async ({
},
});
- // No program enrollments found matching the condition
if (programEnrollments.length === 0) {
return;
}
- const campaign = await prisma.campaign.findUnique({
- where: {
- id: action.data.campaignId,
- },
- });
-
- if (!campaign) {
- return;
- }
-
const programEnrollmentsChunks = chunk(programEnrollments, 100);
for (const programEnrollmentChunk of programEnrollmentsChunks) {
@@ -134,19 +136,12 @@ export const executeSendCampaignWorkflow = async ({
}
};
-function buildEnrollmentWhere({
- programId,
- condition,
-}: {
- programId: string;
- condition: WorkflowCondition;
-}) {
+function buildEnrollmentWhere(condition: WorkflowCondition) {
const thresholdDate = subDays(new Date(), condition.value);
switch (condition.operator) {
case "gte":
return {
- programId,
createdAt: {
lte: thresholdDate,
},