({
event,
programEnrollment,
context,
}: {
event: EventType;
programEnrollment: ProgramEnrollmentWithReward;
context?: RewardContext; // additional reward context (e.g. customer.country, sale.productId, etc.)
})
| 34 | } |
| 35 | |
| 36 | export const determinePartnerReward = ({ |
| 37 | event, |
| 38 | programEnrollment, |
| 39 | context, |
| 40 | }: { |
| 41 | event: EventType; |
| 42 | programEnrollment: ProgramEnrollmentWithReward; |
| 43 | context?: RewardContext; // additional reward context (e.g. customer.country, sale.productId, etc.) |
| 44 | }) => { |
| 45 | let partnerReward: Reward = |
| 46 | programEnrollment[REWARD_EVENT_COLUMN_MAPPING[event]]; |
| 47 | |
| 48 | if (!partnerReward) { |
| 49 | return null; |
| 50 | } |
| 51 | |
| 52 | // Add the links metrics to the context |
| 53 | const partnerLinksStats = aggregatePartnerLinksStats(programEnrollment.links); |
| 54 | |
| 55 | context = { |
| 56 | ...context, |
| 57 | partner: { |
| 58 | ...context?.partner, |
| 59 | ...partnerLinksStats, |
| 60 | totalCommissions: toCentsNumber(programEnrollment.totalCommissions), |
| 61 | country: programEnrollment.partner?.country, |
| 62 | }, |
| 63 | }; |
| 64 | |
| 65 | if (partnerReward.modifiers && context) { |
| 66 | const modifiers = rewardConditionsArraySchema.safeParse( |
| 67 | partnerReward.modifiers, |
| 68 | ); |
| 69 | |
| 70 | // Parse the conditions before evaluating them |
| 71 | if (modifiers.success) { |
| 72 | const matchedCondition = evaluateRewardConditions({ |
| 73 | conditions: modifiers.data, |
| 74 | context, |
| 75 | }); |
| 76 | |
| 77 | if (matchedCondition) { |
| 78 | partnerReward = { |
| 79 | ...partnerReward, |
| 80 | // Override the reward amount, type and max duration with the matched condition |
| 81 | type: matchedCondition.type || partnerReward.type, |
| 82 | amountInCents: |
| 83 | matchedCondition.amountInCents != null |
| 84 | ? matchedCondition.amountInCents |
| 85 | : null, |
| 86 | amountInPercentage: |
| 87 | matchedCondition.amountInPercentage != null |
| 88 | ? new Prisma.Decimal(matchedCondition.amountInPercentage) |
| 89 | : null, |
| 90 | maxDuration: |
| 91 | matchedCondition.maxDuration !== undefined |
| 92 | ? matchedCondition.maxDuration |
| 93 | : partnerReward.maxDuration, |
no test coverage detected
searching dependent graphs…