Customer reattribution script - #4313
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
|
Caution Review failedAn error occurred during the review process. Please try again later. 📝 WalkthroughWalkthroughAdds a one-off Beehiiv customer reattribution script. The script validates current state, supports dry runs, updates database attribution and payout data, synchronizes Tinybird events, and logs before-and-after state. ChangesBeehiiv reattribution workflow
Estimated code review effort: 4 (Complex) | ~45 minutes Sequence Diagram(s)sequenceDiagram
participant Operator
participant ReattributeScript
participant Prisma
participant Tinybird
Operator->>ReattributeScript: start dry-run or execution
ReattributeScript->>Prisma: load and validate attribution state
ReattributeScript->>Tinybird: load matching lead and sale events
ReattributeScript->>Prisma: apply customer, commission, payout, and counter updates
ReattributeScript->>Tinybird: write replacement events and delete old rows
ReattributeScript->>Prisma: load and print final state
Possibly related PRs
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ 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/beehiiv/reattribute-customer.ts`:
- Around line 46-56: Update deleteTinybirdRows in
apps/web/scripts/customers/beehiiv/reattribute-customer.ts at lines 46-56 to
reject non-successful Tinybird responses and validate the delete response before
returning. At lines 298-310, rethrow failed lead deletion results instead of
only logging Promise.allSettled rejections. Apply the same change to failed sale
deletions at lines 325-337 so both workflows fail when cleanup does not
complete.
- Around line 76-85: Validate every ID in payoutIdsToRetally against its payout
status before mutating processed commissions, rather than validating only
PAYOUT_ID. Remove the fallback that adds PAYOUT_ID when no processed commission
references it, or add it only when that ID is present among the processed
commissions. Ensure retallyPayoutsAmount receives only validated, mutable payout
IDs.
- Around line 119-132: Extend the pre-write validation around the existing
attribution guards to verify customer.programId, oldLink.programId,
newLink.programId, and oldLink.partnerId against the configured expected program
and old-link attribution. Reject any mismatch before the update logic executes,
while preserving the existing partnerId and linkId checks and error-abort
behavior.
- Around line 192-285: Make the reattribution flow atomic by wrapping the
customer, commission, activity-log, payout-retally, and link-counter writes in a
single Prisma transaction around the existing update sequence. Update
retallyPayoutsAmount to accept and use the transaction client, and rethrow any
caught error so the transaction rolls back instead of continuing after a failed
retally. Keep partner total synchronization within the same transaction using
that client.
🪄 Autofix
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 Plus
Run ID: a6eb1c24-dd03-48a9-90d5-ea1c92d2f867
📒 Files selected for processing (1)
apps/web/scripts/customers/beehiiv/reattribute-customer.ts
Summary by CodeRabbit