Skip to content

feat(charges): add backend customer charge override set - #4888

Merged
turip merged 5 commits into
mainfrom
feat/charges-set-override-backend-api
Aug 12, 2026
Merged

turip merged 5 commits into
mainfrom
feat/charges-set-override-backend-api

Conversation

@turip

@turip turip commented Aug 8, 2026 •

Copy link
Copy Markdown
Member

Summary

  • add the backend customer charge override Set facade
  • introduce the typed generic Set override patch
  • replace the complete mutable override snapshot while preserving the base intent
  • support flat-fee and usage-based settlement state machines
  • reject credit purchases and post-realization invoice-backed usage-based updates

Stack

Verification

  • production charge packages build
  • focused existing charge and server suites pass
  • no new behavioral test cases are included in this split

Summary by CodeRabbit

  • New Features

    • Added support for updating customer charge overrides for flat-fee and usage-based charges.
    • Overrides replace complete mutable charge settings while preserving the original base intent.
    • Updates reconcile credits, invoices, and usage realization history where supported.
    • Override changes persist through subscription cancellation and resynchronization.
  • Bug Fixes

    • Added validation for ownership, charge type, required fields, deleted charges, and unsupported charge states.
    • Prevented invoice-backed usage-based overrides after realization has begun.

Greptile Summary

The PR introduces a customer-facing facade for replacing complete mutable override snapshots on flat-fee and usage-based charges while preserving base intent.

  • Adds a typed generic set-override patch and state-machine trigger.
  • Reconciles flat-fee credits and invoice projections after override changes.
  • Voids and rebuilds mutable credit-only usage realization history.
  • Rejects credit-purchase overrides, deleted charges, and invoice-backed usage updates after realization begins.
  • Adds service, validation, subscription-sync, and server integration coverage.

Confidence Score: 5/5

The PR appears safe to merge.

No blocking failure remains.

Important Files Changed

Filename Overview
openmeter/billing/charges/api.go Defines and validates the customer override replacement request without exposing deletion through this operation.
openmeter/billing/charges/meta/patchsetoverride.go Adds an API-only typed patch that snapshots mutable fields and always targets the override layer.
openmeter/billing/charges/service/api.go Implements ownership-aware dispatch for supported charge types and rejects credit purchases.
openmeter/billing/charges/flatfee/service/creditsonly.go Rerates flat-fee overrides and reconciles an existing credit realization run.
openmeter/billing/charges/flatfee/service/creditheninvoice.go Reuses invoice-state reconciliation after replacing a flat-fee override.
openmeter/billing/charges/usagebased/service/creditsonly.go Corrects and voids mutable usage runs before rescheduling from the replacement override.
openmeter/billing/charges/usagebased/service/creditheninvoice.go Supports pre-realization override updates and rejects updates once invoice-backed realization history exists.
openmeter/billing/charges/service/api_test.go Covers replacement semantics, ownership and type validation, deletion ordering, settlement reconciliation, and subscription synchronization.

Sequence Diagram

sequenceDiagram
  participant Caller
  participant Facade as Customer Charge Facade
  participant ChargeSM as Charge State Machine
  participant Store as Charge Persistence
  participant Billing as Invoice/Credit Reconciliation

  Caller->>Facade: SetCustomerChargeOverride
  Facade->>Facade: Validate ownership, payload, and charge type
  Facade->>ChargeSM: Apply typed SetOverride patch
  ChargeSM->>Store: Create or replace override layer
  alt Flat fee
    ChargeSM->>Billing: Rerate and reconcile credits/invoice state
  else Credit-only usage
    ChargeSM->>Billing: Correct and void mutable runs
    ChargeSM->>Store: Reschedule charge from override
  else Invoice-backed usage after realization
    ChargeSM-->>Facade: Reject with precondition failure
  end
  Facade-->>Caller: Return refreshed charge
Loading

Reviews (7): Last reviewed commit: "style(charges): format override patch al..." | Re-trigger Greptile

Context used:

