Conversation
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Path: .coderabbit.yaml Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (1)
🚧 Files skipped from review as they are similar to previous changes (1)
📝 WalkthroughWalkthroughThis PR adds a new OnStandardInvoiceCreated lifecycle hook to line engines, threads invoice line IDs into usage-based realization runs, updates state-machine wiring to accept LineID, invokes per-line hooks during invoice creation with recalculation, and adds DB schema + migrations to store realization run → invoice line links. Changes
Sequence DiagramsequenceDiagram
participant Client
participant InvoiceService
participant LineEngine
participant StateMachine
participant DB
Client->>InvoiceService: CreateStandardInvoiceFromGatheringLines()
InvoiceService->>DB: persist invoice + lines
InvoiceService->>LineEngine: OnStandardInvoiceCreated(OnStandardInvoiceCreatedInput{Lines})
LineEngine->>StateMachine: newStateMachineForStandardLine(stdLine)
StateMachine->>DB: Get charge (ExpandRealizations)
StateMachine->>StateMachine: Activate / Fire with invoiceCreatedInput{LineID}
StateMachine->>DB: CreateRealizationRun(LineID)
DB-->>StateMachine: RealizationRun record
StateMachine-->>LineEngine: return StandardLines
LineEngine-->>InvoiceService: updated lines
InvoiceService->>InvoiceService: recalculate invoice
InvoiceService->>DB: persist recalculated invoice
InvoiceService-->>Client: return finalized invoice
Estimated code review effort🎯 4 (Complex) | ⏱️ ~45 minutes Possibly related PRs
Suggested labels
Suggested reviewers
🚥 Pre-merge checks | ✅ 2 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ 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 |
f780aaa to
398ce07
Compare
398ce07 to
31ebba1
Compare
There was a problem hiding this comment.
Actionable comments posted: 2
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@openmeter/billing/charges/usagebased/service/run/create.go`:
- Line 25: Add a non-empty check for LineID inside the
CreateRatedRunInput.Validate() method so the exported input self-validates and
rejects empty LineID before downstream calls; update
CreateRatedRunInput.Validate() to return an error when LineID is nil or empty
(same validation currently in createNewRealizationRun()), and remove or keep the
downstream guard in createNewRealizationRun() as desired to avoid duplicate late
failures after GetRatingForUsage() runs.
In `@openmeter/ent/schema/chargesusagebased.go`:
- Around line 198-201: The edge declaration edge.From("billing_invoice_line",
BillingInvoiceLine.Type).Ref("charge_usage_based_run").Field("line_id").Unique()
is missing the entsql OnDelete annotation; update that edge to include
Annotations(entsql.OnDelete(entsql.SetNull)) so the Ent schema matches the
migration's ON DELETE SET NULL behavior (ensure you add the annotation
import/usages consistent with other edges that specify delete actions).
🪄 Autofix (Beta)
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: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
Run ID: 0527953c-043e-411f-b41a-9806d2470902
⛔ Files ignored due to path filters (18)
openmeter/ent/db/billinginvoiceline.gois excluded by!**/ent/db/**openmeter/ent/db/billinginvoiceline/billinginvoiceline.gois excluded by!**/ent/db/**openmeter/ent/db/billinginvoiceline/where.gois excluded by!**/ent/db/**openmeter/ent/db/billinginvoiceline_create.gois excluded by!**/ent/db/**openmeter/ent/db/billinginvoiceline_query.gois excluded by!**/ent/db/**openmeter/ent/db/billinginvoiceline_update.gois excluded by!**/ent/db/**openmeter/ent/db/chargeusagebasedruns.gois excluded by!**/ent/db/**openmeter/ent/db/chargeusagebasedruns/chargeusagebasedruns.gois excluded by!**/ent/db/**openmeter/ent/db/chargeusagebasedruns/where.gois excluded by!**/ent/db/**openmeter/ent/db/chargeusagebasedruns_create.gois excluded by!**/ent/db/**openmeter/ent/db/chargeusagebasedruns_query.gois excluded by!**/ent/db/**openmeter/ent/db/chargeusagebasedruns_update.gois excluded by!**/ent/db/**openmeter/ent/db/client.gois excluded by!**/ent/db/**openmeter/ent/db/migrate/schema.gois excluded by!**/ent/db/**openmeter/ent/db/mutation.gois excluded by!**/ent/db/**openmeter/ent/db/runtime.gois excluded by!**/ent/db/**openmeter/ent/db/setorclear.gois excluded by!**/ent/db/**tools/migrate/migrations/atlas.sumis excluded by!**/*.sum,!**/*.sum
📒 Files selected for processing (19)
openmeter/billing/charges/creditpurchase/lineengine/engine.goopenmeter/billing/charges/flatfee/lineengine/engine.goopenmeter/billing/charges/service/invoicable_test.goopenmeter/billing/charges/usagebased/adapter/mapper.goopenmeter/billing/charges/usagebased/adapter/realizationrun.goopenmeter/billing/charges/usagebased/realizationrun.goopenmeter/billing/charges/usagebased/service.goopenmeter/billing/charges/usagebased/service/creditheninvoice.goopenmeter/billing/charges/usagebased/service/lineengine.goopenmeter/billing/charges/usagebased/service/run/create.goopenmeter/billing/charges/usagebased/service/stdinvoice.goopenmeter/billing/lineengine.goopenmeter/billing/lineengine/engine.goopenmeter/billing/service/gatheringinvoicependinglines.goopenmeter/ent/schema/billing.goopenmeter/ent/schema/chargesusagebased.gotest/billing/lineengine_test.gotools/migrate/migrations/20260413154216_usagebased-run-line-id.down.sqltools/migrate/migrations/20260413154216_usagebased-run-line-id.up.sql
💤 Files with no reviewable changes (2)
- openmeter/billing/charges/usagebased/service.go
- openmeter/billing/charges/usagebased/service/stdinvoice.go
Summary
Persist usage-based realization run line_id against the billing line and move the usage-based line assignment to a post-persist invoice hook.
This splits the line-ID persistence work out from the broader invoice-accrual changes.
What changed
Database / Ent
Billing lifecycle
Usage-based
Why
Previously the usage-based run could be linked too early, while the line was still in its gathering-line lifecycle. Because the FK points to the invoice line row, that timing was unstable.
This change makes the assignment happen only after the standard invoice line is persisted, so run.LineID points at the stable standard line row.
Tests
Added / updated coverage to assert that for usage-based credit_then_invoice:
Validated with:
nix develop --impure .#ci -c env GOCACHE=/tmp/go-build go test -tags=dynamic -run '^$' ./openmeter/billing/... ./test/billing
nix develop --impure .#ci -c env POSTGRES_HOST=127.0.0.1 GOCACHE=/tmp/go-build go test -count=1 -tags=dynamic -run 'TestInvoicableCharges/TestUsageBasedCreditThenInvoiceLifecycle$' -v ./openmeter/billing/charges/service
Notes
Summary by CodeRabbit
New Features
Improvements