Skip to content

feat(charges): add backend customer charge override clear - #4889

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

feat(charges): add backend customer charge override clear#4889
turip merged 3 commits into
mainfrom
feat/charges-clear-override-backend-api

Conversation

@turip

@turip turip commented Aug 8, 2026

Copy link
Copy Markdown
Member

Summary

  • add the backend customer charge override Clear facade
  • remove the API-managed override layer and restore the latest base intent
  • reconcile flat-fee and usage-based lifecycle and realization state
  • preserve hidden base deletion through dedicated transient clear states
  • regenerate Ent status validation for the transient detailed states

Stack

Known follow-up

The generic charge ApplyPatches flow applies the first returned invoice-effect batch but does not yet explicitly resume lifecycle advancement afterward. Invoice-producing clears therefore still depend on the state-machine refactor follow-up before synchronous facade completion can be guaranteed.

Verification

  • production charge packages build
  • focused existing charge and server suites pass
  • no SQL migration is required because detailed statuses are stored in unconstrained varchar columns
  • no new behavioral test cases are included in this split

Greptile Summary

The PR adds a customer charge override-clear facade that restores the latest base intent and reconciles flat-fee and usage-based lifecycle state.

  • Adds clear-override patches, triggers, validation, and facade handling.
  • Adds transient lifecycle states for restoring live and deleted base intents.
  • Reconciles realization, credit, and invoice effects during clear operations.
  • Regenerates persisted status validation and extends focused charge tests.

Confidence Score: 5/5

The PR appears safe to merge because no blocking failure remains within the eligible follow-up review scope.

No blocking failure remains.

Important Files Changed

Filename Overview
openmeter/billing/charges/service/api.go Adds validated, customer-scoped facade handling that dispatches a clear-override patch for supported charge types.
openmeter/billing/charges/meta/patchclearoverride.go Defines the API-only patch that targets and removes the override layer.
openmeter/billing/charges/flatfee/service/creditheninvoice.go Adds flat-fee invoice-backed clear transitions and reconciliation of current realization and invoice effects.
openmeter/billing/charges/flatfee/service/creditsonly.go Adds credit-only flat-fee restoration and deleted-base reconciliation.
openmeter/billing/charges/usagebased/service/creditheninvoice.go Adds invoice-backed usage charge override-clear lifecycle handling.
openmeter/billing/charges/usagebased/service/creditsonly.go Adds credit-only usage charge restoration and realization reconciliation.
openmeter/ent/db/migrate/schema.go Regenerates persisted status validation to admit the new transient detailed states.

Flowchart

%%{init: {'theme': 'neutral'}}%%
flowchart TD
  API[ClearCustomerChargeOverride] --> Patch[PatchClearOverride]
  Patch --> Apply[ApplyPatches]
  Apply --> Trigger[TriggerClearOverride]
  Trigger --> BaseDeleted{Base intent deleted?}
  BaseDeleted -->|No| ActiveClear[active.clear_override]
  BaseDeleted -->|Yes| DeletedClear[deleted.clear_override]
  ActiveClear --> ReconcileLive[Remove override and reconcile realization]
  ReconcileLive --> Destination{Restored timing and amount}
  Destination --> Created[created]
  Destination --> Active[active]
  Destination --> Final[final]
  DeletedClear --> ReconcileDeleted[Remove override and reconcile deletion]
  ReconcileDeleted --> Deleted[deleted]
Loading

Reviews (4): Last reviewed commit: "fix(charges): align clear override revie..." | Re-trigger Greptile

Context used:

Summary by CodeRabbit

  • New Features

    • Added the ability to clear customer charge overrides for supported flat-fee and usage-based charges.
    • Clearing restores the latest reconciled base intent and applies applicable deletion policies.
    • Added handling for deleted charges, invoice-backed fees, credit realizations, and subscription cancellations.
    • Clearing an absent override is safely idempotent.
  • Bug Fixes

    • Preserved original deletion timestamps and improved correction allocation behavior.
  • Documentation

    • Documented override-clearing lifecycle states and billing reconciliation behavior.

@coderabbitai

coderabbitai Bot commented Aug 8, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

Caution

Review failed

The pull request is closed.

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro Plus

Run ID: 466728b1-ac8f-4a68-bd66-7209d4248a18

📥 Commits

Reviewing files that changed from the base of the PR and between 4fa7c53 and 9a0dee7.

⛔ Files ignored due to path filters (3)
  • openmeter/ent/db/chargeflatfee/chargeflatfee.go is excluded by !**/ent/db/**
  • openmeter/ent/db/chargeusagebased/chargeusagebased.go is excluded by !**/ent/db/**
  • openmeter/ent/db/migrate/schema.go is excluded by !**/ent/db/**
📒 Files selected for processing (26)
  • openmeter/billing/charges/README.md
  • openmeter/billing/charges/api.go
  • openmeter/billing/charges/api_test.go
  • openmeter/billing/charges/flatfee/adapter/charge.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/flatfee/service/triggers.go
  • openmeter/billing/charges/flatfee/statemachine.go
  • openmeter/billing/charges/meta/patch.go
  • openmeter/billing/charges/meta/patchclearoverride.go
  • openmeter/billing/charges/meta/patchclearoverride_test.go
  • openmeter/billing/charges/meta/triggers.go
  • openmeter/billing/charges/models/creditrealization/correction_test.go
  • openmeter/billing/charges/models/creditrealization/realizations.go
  • openmeter/billing/charges/service/api.go
  • openmeter/billing/charges/service/api_test.go
  • openmeter/billing/charges/usagebased/adapter/charge.go
  • openmeter/billing/charges/usagebased/service/creditheninvoice.go
  • openmeter/billing/charges/usagebased/service/creditsonly.go
  • openmeter/billing/charges/usagebased/service/statemachine.go
  • openmeter/billing/charges/usagebased/service/triggers.go
  • openmeter/billing/charges/usagebased/statemachine.go
  • openmeter/billing/worker/subscriptionsync/service/creditsonly_test.go
  • openmeter/billing/worker/subscriptionsync/service/sync_credittheninvoice_test.go
  • openmeter/server/server_test.go

📝 Walkthrough

Walkthrough

The change adds customer charge override clearing for flat-fee and usage-based charges. It adds API and patch contracts, lifecycle states, reconciliation behavior, deletion timestamp preservation, and tests for active, deleted, invoice-backed, and subscription-synchronized charges.

Changes

Customer charge override clearing

Layer / File(s) Summary
Clear-override contracts and API
openmeter/billing/charges/api.go, openmeter/billing/charges/meta/*, openmeter/billing/charges/service/api.go
Adds the clear-override input, patch type, trigger, statuses, validation, and customer service operation.
Flat-fee lifecycle handling
openmeter/billing/charges/flatfee/*
Clears active and deleted overrides, resolves charge states, cancels current realizations, updates gathering lines, and preserves deletion timestamps.
Usage-based lifecycle handling
openmeter/billing/charges/usagebased/*
Clears overrides, restores deleted base intents, rejects unsupported realization states, and reconciles deleted charges.
Reconciliation and end-to-end validation
openmeter/billing/charges/models/creditrealization/*, openmeter/billing/charges/service/api_test.go, openmeter/billing/worker/subscriptionsync/service/*_test.go, openmeter/billing/charges/README.md
Adds correction handling and coverage for active, deleted, invoice-backed, usage-based, and subscription-synchronized override clearing.

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

Sequence Diagram(s)

sequenceDiagram
  participant Client
  participant CustomerChargeService
  participant ChargeStateMachine
  participant ChargeAdapter
  participant Reconciliation
  Client->>CustomerChargeService: ClearCustomerChargeOverride
  CustomerChargeService->>ChargeStateMachine: Apply clear_override patch
  ChargeStateMachine->>ChargeAdapter: Remove override layer
  ChargeAdapter-->>ChargeStateMachine: Restored base intent
  ChargeStateMachine->>Reconciliation: Reconcile realization and gathering state
  Reconciliation-->>CustomerChargeService: Updated charge
  CustomerChargeService-->>Client: Charge response
Loading

Possibly related PRs

Suggested reviewers: tothandras, galexihu, borosr

✨ Finishing Touches
📝 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-clear-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 2 times, most recently from 0e3b6c8 to fb06895 Compare August 9, 2026 11:14
@turip
turip force-pushed the feat/charges-clear-override-backend-api branch from f7feac1 to fb87881 Compare August 9, 2026 11:17
@turip
turip force-pushed the feat/charges-set-override-backend-api branch from fb06895 to 49cbc32 Compare August 11, 2026 14:08
Base automatically changed from feat/charges-set-override-backend-api to main August 12, 2026 08:44
@turip turip added release-note/feature Release note: Exciting New Features area/billing labels Aug 12, 2026
@turip
turip force-pushed the feat/charges-clear-override-backend-api branch from cbfe885 to 9a0dee7 Compare August 12, 2026 08:56
@turip
turip marked this pull request as ready for review August 12, 2026 10:52
@turip
turip requested a review from a team as a code owner August 12, 2026 10:52
@turip
turip merged commit 2064f94 into main Aug 12, 2026
27 checks passed
@turip
turip deleted the feat/charges-clear-override-backend-api branch August 12, 2026 10:52
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