Skip to content

fix: accrue ref rewards in finance log before batch processing#3476

Open
TaprootFreak wants to merge 1 commit intodevelopfrom
fix/finance-log-accrue-ref-rewards
Open

fix: accrue ref rewards in finance log before batch processing#3476
TaprootFreak wants to merge 1 commit intodevelopfrom
fix/finance-log-accrue-ref-rewards

Conversation

@TaprootFreak
Copy link
Collaborator

Summary

  • Ref expenses in the finance log jumped suddenly because costs were only recognized when createPendingRefRewards() created RefReward records — not when refCredit was incremented on the user
  • Fix: calculate accrued but unprocessed open credit (SUM(refCredit - paidRefCredit) minus in-progress RefRewards) and include it in the ref expense total
  • Adds accrued field to the ref breakdown in the change log for transparency (amount = processed RefRewards, accrued = unprocessed open credit, fee = payout tx fees)

How it works

Component Source Unit
amount SUM(amountInChf) from RefReward records this month CHF
accrued (totalOpenCredit - inProgressRefRewards) * EUR/CHF CHF
fee SUM(feeAmountChf) from PayoutOrder (REF_PAYOUT) CHF

Math.max(0, ...) prevents negative values from timing race conditions.

Test plan

  • TypeScript compiles without errors
  • log-job.service.spec.ts tests pass (9/9)
  • Verify ref expense line in finance log grows smoothly instead of jumping
  • Verify accrued field appears in /dashboard/financial/changes response

Ref expenses jumped in the finance log because costs were only recognized
when the batch job created RefReward records, not when refCredit was
incremented on the user. Add accrued open credit (refCredit - paidRefCredit
minus in-progress RefRewards) to the ref expense calculation so costs
appear smoothly as they are earned.
@TaprootFreak TaprootFreak marked this pull request as ready for review March 20, 2026 08:41
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