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.

Fix the Rewardful import

Kiran Kf7ac61df55d4
diff --git a/apps/web/lib/rewardful/import-affiliate-coupons.ts b/apps/web/lib/rewardful/import-affiliate-coupons.ts
index 59e05bffe6..2930b9a595 100644
--- a/apps/web/lib/rewardful/import-affiliate-coupons.ts
+++ b/apps/web/lib/rewardful/import-affiliate-coupons.ts
@@ -1,5 +1,6 @@
import { prisma } from "@dub/prisma";
import { bulkCreateLinks } from "../api/links";
+import { ProcessedLinkProps } from "../types";
import { redis } from "../upstash";
import { RewardfulApi } from "./api";
import { MAX_BATCHES, rewardfulImporter } from "./importer";
@@ -54,7 +55,12 @@ export async function importAffiliateCoupons(payload: RewardfulImportPayload) {
),
);
- const affiliateIdToCouponsMap = affiliateCoupons.reduce(
+ // Find the coupons that have a partner account created on Dub
+ const filteredCoupons = affiliateCoupons.filter(
+ (affiliateCoupon) => filteredPartners[affiliateCoupon.affiliate_id],
+ );
+
+ const affiliateIdToCouponsMap = filteredCoupons.reduce(
(acc, coupon) => {
if (!acc[coupon.affiliate_id]) {
acc[coupon.affiliate_id] = [];
@@ -63,44 +69,44 @@ export async function importAffiliateCoupons(payload: RewardfulImportPayload) {
acc[coupon.affiliate_id].push(coupon);
return acc;
},
- {} as Record<string, typeof affiliateCoupons>,
- );
- if (Object.keys(filteredPartners).length > 0) {
- const linksToCreate: any[] = [];
-
- for (const [affiliateId, partnerId] of Object.entries(filteredPartners)) {
- const coupons = affiliateIdToCouponsMap[affiliateId];
+ {} as Record<string, typeof filteredCoupons>,
+ );
- if (!coupons) {
- continue;
- }
+ const linksToCreate: Partial<ProcessedLinkProps>[] = [];
- const activeCoupons = affiliateCoupons.filter(
- (coupon) => !coupon.archived,
- );
+ if (Object.keys(affiliateIdToCouponsMap).length > 0) {
+ for (const [affiliateId, coupons] of Object.entries(
+ affiliateIdToCouponsMap,
+ )) {
+ const partnerId = filteredPartners[affiliateId];
- if (activeCoupons.length === 0) {
+ if (!partnerId) {
continue;
}
linksToCreate.push(
- ...activeCoupons.map((coupon) => ({
- domain: program.domain,
+ ...coupons.map((coupon) => ({
+ domain: program.domain!,
key: coupon.token,
- url: program.url,
+ url: program.url!,
trackConversion: true,
programId,
partnerId,
folderId: program.defaultFolderId,
userId,
projectId: program.workspaceId,
+ comments: `Link created for coupon ${coupon.token}`,
})),
);
}
+ }
+
+ console.log(linksToCreate)
+ if (linksToCreate.length > 0) {
await bulkCreateLinks({
- links: linksToCreate,
+ links: linksToCreate as ProcessedLinkProps[],
});
}
diff --git a/apps/web/lib/rewardful/import-partners.ts b/apps/web/lib/rewardful/import-partners.ts
index a64d48d993..a942002541 100644
--- a/apps/web/lib/rewardful/import-partners.ts
+++ b/apps/web/lib/rewardful/import-partners.ts
@@ -205,26 +205,23 @@ async function createPartnerAndLinks({
return;
}
- if (programEnrollment.links.length > 0) {
- console.log("Partner already has links", partner.id);
- return;
+ if (programEnrollment.links.length === 0) {
+ await bulkCreateLinks({
+ links: affiliate.links.map((link, idx) => ({
+ domain: program.domain!,
+ key: link.token || nanoid(),
+ url: program.url!,
+ trackConversion: true,
+ programId: program.id,
+ partnerId: partner.id,
+ folderId: program.defaultFolderId,
+ userId,
+ projectId: program.workspaceId,
+ partnerGroupDefaultLinkId: idx === 0 ? partnerGroupDefaultLinkId : null,
+ })),
+ });
}
- await bulkCreateLinks({
- links: affiliate.links.map((link, idx) => ({
- domain: program.domain!,
- key: link.token || nanoid(),
- url: program.url!,
- trackConversion: true,
- programId: program.id,
- partnerId: partner.id,
- folderId: program.defaultFolderId,
- userId,
- projectId: program.workspaceId,
- partnerGroupDefaultLinkId: idx === 0 ? partnerGroupDefaultLinkId : null,
- })),
- });
-
return {
rewardfulAffiliateId: affiliate.id,
dubPartnerId: partner.id,