Add script#4074
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (1)
🚧 Files skipped from review as they are similar to previous changes (1)
📝 WalkthroughWalkthroughAdds a Privy maintenance script that updates sale reward modifiers to exclude selected Stripe products and recalculates affected commission earnings, payouts, and partner totals in dry-run or execute mode. ChangesPrivy excluded product reward sync
Sequence Diagram(s)sequenceDiagram
participant main
participant updateRewards
participant updateCommissions
participant getSaleEvent
participant determinePartnerRewards
participant calculateSaleEarnings
participant Prisma
participant reconcilePayoutAmounts
participant syncTotalCommissions
main->>updateRewards: merge excluded product IDs into reward.modifiers
updateRewards->>Prisma: update reward.modifiers
main->>updateCommissions: recalculate eligible commissions
updateCommissions->>getSaleEvent: load sale event metadata
getSaleEvent-->>updateCommissions: normalized sale products
updateCommissions->>determinePartnerRewards: compute eligible rewards
determinePartnerRewards-->>updateCommissions: reward allocations
updateCommissions->>calculateSaleEarnings: sum recalculated earnings
calculateSaleEarnings-->>updateCommissions: new earnings
updateCommissions->>Prisma: cancel or update commissions
updateCommissions->>reconcilePayoutAmounts: reconcile affected payouts
updateCommissions->>syncTotalCommissions: sync partner totals
Estimated code review effort🎯 4 (Complex) | ⏱️ ~60 minutes Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 3 | ❌ 2❌ Failed checks (1 warning, 1 inconclusive)
✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 4
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@apps/web/scripts/customers/privy/update-excluded-product-rewards.ts`:
- Around line 251-256: The first-sale lookup in
update-excluded-product-rewards.ts is too broad because the
prisma.commission.findFirst call in the recalculation flow only filters by
partnerId, customerId, and type: "sale". Update that lookup, and any related
logic in the affected recalculation block, to also scope by the current program
so firstCommission cannot come from a different program. Use the existing
commission/program identifiers in this function to keep subscriptionStartDate,
subscriptionDurationMonths, and sale.type based only on the current program’s
first sale.
- Around line 44-54: The exclusion detection in isExclusionModifier is too broad
and matches any zero-value reward modifier with a sale.productId in condition,
which can cause unrelated modifiers to be rewritten. Narrow the match to only
the exact generated exclusion modifier shape by checking the full modifier
structure, including the expected conditions set and absence of extra
country/duration/other constraints, then update the replacement logic in
update-excluded-product-rewards.ts to only replace that specific modifier when
scanning and rebuilding the modifiers array.
- Around line 21-23: The updater currently allows an empty EXCLUDED_PRODUCT_IDS
list, which can cause `updateExcludedProductRewards` to run in `--execute` mode
with a meaningless `productId in []` modifier. Add a fail-fast guard near the
EXCLUDED_PRODUCT_IDS/DRY_RUN setup and in the `updateExcludedProductRewards`
flow so the script exits or skips execution when no product IDs are configured,
preserving `DRY_RUN` behavior only for valid lists.
- Around line 143-150: The `update-excluded-product-rewards` flow is creating a
fake zero-value sale row when `parsed.productId` exists, which later causes
`calculateSaleEarnings` and the commission-cancel path to treat a real sale as
zero revenue. Update the logic around `parsed.productId` in
`update-excluded-product-rewards.ts` so it does not return a synthetic row with
`amount: 0` unless the real amount is known; instead skip the row or populate it
from the verified commission amount only for a confirmed single-product sale.
Make sure the downstream recalculation/cancellation paths that use
`calculateSaleEarnings` and the commission handling at the affected reward
update steps keep the original sale amount intact.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: e8893718-6f4d-42b4-9022-17b413005406
📒 Files selected for processing (1)
apps/web/scripts/customers/privy/update-excluded-product-rewards.ts
| function isExclusionModifier(modifier: RewardConditionsArray[number]) { | ||
| const amountIsZero = | ||
| modifier.amountInCents === 0 || modifier.amountInPercentage === 0; | ||
|
|
||
| return modifier.conditions.some( | ||
| (condition) => | ||
| condition.entity === "sale" && | ||
| condition.attribute === "productId" && | ||
| condition.operator === "in" && | ||
| amountIsZero, | ||
| ); |
There was a problem hiding this comment.
🗄️ Data Integrity & Integration | 🟠 Major | ⚡ Quick win
Match only the generated exclusion modifier shape.
Line 48 treats any zero-value modifier with a sale.productId in condition as the global exclusion modifier. If an existing modifier also has country/duration/other conditions, Lines 121-130 replace it with a single product condition and change reward semantics.
Suggested narrowing
function isExclusionModifier(modifier: RewardConditionsArray[number]) {
+ const condition = modifier.conditions[0];
const amountIsZero =
- modifier.amountInCents === 0 || modifier.amountInPercentage === 0;
+ modifier.type === RewardStructureEnum.percentage
+ ? modifier.amountInPercentage === 0
+ : modifier.amountInCents === 0;
- return modifier.conditions.some(
- (condition) =>
+ return Boolean(
+ condition &&
+ modifier.operator === "AND" &&
+ modifier.maxDuration === null &&
+ modifier.conditions.length === 1 &&
condition.entity === "sale" &&
condition.attribute === "productId" &&
condition.operator === "in" &&
amountIsZero,
);
}Also applies to: 121-130
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
In `@apps/web/scripts/customers/privy/update-excluded-product-rewards.ts` around
lines 44 - 54, The exclusion detection in isExclusionModifier is too broad and
matches any zero-value reward modifier with a sale.productId in condition, which
can cause unrelated modifiers to be rewritten. Narrow the match to only the
exact generated exclusion modifier shape by checking the full modifier
structure, including the expected conditions set and absence of extra
country/duration/other constraints, then update the replacement logic in
update-excluded-product-rewards.ts to only replace that specific modifier when
scanning and rebuilding the modifiers array.
Summary by CodeRabbit