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.

Update import-commissions.ts

Kiran K288b1fd2d6fd
diff --git a/apps/web/lib/rewardful/import-commissions.ts b/apps/web/lib/rewardful/import-commissions.ts
index 70f8cd9f33..6d197f907b 100644
--- a/apps/web/lib/rewardful/import-commissions.ts
+++ b/apps/web/lib/rewardful/import-commissions.ts
@@ -3,10 +3,12 @@ import CampaignImported from "@dub/email/templates/campaign-imported";
import { prisma } from "@dub/prisma";
import { nanoid } from "@dub/utils";
import { CommissionStatus, Program } from "@prisma/client";
+import { ZERO_DECIMAL_CURRENCIES } from "../analytics/convert-currency";
import { createId } from "../api/create-id";
import { syncTotalCommissions } from "../api/partners/sync-total-commissions";
import { getLeadEvent } from "../tinybird";
import { recordSaleWithTimestamp } from "../tinybird/record-sale";
+import { redis } from "../upstash";
import { clickEventSchemaTB } from "../zod/schemas/clicks";
import { RewardfulApi } from "./api";
import { MAX_BATCHES, rewardfulImporter } from "./importer";
@@ -25,6 +27,8 @@ export async function importCommissions(payload: RewardfulImportPayload) {
const rewardfulApi = new RewardfulApi({ token });
+ const fxRates = await redis.hgetall<Record<string, string>>("fxRates:usd");
+
let currentPage = page;
let hasMore = true;
let processedBatches = 0;
@@ -45,9 +49,11 @@ export async function importCommissions(payload: RewardfulImportPayload) {
commission,
program,
campaignId,
+ fxRates,
}),
),
);
+
currentPage++;
processedBatches++;
}
@@ -97,10 +103,12 @@ async function createCommission({
commission,
program,
campaignId,
+ fxRates,
}: {
commission: RewardfulCommission;
program: Program;
campaignId: string;
+ fxRates: Record<string, string> | null;
}) {
if (commission.campaign.id !== campaignId) {
console.log(
@@ -135,6 +143,43 @@ async function createCommission({
return;
}
+ // Sale amount
+ let amount = sale.sale_amount_cents;
+ let currency = sale.currency.toUpperCase();
+
+ if (currency !== "USD" && fxRates) {
+ const fxRate = fxRates[currency];
+ const isZeroDecimalCurrency = ZERO_DECIMAL_CURRENCIES.includes(currency);
+
+ if (fxRate) {
+ let convertedAmount = amount / Number(fxRates);
+
+ if (isZeroDecimalCurrency) {
+ convertedAmount = convertedAmount * 100;
+ }
+
+ amount = Math.round(convertedAmount);
+ }
+ }
+
+ let earnings = commission.amount;
+ currency = commission.currency.toUpperCase();
+
+ if (currency !== "USD" && fxRates) {
+ const fxRate = fxRates[currency];
+ const isZeroDecimalCurrency = ZERO_DECIMAL_CURRENCIES.includes(currency);
+
+ if (fxRate) {
+ let convertedEarnings = earnings / Number(fxRates);
+
+ if (isZeroDecimalCurrency) {
+ convertedEarnings = convertedEarnings * 100;
+ }
+
+ earnings = Math.round(convertedEarnings);
+ }
+ }
+
// 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
@@ -223,8 +268,8 @@ async function createCommission({
partnerId: customerFound.link.partnerId,
linkId: customerFound.linkId,
customerId: customerFound.id,
- amount: sale.sale_amount_cents,
- earnings: commission.amount,
+ amount,
+ earnings,
currency: sale.currency.toLowerCase(),
quantity: 1,
status: toDubStatus[commission.state],
@@ -237,7 +282,7 @@ async function createCommission({
...clickData,
event_id: eventId,
event_name: "Invoice paid",
- amount: sale.sale_amount_cents,
+ amount,
customer_id: customerFound.id,
payment_processor: "stripe",
currency: sale.currency.toLowerCase(),