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.

Refactor workflow execution logic to utilize parsed workflow configurations and improve clarity.

Kiran K587eb72c93e2
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 9174670f74..408741b301 100644
--- a/apps/web/app/(ee)/api/workflows/partner-approved/route.ts
+++ b/apps/web/app/(ee)/api/workflows/partner-approved/route.ts
@@ -306,6 +306,23 @@ export const { POST } = serve<Payload>(
message: `Triggered draft bounty submission creation for partner ${partnerId} in program ${programId}.`,
});
});
+
+ // Step 5: Execute Dub workflows using the “partnerEnrolled” trigger.
+ await context.run("execute-workflows", async () => {
+ logger.info({
+ message: "Started executing workflow step 'execute-workflows'.",
+ data: input,
+ });
+
+ // await triggerDraftBountySubmissionCreation({
+ // programId,
+ // partnerIds: [partnerId],
+ // });
+
+ // logger.info({
+ // message: `Triggered draft bounty submission creation for partner ${partnerId} in program ${programId}.`,
+ // });
+ });
},
{
initialPayloadParser: (requestPayload) => {
diff --git a/apps/web/lib/api/workflows/execute-award-bounty-workflow.ts b/apps/web/lib/api/workflows/execute-award-bounty-workflow.ts
index 1e3f830f9c..9da7deb3fa 100644
--- a/apps/web/lib/api/workflows/execute-award-bounty-workflow.ts
+++ b/apps/web/lib/api/workflows/execute-award-bounty-workflow.ts
@@ -1,26 +1,30 @@
import { createPartnerCommission } from "@/lib/partners/create-partner-commission";
-import {
- WorkflowAction,
- WorkflowCondition,
- WorkflowConditionAttribute,
- WorkflowContext,
-} from "@/lib/types";
+import { WorkflowConditionAttribute, WorkflowContext } from "@/lib/types";
+import { WORKFLOW_ACTION_TYPES } from "@/lib/zod/schemas/workflows";
import { sendEmail } from "@dub/email";
import BountyCompleted from "@dub/email/templates/bounty-completed";
import { prisma } from "@dub/prisma";
+import { Workflow } from "@dub/prisma/client";
import { createId } from "../create-id";
import { evaluateWorkflowCondition } from "./execute-workflows";
+import { parseWorkflowConfig } from "./parse-workflow-config";
export const executeAwardBountyWorkflow = async ({
- condition,
+ workflow,
context,
- action,
}: {
- condition: WorkflowCondition;
+ workflow: Workflow;
context: WorkflowContext;
- action: WorkflowAction;
}) => {
- if (action.type !== "awardBounty") {
+ const workflowConfig = parseWorkflowConfig(workflow);
+
+ if (!workflowConfig) {
+ return;
+ }
+
+ const { condition, action } = workflowConfig;
+
+ if (action.type !== WORKFLOW_ACTION_TYPES.AwardBounty) {
return;
}
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 da5bde2c72..a7f37de275 100644
--- a/apps/web/lib/api/workflows/execute-send-campaign-workflow.ts
+++ b/apps/web/lib/api/workflows/execute-send-campaign-workflow.ts
@@ -1,4 +1,4 @@
-import { WorkflowCondition } from "@/lib/types";
+import { WorkflowCondition, WorkflowContext } from "@/lib/types";
import { WORKFLOW_ACTION_TYPES } from "@/lib/zod/schemas/workflows";
import { sendBatchEmail } from "@dub/email";
import { prisma } from "@dub/prisma";
@@ -10,19 +10,19 @@ import { parseWorkflowConfig } from "./parse-workflow-config";
export const executeSendCampaignWorkflow = async ({
workflow,
+ context
}: {
workflow: Workflow;
+ context?: WorkflowContext;
}) => {
const workflowConfig = parseWorkflowConfig(workflow);
- // Workflow config is not valid
if (!workflowConfig) {
return;
}
const { condition, action } = workflowConfig;
- // Action is not a send campaign action
if (action.type !== WORKFLOW_ACTION_TYPES.SendCampaign) {
return;
}
diff --git a/apps/web/lib/api/workflows/execute-workflows.ts b/apps/web/lib/api/workflows/execute-workflows.ts
index 668dd058b8..6fc5a54ff7 100644
--- a/apps/web/lib/api/workflows/execute-workflows.ts
+++ b/apps/web/lib/api/workflows/execute-workflows.ts
@@ -5,13 +5,13 @@ import {
} from "@/lib/types";
import {
OPERATOR_FUNCTIONS,
- workflowActionSchema,
- workflowConditionSchema,
+ WORKFLOW_ACTION_TYPES,
} from "@/lib/zod/schemas/workflows";
import { prisma } from "@dub/prisma";
import { WorkflowTrigger } from "@dub/prisma/client";
-import { z } from "zod";
import { executeAwardBountyWorkflow } from "./execute-award-bounty-workflow";
+import { executeSendCampaignWorkflow } from "./execute-send-campaign-workflow";
+import { parseWorkflowConfig } from "./parse-workflow-config";
export async function executeWorkflows({
trigger,
@@ -70,29 +70,23 @@ export async function executeWorkflows({
// Execute each workflow for the program
for (const workflow of workflows) {
- const conditions = z
- .array(workflowConditionSchema)
- .parse(workflow.triggerConditions);
+ const workflowConfig = parseWorkflowConfig(workflow);
- if (conditions.length === 0) {
+ if (!workflowConfig) {
continue;
}
- const actions = z.array(workflowActionSchema).parse(workflow.actions);
+ const { action } = workflowConfig;
- if (actions.length === 0) {
- continue;
- }
-
- // We only support one trigger and action for now
- const condition = conditions[0];
- const action = actions[0];
-
- if (action.type === "awardBounty") {
+ if (action.type === WORKFLOW_ACTION_TYPES.AwardBounty) {
await executeAwardBountyWorkflow({
- condition,
+ workflow,
+ context: workflowContext,
+ });
+ } else if (action.type === WORKFLOW_ACTION_TYPES.SendCampaign) {
+ await executeSendCampaignWorkflow({
+ workflow,
context: workflowContext,
- action,
});
}
}