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.

code improvements

Pedro Ladeira19ce4447ac9a
diff --git a/apps/web/lib/lemonsqueezy/import-commissions.ts b/apps/web/lib/lemonsqueezy/import-commissions.ts
index e37affaac8..05a5f16e93 100644
--- a/apps/web/lib/lemonsqueezy/import-commissions.ts
+++ b/apps/web/lib/lemonsqueezy/import-commissions.ts
@@ -42,6 +42,10 @@ type SaleEvent = {
metadata: Record<string, unknown>;
};
+// Only renewals/updates — initials are covered by the Order import.
+// Missing/unknown billing_reason is skipped to avoid double-counting.
+const IMPORTABLE_INVOICE_REASONS = new Set(["renewal", "updated"]);
+
const toDubStatus = (status: string): CommissionStatus | null => {
switch (status) {
case "paid":
@@ -242,8 +246,10 @@ async function listInvoiceSaleEvents({
affiliate_id: number;
} =>
Boolean(invoice.affiliate_id) &&
- // Initial invoices are covered by the Order import
- invoice.billing_reason !== "initial",
+ Boolean(
+ invoice.billing_reason &&
+ IMPORTABLE_INVOICE_REASONS.has(invoice.billing_reason),
+ ),
)
.map((invoice) => ({
invoiceId: `ls_invoice_${invoice.id}`,
@@ -461,41 +467,34 @@ async function createCommission({
}
// Prefer LS-provided USD amounts; otherwise convert
- let saleAmount = saleEvent.amountUsd ?? saleEvent.amount;
- if (
- saleEvent.amountUsd == null &&
- saleEvent.currency.toUpperCase() !== "USD" &&
- fxRates
- ) {
- const { amount: convertedAmount } = convertCurrencyWithFxRates({
+ let saleAmount: number | null =
+ saleEvent.amountUsd != null
+ ? saleEvent.amountUsd
+ : saleEvent.currency.toUpperCase() === "USD"
+ ? saleEvent.amount
+ : null;
+
+ if (saleAmount == null && fxRates) {
+ const converted = convertCurrencyWithFxRates({
currency: saleEvent.currency,
- amount: saleAmount,
+ amount: saleEvent.amount,
fxRates,
});
- saleAmount = convertedAmount;
+ saleAmount =
+ converted.currency.toUpperCase() === "USD" ? converted.amount : null;
}
- const createdAt = new Date(saleEvent.createdAt);
- const trackedCommission = await prisma.commission.findFirst({
- where: {
- customerId: existingCustomer.id,
- programId: program.id,
- createdAt: {
- gte: new Date(createdAt.getTime() - 60 * 60 * 1000),
- lte: new Date(createdAt.getTime() + 60 * 60 * 1000),
- },
- type: "sale",
- amount: saleAmount,
- },
- });
-
- if (trackedCommission) {
- console.log(
- `Commission ${saleEvent.invoiceId} with sale amount ${saleAmount} was already recorded on Dub. Skipping...`,
- );
+ if (saleAmount == null) {
+ await logImportError({
+ ...commonImportLogInputs,
+ code: "NOT_SUPPORTED_UNIT",
+ message: `Commission ${saleEvent.invoiceId} skipped: no USD amount and FX rate unavailable for currency ${saleEvent.currency}.`,
+ });
return;
}
+ const createdAt = new Date(saleEvent.createdAt);
+
// LS does not expose per-order commission amounts; derive from Dub sale reward
const earnings = saleReward
? calculateSaleEarnings({
@@ -542,6 +541,10 @@ async function createCommission({
saleAmount > 0 &&
recordSaleWithTimestamp({
...clickData,
+ link_id: partnerLink.id,
+ domain: partnerLink.domain,
+ key: partnerLink.key,
+ url: partnerLink.url,
event_id: eventId,
event_name: "Invoice paid",
amount: saleAmount,
diff --git a/apps/web/lib/lemonsqueezy/import-partners.ts b/apps/web/lib/lemonsqueezy/import-partners.ts
index c3eab9d331..babffde3d8 100644
--- a/apps/web/lib/lemonsqueezy/import-partners.ts
+++ b/apps/web/lib/lemonsqueezy/import-partners.ts
@@ -243,8 +243,33 @@ async function createPartnerAndLinks({
userId,
});
+ // Reject suffixed keys — customers/commissions look up by exact affiliate id
+ if (partnerLink.key !== affiliate.id) {
+ await logImportError({
+ workspace_id: program.workspaceId,
+ import_id: importId,
+ source: "lemonsqueezy",
+ entity: "partner",
+ entity_id: affiliate.id,
+ code: "LINK_NOT_FOUND",
+ message: `Partner link key conflict for affiliate ${affiliate.id}: generated key "${partnerLink.key}" instead of "${affiliate.id}".`,
+ });
+ return;
+ }
+
await createLink(partnerLink);
} catch (error) {
console.error("Error creating partner link", error, affiliate);
+ await logImportError({
+ workspace_id: program.workspaceId,
+ import_id: importId,
+ source: "lemonsqueezy",
+ entity: "partner",
+ entity_id: affiliate.id,
+ code: "LINK_NOT_FOUND",
+ message: `Failed to create partner link for affiliate ${affiliate.id}: ${
+ error instanceof Error ? error.message : String(error)
+ }`,
+ });
}
}
diff --git a/apps/web/lib/lemonsqueezy/schemas.ts b/apps/web/lib/lemonsqueezy/schemas.ts
index 5aba0a5bd0..1b82714458 100644
--- a/apps/web/lib/lemonsqueezy/schemas.ts
+++ b/apps/web/lib/lemonsqueezy/schemas.ts
@@ -69,7 +69,7 @@ export const lemonSqueezyAffiliateSchema = z.object({
store_id: z.number(),
user_id: z.number().nullish(),
user_name: z.string().nullish(),
- user_email: z.string(),
+ user_email: z.string().nullish(),
share_domain: z.string().nullish(),
status: z.string(),
products: z.unknown().nullish(),
@@ -86,7 +86,7 @@ export const lemonSqueezyCustomerSchema = z.object({
id: z.string(),
store_id: z.number(),
name: z.string().nullish(),
- email: z.string(),
+ email: z.string().nullish(),
status: z.string().nullish(),
city: z.string().nullish(),
region: z.string().nullish(),
diff --git a/apps/web/ui/modals/import-lemonsqueezy-modal.tsx b/apps/web/ui/modals/import-lemonsqueezy-modal.tsx
index d0e0166a2f..dfbba0d753 100644
--- a/apps/web/ui/modals/import-lemonsqueezy-modal.tsx
+++ b/apps/web/ui/modals/import-lemonsqueezy-modal.tsx
@@ -71,7 +71,7 @@ function ImportLemonSqueezyModal({
<img
src="https://assets.dub.co/misc/icons/lemonsqueezy.svg"
alt="Lemon Squeezy logo"
- className="h-10 w-10 rounded-full"
+ className="h-10 w-10"
/>
<ArrowRight className="h-5 w-5 text-neutral-600" />
<Logo />