Skip to content

Support flat fee invoice line delete overrides - #4586

Merged
turip merged 1 commit into
mainfrom
feat/flat-fee-line-delete-overrides
Jun 27, 2026
Merged

turip merged 1 commit into
mainfrom
feat/flat-fee-line-delete-overrides

Conversation

@turip

@turip turip commented Jun 27, 2026 •

Copy link
Copy Markdown
Member

Summary

This PR extends the flat-fee invoice-line override flow so manual API deletes are handled through the same charge-owned state-machine and line-engine boundary as manual edits.

When a customer deletes a mutable charge-backed invoice line, the flat-fee state machine records the delete on the override layer and emits invoice patches. The line engine consumes those patches locally, validates that they only affect the edited line, and reuses the standard-line deletion cleanup path for standard invoices so credit corrections, charge-owned detailed-line cleanup, and realization-run deletion remain consistent.

The PR also adds subscription-sync coverage for gathering-line and standard-line delete behavior, including promotional-credit ledger expectations for standard invoices, and updates the charges skill with the shared flat-fee/usage-based line-engine rules.

Validation

  • direnv exec . env POSTGRES_HOST=127.0.0.1 go test -tags=dynamic -count=1 -run 'TestCreditThenInvoiceScenarios/Test(GatheringManual(Edit|Delete)Sync|StandardInvoiceManual(Edit|Delete)Sync)' ./openmeter/billing/worker/subscriptionsync/service
  • direnv exec . go test -tags=dynamic -count=1 ./openmeter/billing/charges/flatfee/service
  • direnv exec . go vet ./openmeter/billing/charges/flatfee/service ./openmeter/billing/worker/subscriptionsync/service

Summary by CodeRabbit

  • Documentation
    • Updated the charges guidance to clarify charge lifecycle/settlement semantics across adapters, expansions, billing line engine wiring, and realization flows.
  • Bug Fixes
    • Improved charge deletion handling to use consistent deletion timestamps and correctly create/update deleted override records when applicable.
    • Enabled manual deletion of flat-fee credit-then-invoice invoice lines, including correct cleanup and balance behavior.
    • Adjusted how standard-line deletion cleanup is applied to ensure stable invoice state.
  • Tests
    • Added subscription sync coverage for manual deletion of both gathering and standard invoice lines, ensuring deleted lines are preserved across resync.

@turip
turip requested a review from a team as a code owner June 27, 2026 06:04
@coderabbitai

coderabbitai Bot commented Jun 27, 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

Run ID: 9ccf6f9f-2ffc-4062-9007-57be2e9da396

📥 Commits

Reviewing files that changed from the base of the PR and between 0a8d4c4 and a708fe2.

📒 Files selected for processing (4)
  • .agents/skills/charges/SKILL.md
  • openmeter/billing/charges/flatfee/service/creditheninvoice.go
  • openmeter/billing/charges/flatfee/service/lineengine.go
  • openmeter/billing/worker/subscriptionsync/service/sync_credittheninvoice_test.go
🚧 Files skipped from review as they are similar to previous changes (4)
  • openmeter/billing/charges/flatfee/service/creditheninvoice.go
  • openmeter/billing/charges/flatfee/service/lineengine.go
  • .agents/skills/charges/SKILL.md
  • openmeter/billing/worker/subscriptionsync/service/sync_credittheninvoice_test.go

📝 Walkthrough

Walkthrough

Updates charge guidance, changes credit-then-invoice deletion to create deleted override intents when needed, routes mutable invoice-line deletes through shared cleanup, and adds subscription-sync coverage for gathering and standard invoice manual deletions.

Changes

Credit-then-invoice manual delete flow

Layer / File(s) Summary
Charge guidance updates
.agents/skills/charges/SKILL.md
The charges skill doc updates lifecycle semantics, adapter persistence boundaries, expansion requirements, line-engine behavior, zero-accrual handling, and flat-fee editing rules.
Deleted override persistence
openmeter/billing/charges/flatfee/service/creditheninvoice.go
DeleteCharge now creates a deleted override intent when deleting the override target without an active override layer, and otherwise updates the target intent with one shared deletion timestamp.
Manual delete API handling
openmeter/billing/charges/flatfee/service/lineengine.go
OnMutableInvoiceLinesEditedViaAPI now accepts deleted lines, validates manual deletes, triggers charge deletion, and dispatches cleanup for gathering-line and standard-line patch operations. OnMutableStandardLinesDeletedBySystem now delegates to shared cleanup.
Manual-delete resync tests
openmeter/billing/worker/subscriptionsync/service/sync_credittheninvoice_test.go
The subscription-sync tests cover gathering-line deletion and standard invoice deletion, then resync the subscription to verify the deleted state persists and balances remain stable.

Sequence Diagram(s)

sequenceDiagram
  participant OnMutableInvoiceLinesEditedViaAPI
  participant DeleteCharge as CreditThenInvoiceStateMachine.DeleteCharge
  participant CreateChargeOverride as Adapter.CreateChargeOverride
  participant handleManualDeleteInvoicePatches
  participant cleanupDeletedStandardLines

  OnMutableInvoiceLinesEditedViaAPI->>DeleteCharge: meta.TriggerDelete for deleted lines
  DeleteCharge->>CreateChargeOverride: create deleted override intent when no override layer exists
  DeleteCharge-->>OnMutableInvoiceLinesEditedViaAPI: drained invoice patches
  OnMutableInvoiceLinesEditedViaAPI->>handleManualDeleteInvoicePatches: dispatch patch operations
  handleManualDeleteInvoicePatches->>cleanupDeletedStandardLines: clean up standard-line deletes
Loading

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~60 minutes

Possibly related PRs

Suggested labels

kind/feature

Suggested reviewers

  • tothandras
🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly matches the main change: adding delete override support for flat-fee invoice lines.
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.
✨ 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/flat-fee-line-delete-overrides

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.

@greptile-apps

greptile-apps Bot commented Jun 27, 2026 •

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR adds manual delete support for flat-fee invoice-line overrides. The main changes are:

  • Records API deletes as flat-fee override intent deletes.
  • Allows the flat-fee line engine to process deleted mutable invoice lines.
  • Reuses standard-line delete cleanup for charge-owned invoice state.
  • Adds subscription-sync tests for gathering and standard invoice manual deletes.
  • Updates charge guidance for the shared line-engine delete contract.

Confidence Score: 5/5

This looks safe to merge.

  • No blocking issues found in the changed code.

Important Files Changed

Filename Overview
openmeter/billing/charges/flatfee/service/creditheninvoice.go Adds deleted override creation for flat-fee delete handling.
openmeter/billing/charges/flatfee/service/lineengine.go Adds manual delete handling for mutable flat-fee invoice lines.
openmeter/billing/worker/subscriptionsync/service/sync_credittheninvoice_test.go Adds subscription-sync coverage for manual gathering-line and standard-line deletes.
.agents/skills/charges/SKILL.md Documents the flat-fee and usage-based line-engine delete contract.

Reviews (2): Last reviewed commit: "feat: support flat fee invoice line dele..." | Re-trigger Greptile

@turip turip added area/billing release-note/feature Release note: Exciting New Features labels Jun 27, 2026
Base automatically changed from feat/flat-fee-invoice-line-edits to main June 27, 2026 08:24
@turip
turip force-pushed the feat/flat-fee-line-delete-overrides branch from 0a8d4c4 to a708fe2 Compare June 27, 2026 08:30
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