Skip to content

refactor: remove generic invoice update path - #4544

Merged
turip merged 1 commit into
mainfrom
refactor/remove-generic-invoice-edit
Jun 22, 2026
Merged

turip merged 1 commit into
mainfrom
refactor/remove-generic-invoice-edit

Conversation

@turip

@turip turip commented Jun 19, 2026 •

Copy link
Copy Markdown
Member

Objective: have exactly one update path for invoices.

Compatibility considerations:

  • HTTP invoice updates still dispatch by invoice type before using the typed update services.
  • Typed update services perform the locking and refetching before edit callbacks run.
  • Gathering invoice updates return a DB-backed invoice shape with lines and available actions preserved.
  • Existing include-deleted-lines behavior remains covered through the typed update services.

Summary by CodeRabbit

  • Refactor
    • Invoice update operations now use type-specific methods for gathering and standard invoices rather than a unified approach
    • Gathering invoice updates now return the updated invoice object alongside result status

@coderabbitai

coderabbitai Bot commented Jun 19, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

Note

Currently processing new changes in this PR. This may take a few minutes, please wait...

⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 6c9ac96f-b88b-45cc-92a4-aa0043374c26

📥 Commits

Reviewing files that changed from the base of the PR and between 5176487 and 65e6b8b.

📒 Files selected for processing (11)
  • openmeter/billing/charges/invoiceupdater/invoiceupdate.go
  • openmeter/billing/httpdriver/invoice.go
  • openmeter/billing/invoice.go
  • openmeter/billing/service.go
  • openmeter/billing/service/gatheringinvoice.go
  • openmeter/billing/service/invoice.go
  • openmeter/billing/worker/subscriptionsync/service/reconciler/invoiceupdater/invoiceupdate.go
  • openmeter/billing/worker/subscriptionsync/service/sync_credittheninvoice_test.go
  • openmeter/billing/worker/subscriptionsync/service/sync_test.go
  • openmeter/server/server_test.go
  • test/billing/invoice_test.go
 _________________________________________________
< Code Wars Episode VI: Return of the Unit Tests. >
 -------------------------------------------------
  \
   \   (\__/)
       (•ㅅ•)
       /   づ
✨ 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 refactor/remove-generic-invoice-edit

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 and usage tips.

@turip
turip marked this pull request as ready for review June 19, 2026 13:16
@turip
turip requested a review from a team as a code owner June 19, 2026 13:16
@turip turip added release-note/misc Miscellaneous changes area/billing labels Jun 19, 2026
@turip
turip enabled auto-merge (squash) June 19, 2026 13:59
@greptile-apps

greptile-apps Bot commented Jun 21, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR removes the generic UpdateInvoice service method and the corresponding UpdateInvoiceInput type, consolidating all invoice update paths into the existing typed methods (UpdateGatheringInvoice, UpdateStandardInvoice). UpdateGatheringInvoice is promoted from returning only error to returning (GatheringInvoice, error), with a post-update DB re-fetch added inside its transaction to supply callers with a fully-populated invoice.

  • HTTP handler: the update endpoint now does a lightweight non-locking GetInvoiceById to determine invoice type, then dispatches to updateGatheringInvoice or updateStandardInvoice helper functions; the typed service methods lock and refetch before running the edit callback.
  • UpdateGatheringInvoice service: returns the updated invoice, with GatheringInvoiceExpandAvailableActions now included in both the pre-edit fetch and the post-update re-fetch (previously only the post-update fetch in UpdateInvoice included it).
  • All callers (reconciler, charges updater, tests) updated to use _, err := or the new direct typed APIs.

Confidence Score: 4/5

The refactoring is mechanically correct — all callers are updated, the auto-delete path continues to work (GetGatheringInvoiceById does not filter soft-deleted records), and the two-step type-dispatch in the HTTP handler is safe because invoice types are immutable.

The consolidation of the generic update path into typed services is clean and well-tested. The only issue is that GatheringInvoiceExpandAvailableActions is now computed for the pre-edit fetch inside UpdateGatheringInvoice when it was not before, adding overhead to every gathering invoice update without functional benefit during the edit phase.

openmeter/billing/service/gatheringinvoice.go — the shared expands variable causes AvailableActions to be fetched twice per update call.

Important Files Changed

Filename Overview
openmeter/billing/service/gatheringinvoice.go UpdateGatheringInvoice changed to return (GatheringInvoice, error); now does a final DB re-fetch inside the transaction to return a fully-populated invoice. Pre-edit fetch shares the same expands as the post-update fetch, including the unnecessary GatheringInvoiceExpandAvailableActions for the edit phase.
openmeter/billing/httpdriver/invoice.go HTTP handler now dispatches by invoice type via a non-locking GetInvoiceById, then delegates to typed update helpers; the approach is correctly documented and the typed services lock-and-refetch before editing.
openmeter/billing/service/invoice.go Removes the 95-line generic UpdateInvoice implementation entirely; clean deletion with no residual dead code.
openmeter/billing/service.go Removes UpdateInvoice from InvoiceService and updates UpdateGatheringInvoice signature in GatheringInvoiceService to return (GatheringInvoice, error).
openmeter/billing/charges/invoiceupdater/invoiceupdate.go Adapts to new UpdateGatheringInvoice signature with _, err := to ignore the returned invoice value; correct and minimal change.
openmeter/billing/worker/subscriptionsync/service/reconciler/invoiceupdater/invoiceupdate.go Same _, err := adaptation as charges/invoiceupdater; return value correctly discarded by the reconciler which only cares about error propagation.
test/billing/invoice_test.go Tests for UpdateInvoice converted to call UpdateGatheringInvoice/UpdateStandardInvoice directly; simplified by removing redundant type-conversion boilerplate.

