Repository navigation
feat: usage based patching - #4053
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)
📝 WalkthroughWalkthroughAdds deletion and patch-triggering for usage-based charges: new adapter and service methods ( Changes
Sequence Diagram(s)sequenceDiagram
participant Client as Client
participant Service as UsageBasedService
participant StateMachine as StateMachine
participant Adapter as Adapter
participant Meta as MetaAdapter
Client->>Service: TriggerPatch(chargeID, patch)
Service->>Service: withLockedCharge(lock & load charge)
Service->>Service: getStateMachineConfigForPatch(charge)
Service->>StateMachine: FireAndActivate(trigger, args...)
StateMachine->>Service: invoke DeleteCharge callback (if delete)
Service->>Service: optionally generate credit corrections
Service->>Adapter: DeleteCharge(charge)
Adapter->>Adapter: set DeletedAt, status->Deleted, persist
Adapter->>Meta: DeleteRegisteredCharge(chargeID)
Meta-->>Adapter: ack
Adapter-->>Service: ack
Service->>Service: refetchCharge()
Service-->>Client: return updated (deleted) charge
Estimated code review effort🎯 4 (Complex) | ⏱️ ~45 minutes Possibly related PRs
Suggested labels
Suggested reviewers
🚥 Pre-merge checks | ✅ 3✅ Passed checks (3 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 |
6ba0114 to
9c96dfc
Compare
9c96dfc to
43bfebe
Compare
There was a problem hiding this comment.
Actionable comments posted: 4
🤖 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/adapter/charge.go`:
- Around line 117-121: The soft-delete of the registered charge must run inside
the same DB transaction as update.Save to avoid drift; change the call site in
charge.go to invoke a meta-adapter method that operates on the current tx (e.g.,
DeleteRegisteredChargeTx or DeleteRegisteredCharge(ctx, tx)) instead of the
existing standalone DeleteRegisteredCharge, and update
openmeter/billing/charges/meta/adapter/charges.go to add a Tx-aware variant (or
overload) that uses the provided transaction rather than starting its own;
ensure the new Tx-aware method is used here so both update.Save and the
registered-charge delete are committed/rolled back together.
In `@openmeter/billing/charges/usagebased/service/creditsonly.go`:
- Around line 125-147: The refund corrections are committed with
s.Adapter.CreateRunCreditRealization inside the loop and the charge is deleted
later with s.Adapter.DeleteCharge, which can leave refunds persisted if the
delete fails; wrap the create-corrections + delete into a single atomic
operation by adding a new transactional adapter method (e.g.,
Adapter.CreateRunCreditRealizationsAndDeleteCharge or Adapter.WithTx(func(tx
AdapterTx) { CreateRunCreditRealization(tx, ...); DeleteCharge(tx, ...) })) and
call that instead of separate CreateRunCreditRealization/DeleteCharge calls;
update service handler usage to pass all corrections for a run and invoke the
new transactional method so both persistence of corrections and deletion of
s.Charge succeed or roll back together.
In `@openmeter/billing/charges/usagebased/service/triggers.go`:
- Around line 62-67: The TriggerPatch function currently calls
stateMachine.FireAndActivate(ctx, patch.Trigger(), patch.TriggerParams()) and
then returns nil, nil which conflicts with its declared return type
(*usagebased.Charge, error); update it either to return the resulting charge
like AdvanceCharge does (refetch or obtain the charge from the state machine
after FireAndActivate and return that along with nil error) or change
TriggerPatch's signature to return only error and update callers/interfaces
accordingly; locate TriggerPatch and the stateMachine.FireAndActivate usage and
choose the appropriate fix (return the charge object instead of nil or simplify
the signature to error).
In `@openmeter/billing/worker/subscriptionsync/service/creditsonly_test.go`:
- Around line 893-979: Add a new test case in creditsonly_test.go that exercises
the refund-correction branch of CreditsOnlyStateMachine.DeleteCharge: create or
seed a usage-based charge that already has a Realization with CreditsAllocated
(non-zero), then trigger the deletion path (same pattern used here: Cancel
subscription + Service.SynchronizeSubscription or directly call DeleteCharge
flow), fetch the resulting charge(s) via s.Charges.GetByID and
AsUsageBasedCharge, and assert that a correction realization was emitted with
adjusted Totals (Refund/Correction reflected in Totals.Total and
Totals.CreditsTotal) and that CreditsAllocated entries were adjusted/removed as
expected; reference the helpers expectCreditsOnlyUsageBasedCharges,
Charges.GetByID, AsUsageBasedCharge, and the
CreditsAllocated/CreditRefundPolicyCorrect symbols to locate where to add/setup
assertions. Ensure the test verifies both the emitted correction realization(s)
values and the final deleted/updated charge statuses.
🪄 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: ac814d13-e669-4d82-820e-a61056a434d5
⛔ 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 (10)
openmeter/billing/charges/service/helpers.goopenmeter/billing/charges/usagebased/adapter.goopenmeter/billing/charges/usagebased/adapter/charge.goopenmeter/billing/charges/usagebased/service.goopenmeter/billing/charges/usagebased/service/creditsonly.goopenmeter/billing/charges/usagebased/service/statemachine.goopenmeter/billing/charges/usagebased/service/triggers.goopenmeter/billing/charges/usagebased/statemachine.goopenmeter/billing/worker/subscriptionsync/service/creditsonly_test.goopenmeter/billing/worker/subscriptionsync/service/reconciler/patchchargeusagebased.go
💤 Files with no reviewable changes (1)
- openmeter/billing/worker/subscriptionsync/service/reconciler/patchchargeusagebased.go
Overview
Add support for usage-based patching.
Notes for reviewer
Summary by CodeRabbit
New Features
Behavior Changes
Bug Fixes
Tests