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.

Partnerstack: Handle manual commissions

Kiran Kfe1010e7045f
diff --git a/apps/web/lib/partnerstack/import-commissions.ts b/apps/web/lib/partnerstack/import-commissions.ts
index 05a1f4cfae..d98e3a0917 100644
--- a/apps/web/lib/partnerstack/import-commissions.ts
+++ b/apps/web/lib/partnerstack/import-commissions.ts
@@ -153,6 +153,83 @@ async function createCommission({
entity_id: commission.key,
} as const;
+ const commissionFound = await prisma.commission.findUnique({
+ where: {
+ invoiceId_programId: {
+ invoiceId: commission.key, // This is not the actual invoice ID, but we use this to deduplicate the commissions
+ programId: program.id,
+ },
+ },
+ });
+
+ if (commissionFound) {
+ console.log(`Commission ${commission.key} already exists, skipping...`);
+ return;
+ }
+
+ // Earnings
+ let earnings = commission.amount;
+ const earningsCurrency = commission.currency;
+
+ if (earningsCurrency.toUpperCase() !== "USD" && fxRates) {
+ const { amount: convertedAmount } = convertCurrencyWithFxRates({
+ currency: earningsCurrency,
+ amount: earnings,
+ fxRates,
+ });
+
+ earnings = convertedAmount;
+ }
+
+ // Handle manual commissions (No customer and transaction)
+ if (!commission.customer && !commission.transaction) {
+ const programEnrollment = await prisma.programEnrollment.findFirst({
+ where: {
+ partner: {
+ email: commission.partnership.email,
+ },
+ },
+ select: {
+ partnerId: true,
+ },
+ });
+
+ // This should never happen, but just in case
+ if (!programEnrollment) {
+ await logImportError({
+ ...commonImportLogInputs,
+ code: "PARTNER_NOT_FOUND",
+ message: `Program enrollment for partner ${commission.partnership.email} not found in Dub.`,
+ });
+
+ return;
+ }
+
+ await prisma.commission.create({
+ data: {
+ id: createId({ prefix: "cm_" }),
+ type: "custom",
+ programId: program.id,
+ partnerId: programEnrollment.partnerId,
+ amount: 0,
+ earnings,
+ quantity: 1,
+ currency: "usd",
+ status: toDubStatus[commission.reward_status],
+ invoiceId: commission.key,
+ createdAt: new Date(commission.created_at),
+ },
+ });
+
+ await syncTotalCommissions({
+ partnerId: programEnrollment.partnerId,
+ programId: program.id,
+ });
+
+ return;
+ }
+
+ // Handle the sale commissions (transactions and customers are required)
if (!commission.transaction) {
await logImportError({
...commonImportLogInputs,
@@ -173,20 +250,6 @@ async function createCommission({
return;
}
- const commissionFound = await prisma.commission.findUnique({
- where: {
- invoiceId_programId: {
- invoiceId: commission.key, // This is not the actual invoice ID, but we use this to deduplicate the commissions
- programId: program.id,
- },
- },
- });
-
- if (commissionFound) {
- console.log(`Commission ${commission.key} already exists, skipping...`);
- return;
- }
-
const customer = customersData.find(
({ email, externalId }) =>
email === commission.customer?.email ||
@@ -217,20 +280,6 @@ async function createCommission({
amount = convertedAmount;
}
- // Earnings
- let earnings = commission.amount;
- const earningsCurrency = commission.currency;
-
- if (earningsCurrency.toUpperCase() !== "USD" && fxRates) {
- const { amount: convertedAmount } = convertCurrencyWithFxRates({
- currency: earningsCurrency,
- amount: earnings,
- fxRates,
- });
-
- earnings = convertedAmount;
- }
-
// here, we also check for commissions that have already been recorded on Dub
// e.g. during the transition period
// since we don't have the Stripe invoiceId from PartnerStack, we use the referral's customer ID
diff --git a/apps/web/lib/partnerstack/schemas.ts b/apps/web/lib/partnerstack/schemas.ts
index 2b4abb8730..aa9c9aece0 100644
--- a/apps/web/lib/partnerstack/schemas.ts
+++ b/apps/web/lib/partnerstack/schemas.ts
@@ -95,4 +95,7 @@ export const partnerStackCommission = z.object({
})
.nullable(),
reward_status: z.enum(["hold", "pending", "approved", "declined", "paid"]),
+ partnership: z.object({
+ email: z.string(),
+ }),
});