Repository navigation
feat: consolidate usage-based realization states - #4660
Conversation
📝 WalkthroughWalkthroughThis PR consolidates invoice-created trigger handling, renames usage-based realization states to a canonical ChangesTrigger and Realization Status Unification
Estimated code review effort: 4 (Complex) | ~60 minutes Possibly related PRs
Suggested labels: Suggested reviewers: 🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ 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 |
There was a problem hiding this comment.
🧹 Nitpick comments (1)
openmeter/billing/charges/usagebased/service/creditheninvoice.go (1)
664-673: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low valueTiny clarity nit:
isFinalRunInPeriodonly ever readsservicePeriod.To.The
Frombound is discarded, yet two callers (resolveStateAfterRealizationCompletedandpayments.areAllInvoicedRunsSettled) build aClosedPeriodjust to hand over a throwawayFrom. Taking the end time directly makes the intent obvious and drops the ceremony at the call sites. Totally optional, just a readability tidy-up.♻️ One possible shape
-func isFinalRunInPeriod(charge usagebased.Charge, servicePeriod timeutil.ClosedPeriod) bool { - return meta.NormalizeTimestamp(servicePeriod.To).Equal(meta.NormalizeTimestamp(charge.Intent.GetEffectiveServicePeriod().To)) +func isFinalRunEnd(charge usagebased.Charge, servicePeriodTo time.Time) bool { + return meta.NormalizeTimestamp(servicePeriodTo).Equal(meta.NormalizeTimestamp(charge.Intent.GetEffectiveServicePeriod().To)) }(and
getInvoiceRealizationRunTypewould passservicePeriod.To.)🤖 Prompt for 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. In `@openmeter/billing/charges/usagebased/service/creditheninvoice.go` around lines 664 - 673, `isFinalRunInPeriod` only uses the period end, so simplify the API by changing it (and `getInvoiceRealizationRunType`) to accept the end timestamp directly instead of a full `timeutil.ClosedPeriod`. Update the callers in `resolveStateAfterRealizationCompleted` and `payments.areAllInvoicedRunsSettled` to pass `servicePeriod.To` rather than constructing a throwaway period, and keep the comparison against `charge.Intent.GetEffectiveServicePeriod().To` via `meta.NormalizeTimestamp`.
🤖 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.
Nitpick comments:
In `@openmeter/billing/charges/usagebased/service/creditheninvoice.go`:
- Around line 664-673: `isFinalRunInPeriod` only uses the period end, so
simplify the API by changing it (and `getInvoiceRealizationRunType`) to accept
the end timestamp directly instead of a full `timeutil.ClosedPeriod`. Update the
callers in `resolveStateAfterRealizationCompleted` and
`payments.areAllInvoicedRunsSettled` to pass `servicePeriod.To` rather than
constructing a throwaway period, and keep the comparison against
`charge.Intent.GetEffectiveServicePeriod().To` via `meta.NormalizeTimestamp`.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
Run ID: c6b9531f-6f07-476a-b6c0-b1c25ab8d5bb
⛔ Files ignored due to path filters (2)
openmeter/ent/db/chargeusagebased/chargeusagebased.gois excluded by!**/ent/db/**openmeter/ent/db/migrate/schema.gois excluded by!**/ent/db/**
📒 Files selected for processing (18)
openmeter/billing/charges/flatfee/service/creditheninvoice.goopenmeter/billing/charges/flatfee/service/lineengine.goopenmeter/billing/charges/meta/triggers.goopenmeter/billing/charges/service/invoicable_test.goopenmeter/billing/charges/service/usagebased_test.goopenmeter/billing/charges/usagebased/realizationrun.goopenmeter/billing/charges/usagebased/service/creditheninvoice.goopenmeter/billing/charges/usagebased/service/creditheninvoice_test.goopenmeter/billing/charges/usagebased/service/creditsonly.goopenmeter/billing/charges/usagebased/service/creditsonly_test.goopenmeter/billing/charges/usagebased/service/lineengine.goopenmeter/billing/charges/usagebased/service/payments.goopenmeter/billing/charges/usagebased/service/statemachine.goopenmeter/billing/charges/usagebased/statemachine.goopenmeter/billing/worker/subscriptionsync/service/sync_credittheninvoice_test.gotest/credits/credit_then_invoice_test.gotest/credits/sanity_lifecycle_test.gotest/credits/sanity_test.go
Summary
Consolidates usage-based realization lifecycle states so the state machine no longer needs separate partial/final status branches. The realization run type now determines whether the completed run is final or non-final, allowing the lifecycle to dynamically route back to active for partial runs or into payment-settlement handling for final runs.
Details
active.realization.*statuses across credit-then-invoice and credits-only usage-based flows.Validation
Not run locally per request; CI will run the test suite.
Summary by CodeRabbit
active.realization.*statuses and legacy-status normalization.Greptile Summary
This PR consolidates the usage-based realization lifecycle into a single state branch. The main changes are:
active.realization.*statuses with legacy status normalization.Confidence Score: 5/5
This looks safe to merge.
Important Files Changed
Reviews (3): Last reviewed commit: "fix: preserve partial run on usage-based..." | Re-trigger Greptile
Context used (3)