fix(ledger): book advance attribution at purchase time#4767
Conversation
|
Important Review skippedDraft detected. Please check the settings in the CodeRabbit UI or the ⚙️ Run configurationConfiguration used: Path: .coderabbit.yaml Review profile: CHILL Plan: Pro Run ID: You can disable this status message by setting the Use the checkbox below for a quick retry:
✨ Finishing Touches🧪 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 |
| for _, attribution := range advanceAttributions { | ||
| templates = append(templates, transactions.AttributeCustomerAdvanceReceivableCostBasisTemplate{ | ||
| At: bookedAt, | ||
| At: purchasedAt, |
There was a problem hiding this comment.
Past-Effective Settlement Precedes Attribution
When a promotional purchase is materialized after its effectiveAt, this attribution is booked at the current time while authorization and settlement remain booked at the earlier effective time. An as-of query between those times therefore shows the purchase settled before its receivable attribution exists, producing invalid ledger history for a supported past-effective charge.
Prompt To Fix With AI
This is a comment left during a code review.
Path: openmeter/ledger/chargeadapter/creditpurchase.go
Line: 242
Comment:
**Past-Effective Settlement Precedes Attribution**
When a promotional purchase is materialized after its `effectiveAt`, this attribution is booked at the current time while authorization and settlement remain booked at the earlier effective time. An as-of query between those times therefore shows the purchase settled before its receivable attribution exists, producing invalid ledger history for a supported past-effective charge.
How can I resolve this? If you propose a fix, please make it concise.
Summary
effective_atBehavior
effective_atInvoice-settled purchases keep their existing trigger at invoice draft creation. This PR does not add payment control or recurring subscription grants.
Cancellation / void limitation
The existing void path operates on currently issued future-balance-overage value for the source charge. Before a future grant reaches
effective_at, its scheduled remainder has no spendable balance slice to void, and the purchase-time advance attribution is not such a slice. As a result, future-effective grants remain non-voidable beforeeffective_at; this PR does not add cancellation of scheduled ledger entries or claim to solve that lifecycle. Void behavior for remaining issued value aftereffective_atis unchanged.Tests
nix develop --impure .#ci -c env POSTGRES_HOST=127.0.0.1 go vet -tags=dynamic ./openmeter/ledger/chargeadapter ./openmeter/billing/charges/creditpurchase/servicenix develop --impure .#ci -c env POSTGRES_HOST=127.0.0.1 go test -tags=dynamic -v ./openmeter/ledger/chargeadapter ./openmeter/billing/charges/creditpurchase/servicenix develop --impure .#ci -c env POSTGRES_HOST=127.0.0.1 go test -tags=dynamic -v ./test/creditsAll database-backed tests ran against PostgreSQL; none were skipped.
Greptile Summary
This PR books existing advance attribution when a credit purchase is materialized. The main changes are:
Confidence Score: 4/5
Past-effective promotional purchases need a ledger-ordering fix before merging.
openmeter/ledger/chargeadapter/creditpurchase.go
Important Files Changed
Sequence Diagram
%%{init: {'theme': 'neutral'}}%% sequenceDiagram participant C as Purchase caller participant H as Credit purchase handler participant L as Ledger C->>H: Materialize past-effective promotional purchase H->>L: Authorize and settle at effectiveAt H->>L: Attribute advance at purchasedAt Note over L: Settlement appears before attribution in ledger history%%{init: {'theme': 'base', 'themeVariables': {"darkMode": true, "background": "#0d1117", "primaryColor": "#21262d", "primaryTextColor": "#e6edf3", "primaryBorderColor": "#8b949e", "lineColor": "#8b949e", "textColor": "#e6edf3", "edgeLabelBackground": "#161b22", "actorBkg": "#21262d", "actorBorder": "#8b949e", "actorTextColor": "#e6edf3", "actorLineColor": "#8b949e", "signalColor": "#8b949e", "signalTextColor": "#e6edf3", "noteBkgColor": "#373320", "noteBorderColor": "#d4a72c", "noteTextColor": "#f0e6c0", "labelBoxBkgColor": "#21262d", "labelBoxBorderColor": "#8b949e", "labelTextColor": "#e6edf3", "loopTextColor": "#e6edf3", "activationBkgColor": "#30363d", "activationBorderColor": "#8b949e"}}}%% sequenceDiagram participant C as Purchase caller participant H as Credit purchase handler participant L as Ledger C->>H: Materialize past-effective promotional purchase H->>L: Authorize and settle at effectiveAt H->>L: Attribute advance at purchasedAt Note over L: Settlement appears before attribution in ledger historyPrompt To Fix All With AI
Reviews (1): Last reviewed commit: "fix(ledger): book advance attribution at..." | Re-trigger Greptile
Context used: