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.

Implement performance bounty validation and commission creation logic; update BountySubmission model to allow optional evidenceUrl.

Kiran K51cadc0fe4f4
diff --git a/apps/web/lib/api/bounties/validate-performance-bounty.ts b/apps/web/lib/api/bounties/validate-performance-bounty.ts
index 8346033fce..09b631d0f1 100644
--- a/apps/web/lib/api/bounties/validate-performance-bounty.ts
+++ b/apps/web/lib/api/bounties/validate-performance-bounty.ts
@@ -1,10 +1,12 @@
+import { createPartnerCommission } from "@/lib/partners/create-partner-commission";
import {
OPERATOR_FUNCTIONS,
workflowConditionSchema,
} from "@/lib/zod/schemas/workflows";
import { prisma } from "@dub/prisma";
-import { WorkflowTrigger } from "@dub/prisma/client";
+import { BountySubmissionStatus, WorkflowTrigger } from "@dub/prisma/client";
import { z } from "zod";
+import { createId } from "../create-id";
export const validatePerformanceBounty = async ({
programId,
@@ -117,7 +119,7 @@ export const validatePerformanceBounty = async ({
}
if (bounty.groups.length > 0) {
- const allowedGroupIds = bounty.groups.map(({ id }) => id);
+ const allowedGroupIds = bounty.groups.map(({ groupId }) => groupId);
if (!allowedGroupIds.includes(programEnrollment.groupId)) {
console.log(
@@ -141,15 +143,6 @@ export const validatePerformanceBounty = async ({
{ 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];
@@ -161,14 +154,52 @@ export const validatePerformanceBounty = async ({
);
}
+ const context = {
+ totalCommission,
+ totalLeads,
+ totalConversions,
+ totalSaleAmount,
+ };
+
const isEligible = operatorFn(context[attribute], value);
if (!isEligible) {
console.log(
`Partner ${partnerId} does not meet the requirements for bounty ${bounty.id}.`,
+ {
+ attribute,
+ value,
+ context,
+ },
);
+ return;
}
- // TODO
+ const commission = await createPartnerCommission({
+ event: "custom",
+ partnerId,
+ programId,
+ amount: 0,
+ quantity: 1,
+ description: `Awarded for meeting the requirements for "${bounty.name}" bounty.`,
+ });
+
+ if (!commission) {
+ console.error(
+ `Failed to create commission for partner ${partnerId} in program ${programId} for bounty ${bounty.id}.`,
+ );
+ return;
+ }
+
+ await prisma.bountySubmission.create({
+ data: {
+ id: createId({ prefix: "bounty_submission_" }),
+ programId,
+ partnerId,
+ bountyId: bounty.id,
+ commissionId: commission.id,
+ status: BountySubmissionStatus.approved,
+ },
+ });
}
};
diff --git a/apps/web/lib/api/conversions/track-lead.ts b/apps/web/lib/api/conversions/track-lead.ts
index e2c13897c1..13e4daca4e 100644
--- a/apps/web/lib/api/conversions/track-lead.ts
+++ b/apps/web/lib/api/conversions/track-lead.ts
@@ -15,10 +15,11 @@ import {
trackLeadResponseSchema,
} from "@/lib/zod/schemas/leads";
import { prisma } from "@dub/prisma";
+import { Customer, WorkflowTrigger } from "@dub/prisma/client";
import { nanoid, R2_URL } from "@dub/utils";
-import { Customer } from "@prisma/client";
import { waitUntil } from "@vercel/functions";
import { z } from "zod";
+import { validatePerformanceBounty } from "../bounties/validate-performance-bounty";
type TrackLeadParams = z.input<typeof trackLeadRequestSchema> & {
rawBody: any;
@@ -261,6 +262,12 @@ export const trackLead = async ({
},
},
});
+
+ await validatePerformanceBounty({
+ programId: link.programId,
+ partnerId: link.partnerId,
+ workflowTrigger: WorkflowTrigger.leadRecorded,
+ });
}
if (
diff --git a/packages/prisma/schema/bounty.prisma b/packages/prisma/schema/bounty.prisma
index 8bc52fda0a..7f25345c57 100644
--- a/packages/prisma/schema/bounty.prisma
+++ b/packages/prisma/schema/bounty.prisma
@@ -55,7 +55,7 @@ model BountySubmission {
bountyId String
commissionId String? @unique
userId String?
- evidenceUrl String
+ evidenceUrl String?
description String? @db.Text
status BountySubmissionStatus @default(pending)
rejectionReason String?