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 configuration parsing to eliminate redundant checks and improve error handling

Kiran K631da4c34c12
diff --git a/apps/web/app/(ee)/api/cron/workflows/[workflowId]/route.ts b/apps/web/app/(ee)/api/cron/workflows/[workflowId]/route.ts
index 753203cebc..4572176915 100644
--- a/apps/web/app/(ee)/api/cron/workflows/[workflowId]/route.ts
+++ b/apps/web/app/(ee)/api/cron/workflows/[workflowId]/route.ts
@@ -40,10 +40,6 @@ export async function POST(
const workflowConfig = parseWorkflowConfig(workflow);
- if (!workflowConfig) {
- return logAndRespond(`Workflow ${workflowId} is not valid. Skipping...`);
- }
-
if (workflowConfig.action.type === WORKFLOW_ACTION_TYPES.SendCampaign) {
await executeSendCampaignWorkflow({
workflow,
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 9da7deb3fa..dfc191859f 100644
--- a/apps/web/lib/api/workflows/execute-award-bounty-workflow.ts
+++ b/apps/web/lib/api/workflows/execute-award-bounty-workflow.ts
@@ -16,13 +16,7 @@ export const executeAwardBountyWorkflow = async ({
workflow: Workflow;
context: WorkflowContext;
}) => {
- const workflowConfig = parseWorkflowConfig(workflow);
-
- if (!workflowConfig) {
- return;
- }
-
- const { condition, action } = workflowConfig;
+ const { condition, action } = parseWorkflowConfig(workflow);
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 86e26ab0cf..a567683524 100644
--- a/apps/web/lib/api/workflows/execute-send-campaign-workflow.ts
+++ b/apps/web/lib/api/workflows/execute-send-campaign-workflow.ts
@@ -15,13 +15,7 @@ export const executeSendCampaignWorkflow = async ({
workflow: Workflow;
context?: WorkflowContext;
}) => {
- const workflowConfig = parseWorkflowConfig(workflow);
-
- if (!workflowConfig) {
- return;
- }
-
- const { condition, action } = workflowConfig;
+ const { condition, action } = parseWorkflowConfig(workflow);
if (action.type !== WORKFLOW_ACTION_TYPES.SendCampaign) {
return;
@@ -37,6 +31,9 @@ export const executeSendCampaignWorkflow = async ({
where: {
id: campaignId,
},
+ include: {
+ groups: true,
+ },
});
if (!campaign) {
@@ -48,6 +45,11 @@ export const executeSendCampaignWorkflow = async ({
programId,
partnerId,
status: "approved",
+ ...(campaign.groups.length > 0 && {
+ groupId: {
+ in: campaign.groups.map(({ groupId }) => groupId),
+ },
+ }),
...buildEnrollmentWhere(condition),
},
include: {
diff --git a/apps/web/lib/api/workflows/execute-workflows.ts b/apps/web/lib/api/workflows/execute-workflows.ts
index de10bd0e68..a4293c21a5 100644
--- a/apps/web/lib/api/workflows/execute-workflows.ts
+++ b/apps/web/lib/api/workflows/execute-workflows.ts
@@ -70,13 +70,7 @@ export async function executeWorkflows({
// Execute each workflow for the program
for (const workflow of workflows) {
- const workflowConfig = parseWorkflowConfig(workflow);
-
- if (!workflowConfig) {
- continue;
- }
-
- const { action } = workflowConfig;
+ const { action } = parseWorkflowConfig(workflow);
switch (action.type) {
case WORKFLOW_ACTION_TYPES.AwardBounty:
diff --git a/apps/web/lib/api/workflows/parse-workflow-config.ts b/apps/web/lib/api/workflows/parse-workflow-config.ts
index 3eda7aa331..01590a9caa 100644
--- a/apps/web/lib/api/workflows/parse-workflow-config.ts
+++ b/apps/web/lib/api/workflows/parse-workflow-config.ts
@@ -8,27 +8,18 @@ import { z } from "zod";
export function parseWorkflowConfig(
workflow: Pick<Workflow, "id" | "triggerConditions" | "actions">,
) {
- const conditionsResult = z
+ const conditions = z
.array(workflowConditionSchema)
- .safeParse(workflow.triggerConditions);
+ .parse(workflow.triggerConditions);
- if (!conditionsResult.success) {
- return null;
- }
-
- const actionsResult = z
- .array(workflowActionSchema)
- .safeParse(workflow.actions);
+ const actions = z.array(workflowActionSchema).parse(workflow.actions);
- if (!actionsResult.success) {
- return null;
+ if (conditions.length === 0) {
+ throw new Error("No conditions found in workflow.");
}
- const conditions = conditionsResult.data;
- const actions = actionsResult.data;
-
- if (conditions.length === 0 || actions.length === 0) {
- return null;
+ if (actions.length === 0) {
+ throw new Error("No actions found in workflow.");
}
// We only support one trigger and one action for now
diff --git a/apps/web/lib/api/workflows/schedule-workflow.ts b/apps/web/lib/api/workflows/schedule-workflow.ts
index dae058b3ef..c129e02c65 100644
--- a/apps/web/lib/api/workflows/schedule-workflow.ts
+++ b/apps/web/lib/api/workflows/schedule-workflow.ts
@@ -8,11 +8,7 @@ export async function scheduleWorkflow(workflow: Workflow | null) {
return;
}
- const condition = parseWorkflowConfig(workflow)?.condition;
-
- if (!condition) {
- return;
- }
+ const { condition } = parseWorkflowConfig(workflow);
// Skip scheduling if the condition is not based on partnerEnrolledDays,
// or if the required enrolled days is 0 (immediate execution case)