Repository navigation
feat: add gathering/standard invoice update functions - #3860
Conversation
📝 WalkthroughWalkthroughThis PR splits invoice handling by type (Standard vs Gathering), adds type-aware mapping and update flows, introduces generic invoice/line interfaces, and updates adapters, services, HTTP mapping, and many tests to use the new typed APIs and helpers. Changes
Sequence Diagram(s)sequenceDiagram
participant Client
participant Service
participant Adapter
participant DB
Client->>Service: UpdateInvoice(ctx, UpdateInvoiceInput)
Service->>Adapter: GetInvoiceType(ctx, invoiceID)
Adapter->>DB: Query invoice by id/namespace
DB-->>Adapter: Invoice record (status/type)
Adapter-->>Service: InvoiceType
alt Type == Gathering
Service->>Service: UpdateGatheringInvoice(ctx, EditFn)
Service->>DB: Apply gathering-specific merge/update
else Type == Standard
Service->>Service: UpdateStandardInvoice(ctx, EditFn)
Service->>DB: Apply standard-specific merge/update
end
DB-->>Service: Updated invoice
Service-->>Client: Return mapped api.Invoice via MapInvoiceToAPI
Estimated code review effort🎯 4 (Complex) | ⏱️ ~60 minutes Possibly related PRs
Suggested labels
Suggested reviewers
🚥 Pre-merge checks | ✅ 2 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing touches
🧪 Generate unit tests (beta)
No actionable comments were generated in the recent review. 🎉 Tip Issue Planner is now in beta. Read the docs and try it out! Share your feedback on Discord. 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 |
There was a problem hiding this comment.
Actionable comments posted: 3
🤖 Fix all issues with AI agents
In `@openmeter/billing/httpdriver/invoiceline.go`:
- Around line 790-854: mergeGatheringLineFromInvoiceLineReplaceUpdate currently
assigns existing.Price = lo.FromPtr(rateCardParsed.Price) without handling a nil
rateCardParsed.Price, which will overwrite the value with zero instead of
preserving or rejecting it; update the function
mergeGatheringLineFromInvoiceLineReplaceUpdate to check rateCardParsed.Price for
nil and either preserve existing.Price when nil or return a
billing.ValidationError (consistent with
gatheringLineFromInvoiceLineReplaceUpdate behavior), referencing
rateCardParsed.Price and existing.Price to locate the change.
In `@openmeter/billing/service/gatheringinvoice.go`:
- Around line 209-213: GetGatheringInvoiceById is missing input validation like
other methods; call input.Validate() at the start of
Service.GetGatheringInvoiceById and return an empty billing.GatheringInvoice
plus the error if validation fails, before invoking transaction.Run with
s.adapter and s.adapter.GetGatheringInvoiceById, so the method follows the same
validation pattern as ListGatheringInvoices and UpdateGatheringInvoice.
In `@openmeter/billing/service/invoice.go`:
- Around line 820-899: UpdateInvoice ignores input.IncludeDeletedLines so edit
callbacks can't see deleted lines; pass input.IncludeDeletedLines through into
the type-specific update calls by adding IncludeDeletedLines:
input.IncludeDeletedLines to the UpdateGatheringInvoice call
(UpdateGatheringInvoiceInput) and to the UpdateStandardInvoice call
(UpdateStandardInvoiceInput), and also include input.IncludeDeletedLines when
refetching the invoice (e.g., the s.adapter.GetGatheringInvoiceById /
GetStandardInvoiceById call) if those retrieval inputs support that flag so the
returned invoice matches the requested visibility.
🧹 Nitpick comments (4)
openmeter/billing/httpdriver/invoiceline.go (1)
923-968: Gathering line merge flow looks solid, with one note on duplication.The logic here parallels
mergeStandardInvoiceLinesFromAPIclosely, which is fine given the different underlying types. The absence ofSnapshotLineQuantitycalls is correct for gathering invoices since they don't need quantity snapshots.One thing: the two
merge*InvoiceLinesFromAPIfunctions share the same pattern (group by ID → iterate → create-or-merge → mark deletions). If this pattern grows more complex over time, consider extracting the shared orchestration via generics. Not urgent though — the current approach is clear and type-safe.openmeter/billing/adapter/invoice.go (1)
890-894: Useinvoice.Typedirectly instead of inferring from status.The invoice entity already stores a
Typefield (set during creation, validated as immutable). Inferring the type frominvoice.Status == billing.StandardInvoiceStatusGatheringduplicates that source of truth. Other parts of the codebase useinvoice.Typedirectly (e.g., line 282 in gatheringinvoice.go and line 645 in adapter/invoice.go), so using it here would be more consistent and robust.test/billing/invoice_test.go (1)
773-782: Add a test for the genericUpdateInvoicedispatcherRight now the test exercises
UpdateStandardInvoicedirectly, which is great. But the genericUpdateInvoicemethod—which dispatches to eitherUpdateStandardInvoiceorUpdateGatheringInvoicebased on invoice type—doesn't have direct test coverage. This dispatcher has real logic (type checking, EditFn wrapping), so a focused test hitting the StandardInvoice path throughUpdateInvoicewould help keep the public API from drifting.openmeter/billing/httpdriver/invoice.go (1)
505-523: Avoid the extra customer override lookup for standard invoices. UpdateInvoice now always fetches overrides, but the standard mapper doesn’t need them; branching by invoice type keeps the standard path lean and saves a DB roundtrip.💡 Suggested refactor
- genericInvoice, err := invoice.AsGenericInvoice() - if err != nil { - return UpdateInvoiceResponse{}, fmt.Errorf("converting invoice to generic invoice: %w", err) - } - - // TODO: For the V3 api let's make sure that we don't return gathering invoice customer data (or even gathering invoices) - mergedProfile, err := h.service.GetCustomerOverride(ctx, billing.GetCustomerOverrideInput{ - Customer: genericInvoice.GetCustomerID(), - Expand: billing.CustomerOverrideExpand{ - Customer: true, - Apps: true, - }, - }) - if err != nil { - return UpdateInvoiceResponse{}, fmt.Errorf("failed to get customer override: %w", err) - } - - return MapInvoiceToAPI(invoice, mergedProfile.Customer, mergedProfile.MergedProfile) + if invoice.Type() == billing.InvoiceTypeStandard { + stdInvoice, err := invoice.AsStandardInvoice() + if err != nil { + return UpdateInvoiceResponse{}, fmt.Errorf("converting invoice to standard invoice: %w", err) + } + + return MapStandardInvoiceToAPI(stdInvoice) + } + + genericInvoice, err := invoice.AsGenericInvoice() + if err != nil { + return UpdateInvoiceResponse{}, fmt.Errorf("converting invoice to generic invoice: %w", err) + } + + // TODO: For the V3 api let's make sure that we don't return gathering invoice customer data (or even gathering invoices) + mergedProfile, err := h.service.GetCustomerOverride(ctx, billing.GetCustomerOverrideInput{ + Customer: genericInvoice.GetCustomerID(), + Expand: billing.CustomerOverrideExpand{ + Customer: true, + Apps: true, + }, + }) + if err != nil { + return UpdateInvoiceResponse{}, fmt.Errorf("failed to get customer override: %w", err) + } + + return MapInvoiceToAPI(invoice, mergedProfile.Customer, mergedProfile.MergedProfile)
1e38ad7 to
29b59c3
Compare
Overview
Invoice Update has 3 flavors:
Notes for reviewer
Summary by CodeRabbit
New Features
Refactor