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 Ladeira1160722892fa
diff --git a/apps/web/lib/rewardful/import-affiliate-coupons.ts b/apps/web/lib/rewardful/import-affiliate-coupons.ts
index 7b7c5a32b7..67c0262a9d 100644
--- a/apps/web/lib/rewardful/import-affiliate-coupons.ts
+++ b/apps/web/lib/rewardful/import-affiliate-coupons.ts
@@ -82,9 +82,24 @@ export async function importAffiliateCoupons(payload: RewardfulImportPayload) {
),
);
- const couponsToImport = filteredCoupons.filter(
- (coupon) => !existingDiscountCodeSet.has(coupon.token.toLowerCase()),
- );
+ // Skip coupons that already have a discount code, AND dedupe repeated tokens
+ // within this batch — two coupons sharing a token can't both own the same
+ // (programId, code), and keeping both would overwrite the link mapping and
+ // leave an orphan link.
+ const seenTokens = new Set<string>();
+ const couponsToImport = filteredCoupons.filter((coupon) => {
+ const normalizedToken = coupon.token.toLowerCase();
+
+ if (
+ existingDiscountCodeSet.has(normalizedToken) ||
+ seenTokens.has(normalizedToken)
+ ) {
+ return false;
+ }
+
+ seenTokens.add(normalizedToken);
+ return true;
+ });
// A coupon's link MUST be owned by the coupon's partner. But affiliate link
// tokens (created in import-partners) and coupon tokens share the same
@@ -169,14 +184,14 @@ export async function importAffiliateCoupons(payload: RewardfulImportPayload) {
: undefined;
if (!link) {
- console.error(`Link not found for coupon ${coupon.token}`);
+ console.error(`Link not found for Rewardful coupon`);
return null;
}
// Safety net: never bind a discount code to a link owned by a different partner
if (link.partnerId !== partnerId) {
console.error(
- `Skipping coupon ${coupon.token}: resolved link ${link.id} is owned by ${link.partnerId}, expected ${partnerId}`,
+ `Skipping Rewardful coupon: resolved link is owned by a different partner`,
);
return null;
}
diff --git a/apps/web/lib/rewardful/import-customers.ts b/apps/web/lib/rewardful/import-customers.ts
index 0e36b8fe82..58de365a0c 100644
--- a/apps/web/lib/rewardful/import-customers.ts
+++ b/apps/web/lib/rewardful/import-customers.ts
@@ -166,7 +166,7 @@ async function createCustomer({
await logImportError({
...commonImportLogInputs,
code: "LINK_NOT_FOUND",
- message: `Link not found for referral ${referralId} (token: ${referral.link?.token || referral.coupon?.token}).`,
+ message: `Link not found for referral ${referralId}.`,
});
return;