fix: track affiliate rebates from redeem codes and admin recharges - #6321
fix: track affiliate rebates from redeem codes and admin recharges#6321QHSsss wants to merge 4 commits into
Conversation
…exes and service layer support
|
All contributors have signed the CLA. ✅ |
There was a problem hiding this comment.
🔵 Needs a closer look
It changes financial-ledger writes, transactional semantics, and production migrations/constraints across multiple layers, warranting final human validation despite strong test coverage.
Pull request overview
This PR extends the affiliate rebate system to track and display rebates generated from multiple “recharge” sources (payment orders, balance redeem codes, and admin balance adjustments), while tightening transactional guarantees (rebate accrual shares the same DB transaction as the underlying balance change) and adding deployment-safe schema changes (fast column adds, concurrent indexes, and NOT VALID constraints).
Changes:
- Add rebate source typing + base amount snapshotting across backend ledger writes, plus source-based querying/sorting/filtering for admin rebate records.
- Ensure redeem-code and admin-recharge rebates accrue atomically with their balance mutations (rollback on accrual failure) and prevent duplicate accrual paths.
- Introduce migration safety measures: concurrent indexes outside transactions, duplicate-source prechecks, and NOT VALID constraints + targeted repair of a superseded migration draft.
File summaries
| File | Description |
|---|---|
| frontend/src/views/admin/affiliates/AdminAffiliateRecordsTable.vue | Adds rebate source filter UI and displays source type/reference/status/base amount with stable request sequencing. |
| frontend/src/views/admin/tests/AdminAffiliateRecordsTable.spec.ts | Adds component tests for source filtering, status badge rendering, and stale-response protection. |
| frontend/src/i18n/locales/zh/admin/overview.ts | Updates CN admin copy and adds rebate source-related labels and placeholders. |
| frontend/src/i18n/locales/en/admin/overview.ts | Updates EN admin copy and adds rebate source-related labels and placeholders. |
| frontend/src/api/admin/affiliates.ts | Extends rebate record types and request params to include source metadata and filtering. |
| backend/migrations/affiliate_rebate_sources_migration_test.go | Asserts migration SQL stays “fast” (no historical backfills / blocking indexes) and includes required constraints/indexes. |
| backend/migrations/232_affiliate_rebate_source_constraints.sql | Adds NOT VALID FKs + CHECK constraints and repairs unsafe historical admin-source classifications from a known draft. |
| backend/migrations/231_affiliate_rebate_sources.sql | Adds source_type/base_amount/source_redeem_code_id columns to affiliate ledger with comments. |
| backend/migrations/231_affiliate_rebate_sources_indexes_notx.sql | Creates concurrent indexes and unique source idempotency indexes outside transactions. |
| backend/internal/service/redeem_service.go | Moves balance redeem-code rebate accrual into the redemption transaction (rollback on failure) and removes best-effort post-commit accrual. |
| backend/internal/service/payment_fulfillment.go | Switches payment order rebate accrual to the new generic source-based accrual API. |
| backend/internal/service/payment_fulfillment_test.go | Updates fulfillment tests/stubs to assert source type + base amount are passed through. |
| backend/internal/service/affiliate_service.go | Introduces AffiliateRebateSource + validation, threads source + per-invitee cap into repository accrual, and updates record/filter structs. |
| backend/internal/service/affiliate_service_test.go | Adds validation tests for sources and verifies source + cap are passed to the repository layer. |
| backend/internal/service/admin_user.go | Wraps admin balance changes + adjustment record + rebate accrual in a single DB transaction and returns errors on rebate accrual failure. |
| backend/internal/service/admin_service.go | Updates internal interface to the new AccrueInviteRebate signature. |
| backend/internal/service/admin_service_update_balance_test.go | Updates unit tests to reflect transactional behavior, source typing, and rollback-on-accrual-failure semantics. |
| backend/internal/repository/redeem_code_repo.go | Ensures redeem code writes participate in an existing transaction via context client selection. |
| backend/internal/repository/migrations_runner.go | Adds non-tx migration prechecks for duplicate rebate sources and invalid index cleanup; tightens execution-mode validation. |
| backend/internal/repository/migrations_runner_notx_test.go | Adds tests for execution-mode validation and affiliate index migration precheck/invalid-index retry behavior. |
| backend/internal/repository/migrations_runner_checksum_test.go | Adds checksum compatibility coverage for a superseded 231 draft migration. |
| backend/internal/repository/affiliate_repo.go | Implements source validation, idempotent accrual with caps in SQL, and expands rebate records query to include all sources + stable ordering. |
| backend/internal/repository/affiliate_repo_integration_test.go | Adds integration tests for idempotency, transactional atomicity, caps under concurrency, and historical repair behavior. |
| backend/internal/handler/auth_email_oauth_test.go | Updates stub interface to match the new AccrueQuota signature. |
| backend/internal/handler/admin/affiliate_handler.go | Adds source_type parsing/validation with backward-compatible default behavior for legacy admin clients. |
| backend/internal/handler/admin/affiliate_handler_test.go | Adds a focused test that invalid source_type is rejected early with 400. |
Review details
- Files reviewed: 26/26 changed files
- Comments generated: 0
- Review effort level: Lite
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
|
I have read the CLA Document and I hereby sign the CLA |
Summary
Problem
Affiliate rebates generated by balance redeem code top-ups were written to the affiliate ledger without a payment order.
The admin rebate records query only exposed payment-order-backed records, so rebates generated by balance redeem codes were missing from the page and could not be filtered or audited by source.
Admin balance recharge rebates also lacked a mandatory source reference, which made their origin difficult to verify.
Implementation
source_type,base_amount, andsource_redeem_code_idto affiliate rebate ledger records.payment_orderbalance_redeem_codeadmin_rechargelegacy_unknownlegacy_unknowninstead of inferring their sources from timestamps.source_typeis omitted from the API request.Migration Safety
NOT VALIDto avoid scanning the full ledger during deployment while still validating new writes.Testing
Commands executed:
All targeted tests passed.
CLA
I have read the CLA Document and I hereby sign the CLA