Skip to content

Customer reattribution script - #4313

Open
pepeladeira wants to merge 2 commits into
mainfrom
reattribute-customer-script
Open

Customer reattribution script#4313
pepeladeira wants to merge 2 commits into
mainfrom
reattribute-customer-script

Conversation

@pepeladeira

@pepeladeira pepeladeira commented Aug 12, 2026

Copy link
Copy Markdown
Collaborator

Summary by CodeRabbit

  • New Features
    • Added a Beehiiv customer reattribution tool with dry-run and execution modes.
    • Validates attribution ownership, commission status, and payout eligibility before making changes.
    • Updates related attribution, payout, partner totals, counters, and event records.
    • Displays before-and-after results and safely handles missing event data.

@vercel

vercel Bot commented Aug 12, 2026

Copy link
Copy Markdown
Contributor

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
dub Ready Ready Preview Aug 12, 2026 10:58pm

Request Review

@coderabbitai

coderabbitai Bot commented Aug 12, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

Caution

Review failed

An error occurred during the review process. Please try again later.

📝 Walkthrough

Walkthrough

Adds 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.

Changes

Beehiiv reattribution workflow

Layer / File(s) Summary
Validation and dry-run controls
apps/web/scripts/customers/beehiiv/reattribute-customer.ts
The script loads attribution data, classifies commissions, validates customer, link, commission, and payout conditions, and reports planned changes before optional execution.
Database reattribution and totals
apps/web/scripts/customers/beehiiv/reattribute-customer.ts
Execution updates customer and commission attribution, detaches processed commissions from payouts, deletes activity logs, retallys payouts, adjusts link counters, and synchronizes partner totals.
Tinybird event synchronization and verification
apps/web/scripts/customers/beehiiv/reattribute-customer.ts
The script rewrites matching lead and sale events, deletes old-link rows, skips missing events, collects deletion results with Promise.allSettled, and logs post-execution state.

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
Loading

Possibly related PRs

  • dubinc/dub#4157: Both scripts reattribute Beehiiv customers across links, commissions, payouts, counters, and Tinybird events.
🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and concisely describes the pull request's main change: adding a customer reattribution script.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch reattribute-customer-script

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

📥 Commits

Reviewing files that changed from the base of the PR and between 396cac4 and 9dec79b.

📒 Files selected for processing (1)
  • apps/web/scripts/customers/beehiiv/reattribute-customer.ts

Comment thread apps/web/scripts/customers/beehiiv/reattribute-customer.ts
Comment thread apps/web/scripts/customers/beehiiv/reattribute-customer.ts Outdated
Comment thread apps/web/scripts/customers/beehiiv/reattribute-customer.ts
Comment thread apps/web/scripts/customers/beehiiv/reattribute-customer.ts
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant