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.

account for commissions already tracked on Dub

Steven Tey8b96a838edb1
diff --git a/apps/web/lib/rewardful/import-commissions.ts b/apps/web/lib/rewardful/import-commissions.ts
index 3a97155598..1799e0482b 100644
--- a/apps/web/lib/rewardful/import-commissions.ts
+++ b/apps/web/lib/rewardful/import-commissions.ts
@@ -139,6 +139,33 @@ async function createCommission({
return;
}
+ // 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 Rewardful, we use the referral's Stripe customer ID
+ // and check for commissions that were created with the same amount and within a +-1 hour window
+ const chargedAt = new Date(sale.charged_at);
+ const trackedCommission = await prisma.commission.findFirst({
+ where: {
+ programId: program.id,
+ type: "sale",
+ customer: {
+ stripeCustomerId: sale.referral.stripe_customer_id,
+ },
+ amount: sale.sale_amount_cents,
+ createdAt: {
+ gte: new Date(chargedAt.getTime() - 60 * 60 * 1000), // 1 hour before
+ lte: new Date(chargedAt.getTime() + 60 * 60 * 1000), // 1 hour after
+ },
+ },
+ });
+
+ if (trackedCommission) {
+ console.log(
+ `Commission ${trackedCommission.id} was already recorded on Dub, skipping...`,
+ );
+ return;
+ }
+
const customerFound = await prisma.customer.findUnique({
where: {
stripeCustomerId: sale.referral.stripe_customer_id,