@coderabbitai

coderabbitai Bot commented Aug 8, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro Plus

Run ID: 50eba168-04ed-4dad-8471-aee6cc983bbc

📥 Commits

Reviewing files that changed from the base of the PR and between c07407c and f9c12dc.

📒 Files selected for processing (2)
  • openmeter/billing/charges/flatfee/patch.go
  • openmeter/billing/charges/usagebased/patch.go
🚧 Files skipped from review as they are similar to previous changes (2)
  • openmeter/billing/charges/flatfee/patch.go
  • openmeter/billing/charges/usagebased/patch.go

📝 Walkthrough

Walkthrough

The PR adds customer charge override creation and replacement for flat-fee and usage-based charges. It validates mutable snapshots, preserves base intent, applies lifecycle-specific rerating, and rejects unsupported realization states.

Changes

Customer charge overrides

Layer / File(s) Summary
Override contract and validation
openmeter/billing/charges/api.go, openmeter/billing/charges/meta/*, openmeter/billing/charges/flatfee/*, openmeter/billing/charges/usagebased/*, openmeter/billing/charges/README.md, openmeter/billing/charges/api_test.go, openmeter/billing/charges/meta/patchsetoverride_test.go
Adds the set-override input, typed patch contract, validation, snapshot cloning, target-layer resolution, operation trigger, and documentation.
Customer API application
openmeter/billing/charges/service/api.go, openmeter/billing/charges/service/api_test.go, openmeter/server/server_test.go
Validates ownership and payloads, creates type-specific patches, applies overrides, returns updated charges, and tests reconciliation and rejection behavior.
Flat-fee override lifecycle
openmeter/billing/charges/flatfee/service/*
Routes set-override triggers by state, replaces mutable intent fields, rerates proration, and reconciles credits and invoices.
Usage-based override lifecycle
openmeter/billing/charges/usagebased/service/*, openmeter/billing/worker/subscriptionsync/service/creditsonly_test.go
Applies overrides before realization, updates gathering lines, voids or persists realization runs, rejects unsupported states, and verifies subscription cancellation behavior.

Estimated code review effort: 4 (Complex) | ~45 minutes

Sequence Diagram(s)

sequenceDiagram
  participant Customer
  participant CustomerChargeAPIService
  participant ChargeStateMachine
  participant ChargeState
  Customer->>CustomerChargeAPIService: SetCustomerChargeOverride
  CustomerChargeAPIService->>ChargeStateMachine: Apply set_override patch
  ChargeStateMachine->>ChargeState: Rerate and reconcile charge
  ChargeState-->>CustomerChargeAPIService: Updated charge
  CustomerChargeAPIService-->>Customer: Updated charge
Loading

Possibly related PRs

Suggested reviewers: tothandras

🚥 Pre-merge checks | ✅ 4
✅ Passed checks (4 passed)
Check name Status Explanation
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly identifies the main change: adding a backend customer charge override set operation.
✨ Finishing Touches 💡 1
🛠️ Fix failing CI checks 💡
  • Create stacked PR
  • Commit on current branch
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch feat/charges-set-override-backend-api

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@turip
turip force-pushed the feat/charges-set-override-backend-api branch from faa6040 to 0e3b6c8 Compare August 9, 2026 06:33
@turip
turip force-pushed the feat/charges-delete-backend-api branch from 415fb14 to 9ec1a79 Compare August 9, 2026 11:13
@turip
turip force-pushed the feat/charges-set-override-backend-api branch from 0e3b6c8 to fb06895 Compare August 9, 2026 11:14
@turip
turip force-pushed the feat/charges-delete-backend-api branch from 9ec1a79 to 80a7fef Compare August 11, 2026 13:18
Base automatically changed from feat/charges-delete-backend-api to main August 11, 2026 14:03
@turip
turip force-pushed the feat/charges-set-override-backend-api branch from fb06895 to 49cbc32 Compare August 11, 2026 14:08
@turip
turip marked this pull request as ready for review August 11, 2026 14:08
@turip
turip requested a review from a team as a code owner August 11, 2026 14:08

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🧹 Nitpick comments (1)
openmeter/billing/charges/service/api_test.go (1)

689-695: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low value

Drop the pass-through SetupSuite and TearDownTest.

Both methods only call the embedded BaseSuite version. Go promotes embedded methods, so removing them keeps the same behavior with less code.

♻️ Proposed cleanup
-func (s *CustomerChargeAPISetOverrideTestSuite) SetupSuite() {
-	s.BaseSuite.SetupSuite()
-}
-
-func (s *CustomerChargeAPISetOverrideTestSuite) TearDownTest() {
-	s.BaseSuite.TearDownTest()
-}
-

As per coding guidelines: "Do not extract trivial or single-use helpers unless the name captures non-obvious domain intent; inline pass-through wrappers."

🤖 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/service/api_test.go` around lines 689 - 695, Remove
the pass-through SetupSuite and TearDownTest methods from
CustomerChargeAPISetOverrideTestSuite, allowing the embedded BaseSuite methods
to be promoted and preserving the existing behavior.

Source: Coding guidelines

🤖 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.

Inline comments:
In `@openmeter/billing/charges/service/api.go`:
- Around line 143-145: Update the ownership check in the charge retrieval flow
so the customerID.ID != input.CustomerID branch returns
models.NewGenericNotFoundError(errors.New("charge not found")) instead of a
formatted ownership error, producing a typed 404 without revealing charge
ownership.

---

Nitpick comments:
In `@openmeter/billing/charges/service/api_test.go`:
- Around line 689-695: Remove the pass-through SetupSuite and TearDownTest
methods from CustomerChargeAPISetOverrideTestSuite, allowing the embedded
BaseSuite methods to be promoted and preserving the existing behavior.
🪄 Autofix

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 Plus

Run ID: 3de04aa4-e9c5-4d1f-8d84-0523cc31bd03

📥 Commits

Reviewing files that changed from the base of the PR and between 3873500 and 49cbc32.

📒 Files selected for processing (21)
  • openmeter/billing/charges/README.md
  • openmeter/billing/charges/api.go
  • openmeter/billing/charges/api_test.go
  • openmeter/billing/charges/flatfee/charge.go
  • openmeter/billing/charges/flatfee/patch.go
  • openmeter/billing/charges/flatfee/service/creditheninvoice.go
  • openmeter/billing/charges/flatfee/service/creditsonly.go
  • openmeter/billing/charges/flatfee/service/statemachine.go
  • openmeter/billing/charges/meta/patch.go
  • openmeter/billing/charges/meta/patchsetoverride.go
  • openmeter/billing/charges/meta/patchsetoverride_test.go
  • openmeter/billing/charges/meta/triggers.go
  • openmeter/billing/charges/service/api.go
  • openmeter/billing/charges/service/api_test.go
  • openmeter/billing/charges/usagebased/charge.go
  • openmeter/billing/charges/usagebased/patch.go
  • openmeter/billing/charges/usagebased/service/creditheninvoice.go
  • openmeter/billing/charges/usagebased/service/creditsonly.go
  • openmeter/billing/charges/usagebased/service/statemachine.go
  • openmeter/billing/worker/subscriptionsync/service/creditsonly_test.go
  • openmeter/server/server_test.go

@turip turip added release-note/feature Release note: Exciting New Features area/billing labels Aug 12, 2026
@turip
turip requested a review from borosr August 12, 2026 08:01
borosr
borosr previously approved these changes Aug 12, 2026
borosr
borosr previously approved these changes Aug 12, 2026
@turip
turip enabled auto-merge (squash) August 12, 2026 08:18
@turip
turip merged commit 4fa7c53 into main Aug 12, 2026
43 of 44 checks passed
@turip
turip deleted the feat/charges-set-override-backend-api branch August 12, 2026 08:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/billing release-note/feature Release note: Exciting New Features

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants