-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Rewardful import coupons #2963
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Rewardful import coupons #2963
Changes from 8 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
fa82d53
wip
devkiran bb861bf
create links for affiliate coupons
devkiran 2b5ada9
Update import-affiliate-coupons.ts
devkiran 32ff7e0
Update import-campaigns.ts
devkiran 3e3adae
Merge branch 'main' into rewardful-import-coupons
devkiran a9a4725
Update types.ts
devkiran 6f1b31c
Update import-affiliate-coupons.ts
devkiran d1924eb
Merge branch 'main' into rewardful-import-coupons
devkiran ca76c57
Update import-affiliate-coupons.ts
devkiran 31e2bc2
Merge branch 'rewardful-import-coupons' of https://github.com/dubinc/…
devkiran 8e42b2c
Update Rewardful coupon type references
devkiran cdd99ba
Update import-commissions.ts
devkiran dbd6ba5
Update migrate-sales.ts
devkiran d0512a9
Merge branch 'main' into rewardful-import-coupons
steven-tey d7314e0
Merge branch 'main' into rewardful-import-coupons
steven-tey File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
import { prisma } from "@dub/prisma"; | ||
import { bulkCreateLinks } from "../api/links"; | ||
import { redis } from "../upstash"; | ||
import { RewardfulApi } from "./api"; | ||
import { MAX_BATCHES, rewardfulImporter } from "./importer"; | ||
import { RewardfulImportPayload } from "./types"; | ||
|
||
export async function importAffiliateCoupons(payload: RewardfulImportPayload) { | ||
const { programId, userId, page = 1 } = payload; | ||
|
||
const program = await prisma.program.findUniqueOrThrow({ | ||
where: { | ||
id: programId, | ||
}, | ||
select: { | ||
id: true, | ||
workspaceId: true, | ||
domain: true, | ||
url: true, | ||
defaultFolderId: true, | ||
}, | ||
}); | ||
|
||
const { token } = await rewardfulImporter.getCredentials(program.workspaceId); | ||
|
||
const rewardfulApi = new RewardfulApi({ token }); | ||
|
||
let currentPage = page; | ||
let hasMore = true; | ||
let processedBatches = 0; | ||
|
||
while (hasMore && processedBatches < MAX_BATCHES) { | ||
const affiliateCoupons = await rewardfulApi.listAffiliateCoupons({ | ||
page: currentPage, | ||
}); | ||
|
||
if (affiliateCoupons.length === 0) { | ||
hasMore = false; | ||
break; | ||
} | ||
devkiran marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
const affiliateIds = affiliateCoupons.map( | ||
(affiliateCoupon) => affiliateCoupon.affiliate_id, | ||
); | ||
|
||
const results = await redis.hmget<Record<string, string>>( | ||
`rewardful:affiliates:${program.id}`, | ||
...affiliateIds, | ||
); | ||
|
||
const filteredPartners = Object.fromEntries( | ||
Object.entries(results ?? {}).filter( | ||
([_, value]) => value !== null && value !== undefined, | ||
), | ||
); | ||
|
||
const affiliateIdToCouponsMap = affiliateCoupons.reduce( | ||
(acc, coupon) => { | ||
if (!acc[coupon.affiliate_id]) { | ||
acc[coupon.affiliate_id] = []; | ||
} | ||
|
||
acc[coupon.affiliate_id].push(coupon); | ||
return acc; | ||
}, | ||
{} as Record<string, typeof affiliateCoupons>, | ||
); | ||
|
||
if (Object.keys(filteredPartners).length > 0) { | ||
const linksToCreate: any[] = []; | ||
devkiran marked this conversation as resolved.
Show resolved
Hide resolved
devkiran marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
for (const [affiliateId, partnerId] of Object.entries(filteredPartners)) { | ||
const coupons = affiliateIdToCouponsMap[affiliateId]; | ||
|
||
if (!coupons) { | ||
continue; | ||
} | ||
|
||
linksToCreate.push( | ||
...coupons.map((coupon) => ({ | ||
domain: program.domain, | ||
key: coupon.token.toLowerCase(), | ||
devkiran marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
url: program.url, | ||
trackConversion: true, | ||
programId, | ||
partnerId, | ||
folderId: program.defaultFolderId, | ||
userId, | ||
projectId: program.workspaceId, | ||
})), | ||
); | ||
} | ||
|
||
await bulkCreateLinks({ | ||
links: linksToCreate, | ||
}); | ||
} | ||
|
||
currentPage++; | ||
processedBatches++; | ||
} | ||
|
||
if (!hasMore) { | ||
await redis.del(`rewardful:affiliates:${program.id}`); | ||
} | ||
devkiran marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
const action = hasMore ? "import-affiliate-coupons" : "import-customers"; | ||
|
||
await rewardfulImporter.queue({ | ||
...payload, | ||
action, | ||
page: hasMore ? currentPage : undefined, | ||
}); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.