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.

wip validatePerformanceBounty

Kiran K29c32cd2ff13
diff --git a/apps/web/lib/api/bounties/validate-performance-bounty.ts b/apps/web/lib/api/bounties/validate-performance-bounty.ts
new file mode 100644
index 0000000000..8346033fce
--- /dev/null
+++ b/apps/web/lib/api/bounties/validate-performance-bounty.ts
@@ -0,0 +1,174 @@
+import {
+ OPERATOR_FUNCTIONS,
+ workflowConditionSchema,
+} from "@/lib/zod/schemas/workflows";
+import { prisma } from "@dub/prisma";
+import { WorkflowTrigger } from "@dub/prisma/client";
+import { z } from "zod";
+
+export const validatePerformanceBounty = async ({
+ programId,
+ partnerId,
+ workflowTrigger,
+}: {
+ programId: string;
+ partnerId: string;
+ workflowTrigger: WorkflowTrigger;
+}) => {
+ const now = new Date();
+
+ // Find all active bounties for the program
+ const bounties = await prisma.bounty.findMany({
+ where: {
+ programId,
+ workflow: {
+ trigger: workflowTrigger,
+ },
+ startsAt: {
+ lte: now,
+ },
+ OR: [
+ {
+ endsAt: {
+ gte: now,
+ },
+ },
+ {
+ endsAt: null,
+ },
+ ],
+ },
+ include: {
+ groups: true,
+ workflow: true,
+ submissions: {
+ where: {
+ partnerId,
+ programId,
+ },
+ },
+ },
+ });
+
+ if (bounties.length === 0) {
+ console.log(`Program ${programId} has no active performance bounties.`);
+ }
+
+ // Find the program enrollment for the partner
+ const programEnrollment = await prisma.programEnrollment.findUnique({
+ where: {
+ partnerId_programId: {
+ partnerId,
+ programId,
+ },
+ },
+ select: {
+ groupId: true,
+ totalCommissions: true,
+ links: {
+ select: {
+ leads: true,
+ conversions: true,
+ saleAmount: true,
+ },
+ },
+ },
+ });
+
+ if (!programEnrollment) {
+ console.error(
+ `Partner ${partnerId} is not enrolled in program ${programId}.`,
+ );
+ return;
+ }
+
+ if (!programEnrollment?.groupId) {
+ console.error(
+ `Partner ${partnerId} doesn't belong to any group for program ${programId}.`,
+ );
+ return;
+ }
+
+ // Evaluate each bounty to see if the partner is eligible for it
+ for (const bounty of bounties) {
+ if (!bounty.workflow) {
+ console.error(`Bounty ${bounty.id} has no workflow.`);
+ continue;
+ }
+
+ const workflowConditions = z
+ .array(workflowConditionSchema)
+ .parse(bounty.workflow.triggerConditions);
+
+ if (workflowConditions.length === 0) {
+ console.error(
+ `Bounty ${bounty.id} has no workflow conditions. It seems like workflow configuration is incorrect.`,
+ );
+ continue;
+ }
+
+ console.log(bounty);
+
+ if (bounty.submissions.length > 0) {
+ console.log(
+ `Partner ${partnerId} has an existing submission for bounty ${bounty.id}.`,
+ );
+ continue;
+ }
+
+ if (bounty.groups.length > 0) {
+ const allowedGroupIds = bounty.groups.map(({ id }) => id);
+
+ if (!allowedGroupIds.includes(programEnrollment.groupId)) {
+ console.log(
+ `Partner ${partnerId} is not eligible for bounty ${bounty.id} because they are not in any of the assigned groups.`,
+ );
+ continue;
+ }
+ }
+
+ // Calculate the metrics
+ const totalCommission = programEnrollment.totalCommissions;
+
+ const { totalLeads, totalConversions, totalSaleAmount } =
+ programEnrollment.links.reduce(
+ (acc, link) => {
+ acc.totalLeads += link.leads;
+ acc.totalConversions += link.conversions;
+ acc.totalSaleAmount += link.saleAmount;
+ return acc;
+ },
+ { totalLeads: 0, totalConversions: 0, totalSaleAmount: 0 },
+ );
+
+ const context = {
+ totalCommission,
+ totalLeads,
+ totalConversions,
+ totalSaleAmount,
+ };
+
+ console.log(context);
+
+ // Evaluate the workflow condition
+ // Note: We only support one condition for now
+ const { attribute, operator, value } = workflowConditions[0];
+ const operatorFn = OPERATOR_FUNCTIONS[operator];
+
+ if (!operatorFn) {
+ throw new Error(
+ `Operator ${operator} is not supported in the workflow trigger condition.`,
+ );
+ }
+
+ const isEligible = operatorFn(context[attribute], value);
+
+ if (!isEligible) {
+ console.log(
+ `Partner ${partnerId} does not meet the requirements for bounty ${bounty.id}.`,
+ );
+ }
+
+ // TODO
+ }
+};
diff --git a/apps/web/lib/types.ts b/apps/web/lib/types.ts
index cfe257b426..74fdb3bd0b 100644
--- a/apps/web/lib/types.ts
+++ b/apps/web/lib/types.ts
@@ -540,3 +540,5 @@ export type WorkflowComparisonOperator =
(typeof WORKFLOW_COMPARISON_OPERATORS)[number];
export type WorkflowAction = z.infer<typeof workflowActionSchema>;
+
+export type OperatorFn = (a: number, b: number) => boolean;
diff --git a/apps/web/lib/zod/schemas/bounties.ts b/apps/web/lib/zod/schemas/bounties.ts
index 90688772f6..ee81b70f82 100644
--- a/apps/web/lib/zod/schemas/bounties.ts
+++ b/apps/web/lib/zod/schemas/bounties.ts
@@ -23,7 +23,7 @@ export const createBountySchema = z.object({
description: z
.string()
.trim()
- .max(5000, "Description must be less than 5000 characters")
+ .max(500, "Description must be less than 500 characters")
.nullish(),
type: z.nativeEnum(BountyType),
startsAt: parseDateSchema,
diff --git a/apps/web/lib/zod/schemas/workflows.ts b/apps/web/lib/zod/schemas/workflows.ts
index 5ac30ebfaa..d4ec74f7db 100644
--- a/apps/web/lib/zod/schemas/workflows.ts
+++ b/apps/web/lib/zod/schemas/workflows.ts
@@ -1,4 +1,5 @@
import {
+ OperatorFn,
WorkflowComparisonOperator,
WorkflowConditionAttribute,
} from "@/lib/types";
@@ -34,6 +35,13 @@ export const WORKFLOW_ATTRIBUTE_TRIGGER_MAP: Record<
export const WORKFLOW_COMPARISON_OPERATORS = ["gte"] as const;
+export const OPERATOR_FUNCTIONS: Record<
+ WorkflowComparisonOperator,
+ OperatorFn
+> = {
+ gte: (a, b) => a >= b,
+};
+
export const WORKFLOW_COMPARISON_OPERATOR_LABELS: Record<
WorkflowComparisonOperator,
string
diff --git a/packages/prisma/schema/bounty.prisma b/packages/prisma/schema/bounty.prisma
index a12fb5a763..8bc52fda0a 100644
--- a/packages/prisma/schema/bounty.prisma
+++ b/packages/prisma/schema/bounty.prisma
@@ -27,10 +27,10 @@ model Bounty {
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
- program Program @relation(fields: [programId], references: [id], onDelete: Cascade)
submissions BountySubmission[]
groups BountyGroup[]
- workflow Workflow? @relation(fields: [workflowId], references: [id], onDelete: Cascade)
+ program Program @relation(fields: [programId], references: [id], onDelete: Cascade)
+ workflow Workflow? @relation(fields: [workflowId], references: [id], onDelete: Cascade)
@@index(programId)
@@index(workflowId)
diff --git a/packages/prisma/schema/workflow.prisma b/packages/prisma/schema/workflow.prisma
index 6b6f74ae7c..63ba7c842b 100644
--- a/packages/prisma/schema/workflow.prisma
+++ b/packages/prisma/schema/workflow.prisma
@@ -17,8 +17,8 @@ model Workflow {
name String?
trigger WorkflowTrigger
triggerType WorkflowTriggerType @default(event)
- triggerConditions Json?
- actions Json?
+ triggerConditions Json
+ actions Json
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt