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 create-partner-commission.ts

Kiran Kab39fe7f9e1a
diff --git a/apps/web/lib/partners/create-partner-commission.ts b/apps/web/lib/partners/create-partner-commission.ts
index 7b44920c1b..995e3c0785 100644
--- a/apps/web/lib/partners/create-partner-commission.ts
+++ b/apps/web/lib/partners/create-partner-commission.ts
@@ -55,10 +55,6 @@ export const createPartnerCommission = async ({
}
let status: CommissionStatus = "pending";
- let finalReward: Pick<
- RewardProps,
- "type" | "amount" | "maxDuration" | "maxAmount"
- > = reward;
if (event === "sale") {
const firstCommission = await prisma.commission.findFirst({
@@ -73,25 +69,20 @@ export const createPartnerCommission = async ({
});
if (firstCommission) {
- // if the first commission has a reward type, amount, and max duration, we need to use that reward
- if (
- firstCommission.rewardType &&
- firstCommission.rewardAmount &&
- firstCommission.rewardMaxDuration
- ) {
- finalReward = {
- ...reward,
- type: firstCommission.rewardType,
- amount: firstCommission.rewardAmount,
- maxDuration: firstCommission.rewardMaxDuration,
- };
- }
+ // use reward details from the first sale to lock in original terms.
+ // ensures historical consistency if the reward configuration has changed since the commission was created
+ reward = {
+ ...reward,
+ type: firstCommission.rewardType!,
+ amount: firstCommission.rewardAmount!,
+ maxDuration: firstCommission.rewardMaxDuration,
+ };
// for reward types with a max duration, we need to check if the first commission is within the max duration
// if its beyond the max duration, we should not create a new commission
- if (typeof finalReward.maxDuration === "number") {
+ if (typeof reward.maxDuration === "number") {
// One-time sale reward
- if (finalReward.maxDuration === 0) {
+ if (reward.maxDuration === 0) {
console.log(
`Partner ${partnerId} is only eligible for first-sale commissions, skipping commission creation...`,
);
@@ -105,7 +96,7 @@ export const createPartnerCommission = async ({
firstCommission.createdAt,
);
- if (monthsDifference >= finalReward.maxDuration) {
+ if (monthsDifference >= reward.maxDuration) {
console.log(
`Partner ${partnerId} has reached max duration for ${event} event, skipping commission creation...`,
);
@@ -127,16 +118,16 @@ export const createPartnerCommission = async ({
let earnings =
event === "sale"
? calculateSaleEarnings({
- reward: finalReward,
+ reward,
sale: {
quantity,
amount,
},
})
- : finalReward.amount * quantity;
+ : reward.amount * quantity;
// handle rewards with max reward amount limit
- if (finalReward.maxAmount) {
+ if (reward.maxAmount) {
const totalRewards = await prisma.commission.aggregate({
where: {
earnings: {
@@ -155,14 +146,14 @@ export const createPartnerCommission = async ({
});
const totalEarnings = totalRewards._sum.earnings || 0;
- if (totalEarnings >= finalReward.maxAmount) {
+ if (totalEarnings >= reward.maxAmount) {
console.log(
`Partner ${partnerId} has reached max reward amount for ${event} event, skipping commission creation...`,
);
return;
}
- const remainingRewardAmount = finalReward.maxAmount - totalEarnings;
+ const remainingRewardAmount = reward.maxAmount - totalEarnings;
earnings = Math.max(0, Math.min(earnings, remainingRewardAmount));
}
@@ -183,6 +174,9 @@ export const createPartnerCommission = async ({
earnings,
status,
createdAt,
+ rewardType: reward.type,
+ rewardAmount: reward.amount,
+ rewardMaxDuration: reward.maxDuration,
},
});