Sequence Diagram

%%{init: {'theme': 'neutral'}}%%
sequenceDiagram
    participant Client
    participant HTTPHandler
    participant BillingService
    participant Adapter

    Client->>HTTPHandler: "PATCH /invoices/{id}"
    HTTPHandler->>BillingService: GetInvoiceById(id) [no lock]
    BillingService-->>HTTPHandler: Invoice (type only)

    alt Gathering Invoice
        HTTPHandler->>BillingService: UpdateGatheringInvoice(id, EditFn)
        BillingService->>Adapter: GetGatheringInvoiceById(id) [pre-tx, for customer lock]
        BillingService->>BillingService: transactionForInvoiceManipulation (acquires customer lock)
        BillingService->>Adapter: GetGatheringInvoiceById(id, expands+AvailableActions) [pre-edit fetch]
        BillingService->>BillingService: "EditFn(&invoice)"
        BillingService->>BillingService: Validate + CalculateGatheringInvoice
        BillingService->>Adapter: UpdateGatheringInvoice(invoice)
        opt all lines deleted
            BillingService->>Adapter: DeleteGatheringInvoices(id)
        end
        BillingService->>Adapter: GetGatheringInvoiceById(id, expands+AvailableActions) [post-update fetch]
        BillingService-->>HTTPHandler: GatheringInvoice
    else Standard Invoice
        HTTPHandler->>BillingService: UpdateStandardInvoice(id, EditFn)
        BillingService->>BillingService: transactionForInvoiceManipulation (acquires customer lock)
        BillingService->>Adapter: GetStandardInvoiceById(id, expands) [pre-edit fetch]
        BillingService->>BillingService: "EditFn(&invoice)"
        BillingService->>Adapter: UpdateStandardInvoice(invoice)
        BillingService-->>HTTPHandler: StandardInvoice
    end

    HTTPHandler-->>Client: 200 Invoice JSON
Loading
%%{init: {'theme': 'base', 'themeVariables': {"darkMode": true, "background": "#0d1117", "primaryColor": "#21262d", "primaryTextColor": "#e6edf3", "primaryBorderColor": "#8b949e", "lineColor": "#8b949e", "textColor": "#e6edf3", "edgeLabelBackground": "#161b22", "actorBkg": "#21262d", "actorBorder": "#8b949e", "actorTextColor": "#e6edf3", "actorLineColor": "#8b949e", "signalColor": "#8b949e", "signalTextColor": "#e6edf3", "noteBkgColor": "#373320", "noteBorderColor": "#d4a72c", "noteTextColor": "#f0e6c0", "labelBoxBkgColor": "#21262d", "labelBoxBorderColor": "#8b949e", "labelTextColor": "#e6edf3", "loopTextColor": "#e6edf3", "activationBkgColor": "#30363d", "activationBorderColor": "#8b949e"}}}%%
sequenceDiagram
    participant Client
    participant HTTPHandler
    participant BillingService
    participant Adapter

    Client->>HTTPHandler: "PATCH /invoices/{id}"
    HTTPHandler->>BillingService: GetInvoiceById(id) [no lock]
    BillingService-->>HTTPHandler: Invoice (type only)

    alt Gathering Invoice
        HTTPHandler->>BillingService: UpdateGatheringInvoice(id, EditFn)
        BillingService->>Adapter: GetGatheringInvoiceById(id) [pre-tx, for customer lock]
        BillingService->>BillingService: transactionForInvoiceManipulation (acquires customer lock)
        BillingService->>Adapter: GetGatheringInvoiceById(id, expands+AvailableActions) [pre-edit fetch]
        BillingService->>BillingService: "EditFn(&invoice)"
        BillingService->>BillingService: Validate + CalculateGatheringInvoice
        BillingService->>Adapter: UpdateGatheringInvoice(invoice)
        opt all lines deleted
            BillingService->>Adapter: DeleteGatheringInvoices(id)
        end
        BillingService->>Adapter: GetGatheringInvoiceById(id, expands+AvailableActions) [post-update fetch]
        BillingService-->>HTTPHandler: GatheringInvoice
    else Standard Invoice
        HTTPHandler->>BillingService: UpdateStandardInvoice(id, EditFn)
        BillingService->>BillingService: transactionForInvoiceManipulation (acquires customer lock)
        BillingService->>Adapter: GetStandardInvoiceById(id, expands) [pre-edit fetch]
        BillingService->>BillingService: "EditFn(&invoice)"
        BillingService->>Adapter: UpdateStandardInvoice(invoice)
        BillingService-->>HTTPHandler: StandardInvoice
    end

    HTTPHandler-->>Client: 200 Invoice JSON
Loading

Fix All in Claude Code

Reviews (1): Last reviewed commit: "refactor: remove generic invoice update ..." | Re-trigger Greptile

Comment thread openmeter/billing/httpdriver/invoice.go Resolved
@turip
turip merged commit 277f8d3 into main Jun 22, 2026
26 of 30 checks passed
@turip
turip deleted the refactor/remove-generic-invoice-edit branch June 22, 2026 09:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants