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 commissionsToCreate

Steven Teye6fca91112a6
diff --git a/apps/web/lib/actions/partners/create-manual-commission.ts b/apps/web/lib/actions/partners/create-manual-commission.ts
index 64c9a94878..e082fcad1c 100644
--- a/apps/web/lib/actions/partners/create-manual-commission.ts
+++ b/apps/web/lib/actions/partners/create-manual-commission.ts
@@ -5,7 +5,10 @@ import { createId } from "@/lib/api/create-id";
import { getDefaultProgramIdOrThrow } from "@/lib/api/programs/get-default-program-id-or-throw";
import { getProgramEnrollmentOrThrow } from "@/lib/api/programs/get-program-enrollment-or-throw";
import { executeWorkflows } from "@/lib/api/workflows/execute-workflows";
-import { createPartnerCommission } from "@/lib/partners/create-partner-commission";
+import {
+ createPartnerCommission,
+ CreatePartnerCommissionProps,
+} from "@/lib/partners/create-partner-commission";
import { getCustomerEventsTB } from "@/lib/tinybird/get-customer-events-tb";
import {
recordClickZod,
@@ -131,7 +134,7 @@ export const createManualCommissionAction = authActionClient
const tbEventsToRecord: Promise<any>[] = []; // a list of promises of events to record in Tinybird
const commissionsToTransferEventIds: string[] = []; // eventIds for the commissions to transfer to the new customer-partner pair – we need to nullify them later
- const commissionToCreate: (() => Promise<any>)[] = []; // a list of functions that return promises of commissions to create
+ const commissionsToCreate: CreatePartnerCommissionProps[] = [];
// If we're using existing events, we need to duplicate them under the new customer.id
if (useExistingEvents) {
@@ -206,22 +209,20 @@ export const createManualCommissionAction = authActionClient
if (commissionType === "lead") {
// add the new lead event to the list of commissions to create
- commissionToCreate.push(() =>
- createPartnerCommission({
- event: "lead",
- programId,
- partnerId,
- linkId: link.id,
- customerId: duplicateCustomerId,
- eventId: leadEventData.event_id,
- quantity: 1,
- createdAt: new Date(leadEventData.timestamp),
- user,
- context: {
- customer: { country: customer.country },
- },
- }),
- );
+ commissionsToCreate.push({
+ event: "lead" as const,
+ programId,
+ partnerId,
+ linkId: link.id,
+ customerId: duplicateCustomerId,
+ eventId: leadEventData.event_id,
+ quantity: 1,
+ createdAt: new Date(leadEventData.timestamp),
+ user,
+ context: {
+ customer: { country: customer.country },
+ },
+ });
}
}
@@ -259,28 +260,25 @@ export const createManualCommissionAction = authActionClient
if (commissionType === "sale") {
// add the new sale events to the list of commissions to create
- commissionToCreate.push(
- ...saleEventsData.map(
- (saleEventData) => () =>
- createPartnerCommission({
- event: "sale",
- programId,
- partnerId,
- linkId: link.id,
- customerId: duplicateCustomerId,
- eventId: saleEventData.event_id,
- quantity: 1,
- amount: saleEventData.amount,
- currency: saleEventData.currency,
- invoiceId: saleEventData.invoice_id,
- createdAt: new Date(saleEventData.timestamp),
- user,
- context: {
- customer: { country: customer.country },
- sale: { productId },
- },
- }),
- ),
+ commissionsToCreate.push(
+ ...saleEventsData.map((saleEventData) => ({
+ event: "sale" as const,
+ programId,
+ partnerId,
+ linkId: link.id,
+ customerId: duplicateCustomerId,
+ eventId: saleEventData.event_id,
+ quantity: 1,
+ amount: saleEventData.amount,
+ currency: saleEventData.currency,
+ invoiceId: saleEventData.invoice_id,
+ createdAt: new Date(saleEventData.timestamp),
+ user,
+ context: {
+ customer: { country: customer.country },
+ sale: { productId },
+ },
+ })),
);
}
totalSales = existingSaleEvents.length;
@@ -360,22 +358,20 @@ export const createManualCommissionAction = authActionClient
tbEventsToRecord.push(recordLeadWithTimestamp(leadEventData));
if (commissionType === "lead") {
- commissionToCreate.push(() =>
- createPartnerCommission({
- event: "lead",
- programId,
- partnerId,
- linkId: link.id,
- customerId: customer.id,
- eventId: leadEventData.event_id,
- quantity: 1,
- createdAt: new Date(leadEventData.timestamp),
- user,
- context: {
- customer: { country: customer.country },
- },
- }),
- );
+ commissionsToCreate.push({
+ event: "lead" as const,
+ programId,
+ partnerId,
+ linkId: link.id,
+ customerId: customer.id,
+ eventId: leadEventData.event_id,
+ quantity: 1,
+ createdAt: new Date(leadEventData.timestamp),
+ user,
+ context: {
+ customer: { country: customer.country },
+ },
+ });
}
// Prepare sale event if requested
@@ -396,26 +392,24 @@ export const createManualCommissionAction = authActionClient
tbEventsToRecord.push(recordSaleWithTimestamp(saleEventData));
if (commissionType === "sale") {
- commissionToCreate.push(() =>
- createPartnerCommission({
- event: "sale",
- programId,
- partnerId,
- linkId: link.id,
- customerId: customer.id,
- eventId: saleEventData.event_id,
- quantity: 1,
- amount: saleEventData.amount,
- currency: saleEventData.currency,
- invoiceId: saleEventData.invoice_id,
- createdAt: new Date(saleEventData.timestamp),
- user,
- context: {
- customer: { country: customer.country },
- sale: { productId },
- },
- }),
- );
+ commissionsToCreate.push({
+ event: "sale" as const,
+ programId,
+ partnerId,
+ linkId: link.id,
+ customerId: customer.id,
+ eventId: saleEventData.event_id,
+ quantity: 1,
+ amount: saleEventData.amount,
+ currency: saleEventData.currency,
+ invoiceId: saleEventData.invoice_id,
+ createdAt: new Date(saleEventData.timestamp),
+ user,
+ context: {
+ customer: { country: customer.country },
+ sale: { productId },
+ },
+ });
}
}
}
@@ -482,8 +476,14 @@ export const createManualCommissionAction = authActionClient
updatedRes,
);
+ console.log("Commissions to create: ", commissionsToCreate);
+
// create partner commissions
- await Promise.allSettled(commissionToCreate.map((fn) => fn()));
+ await Promise.allSettled(
+ commissionsToCreate.map((commission) =>
+ createPartnerCommission(commission),
+ ),
+ );
// execute workflows
if (["lead", "sale"].includes(commissionType)) {
diff --git a/apps/web/lib/partners/create-partner-commission.ts b/apps/web/lib/partners/create-partner-commission.ts
index 57ed637257..5b081ff0fa 100644
--- a/apps/web/lib/partners/create-partner-commission.ts
+++ b/apps/web/lib/partners/create-partner-commission.ts
@@ -22,6 +22,24 @@ import { CommissionWebhookSchema } from "../zod/schemas/commissions";
import { aggregatePartnerLinksStats } from "./aggregate-partner-links-stats";
import { determinePartnerReward } from "./determine-partner-reward";
+export type CreatePartnerCommissionProps = {
+ event: CommissionType;
+ partnerId: string;
+ programId: string;
+ linkId?: string;
+ customerId?: string;
+ eventId?: string;
+ invoiceId?: string | null;
+ amount?: number;
+ quantity: number;
+ currency?: string;
+ description?: string | null;
+ createdAt?: Date;
+ user?: Session["user"]; // user who created the manual commission
+ context?: RewardContext;
+ skipWorkflow?: boolean;
+};
+
export const createPartnerCommission = async ({
event,
partnerId,
@@ -38,23 +56,7 @@ export const createPartnerCommission = async ({
user,
context,
skipWorkflow = false,
-}: {
- event: CommissionType;
- partnerId: string;
- programId: string;
- linkId?: string;
- customerId?: string;
- eventId?: string;
- invoiceId?: string | null;
- amount?: number;
- quantity: number;
- currency?: string;
- description?: string | null;
- createdAt?: Date;
- user?: Session["user"]; // user who created the manual commission
- context?: RewardContext;
- skipWorkflow?: boolean;
-}) => {
+}: CreatePartnerCommissionProps) => {
let earnings = 0;
let reward: RewardProps | null = null;
let status: CommissionStatus = "pending";