Repository navigation
chore: add lineupdater - #4300
Conversation
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Path: .coderabbit.yaml Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (3)
📝 WalkthroughWalkthroughAdds an invoice patching system: a Patch abstraction for line operations, flat-fee helpers for per-unit amounts, and an Updater service that parses patches, provisions upcoming lines, fetches invoices, and applies updates to gathering, mutable standard, or immutable invoices with validation and logging. ChangesInvoice Patching System
Sequence DiagramsequenceDiagram
participant Client
participant Updater
participant PatchParser
participant BillingService
participant InvoiceStore
Client->>Updater: ApplyPatches(customerID, patches)
Updater->>PatchParser: parsePatches(patches)
PatchParser-->>Updater: patchesParsed
Updater->>BillingService: provisionUpcomingLines(customerID, currencies)
BillingService-->>Updater: provisioned lines / pending IDs
Updater->>BillingService: listInvoicesByID(invoiceIDs)
BillingService-->>Updater: invoices map (by ID)
Updater->>InvoiceStore: determine invoice type (gathering/standard)
alt Gathering
Updater->>InvoiceStore: updateGatheringInvoice(invoiceID, patches)
InvoiceStore-->>Updater: OK
else Mutable Standard
Updater->>InvoiceStore: updateMutableStandardInvoice(invoiceID, patches)
InvoiceStore->>BillingService: upsertSplitLineGroups(...)
BillingService-->>InvoiceStore: OK
InvoiceStore-->>Updater: OK
else Immutable
Updater->>InvoiceStore: updateImmutableInvoice(invoiceID, patches)
InvoiceStore->>InvoiceStore: validate lines, merge validation issues
InvoiceStore-->>Updater: upserted validation issues (if any)
end
Updater-->>Client: result (error or nil)
Estimated code review effort🎯 4 (Complex) | ⏱️ ~45 minutes Suggested labels
Suggested reviewers
🚥 Pre-merge checks | ✅ 3 | ❌ 2❌ Failed checks (1 warning, 1 inconclusive)
✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Warning Review ran into problems🔥 ProblemsGit: Failed to clone repository. Please run the 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: 4
🧹 Nitpick comments (1)
openmeter/billing/charges/invoiceupdater/invoiceupdate.go (1)
318-321: ⚡ Quick win
return erris alwaysnilhere — let's say what we mean.By the time execution reaches line 320, the outer
errfromUpdateStandardInvoicewas already checked at line 294, and the innererrfromDeleteInvoiceis shadowed inside theifblock (declared with:=on line 303). So this is effectivelyreturn nildressed up to look like error propagation. A quick swap makes the control flow obvious.♻️ Suggested change
- return err + return nil }🤖 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/invoiceupdater/invoiceupdate.go` around lines 318 - 321, The final "return err" in UpdateStandardInvoice is always nil because the inner DeleteInvoice call used ":=" which shadowed the outer err; change the DeleteInvoice invocation so it assigns to the existing err (use "=" instead of ":=") or simply return the DeleteInvoice error directly (e.g., if err = i.DeleteInvoice(...); err != nil { return err }) so the function propagates the actual DeleteInvoice error instead of returning nil.
🤖 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/invoiceupdater/feehelper.go`:
- Line 33: Update the misleading error text in
openmeter/billing/charges/invoiceupdater/feehelper.go where fmt.Errorf("line
misses usage based metadata") is returned alongside alpacadecimal.Zero; change
the message to clearly reference flat-fee metadata (e.g., "line missing flat-fee
metadata" or similar) so it accurately reflects that these helpers are for
flat-fee lines—apply the same replacement to the other fmt.Errorf occurrence in
the same file.
In `@openmeter/billing/charges/invoiceupdater/invoiceupdate.go`:
- Around line 83-86: Remove the dead split-line-group scaffolding: delete the
splitLineGroups field usage (patchesParsed.splitLineGroups and
splitLineGroupPatches), the ApplyPatches call to upsertSplitLineGroups and the
upsertSplitLineGroups method itself, and remove the unused models import if
nothing else references it; search for symbols upsertSplitLineGroups,
splitLineGroups, splitLineGroupPatches and eliminate their declarations/usages
so the file no longer contains unreachable split-line-group handling
scaffolding.
- Around line 415-428: The comparison currently uses
lo.FromPtr(existingLine.UsageBased.Quantity) which treats a nil Quantity as
zero; update the check in the invoice update flow (around SnapshotLineQuantity
call and the existingLine / targetStateWithUpdatedQty variables) to explicitly
treat a nil existingLine.UsageBased.Quantity as a change: first check if
existingLine.UsageBased.Quantity == nil and append the validation issue if so,
otherwise compare the dereferenced value with
targetStateWithUpdatedQty.UsageBased.Quantity (e.g. if existingQty :=
existingLine.UsageBased.Quantity; existingQty == nil ||
!targetStateWithUpdatedQty.UsageBased.Quantity.Equal(*existingQty) {
newValidationIssueOnLine(...) } ), removing lo.FromPtr usage so nil is not
silently coalesced.
- Around line 44-81: Collect all invoice IDs from
patchesParsed.updatedLinesByInvoiceID before the loop, call the billing service
batch fetch (use u.billingService.ListInvoices or ListStandardInvoices with the
IDs filter) to retrieve all invoices in one round-trip, build a map[id]invoice
from the returned slice, and then in the existing loop replace the per-iteration
GetInvoiceById call with a lookup into that map; if an invoice is missing return
an error similar to the current GetInvoiceById error path. Keep the rest of the
logic intact (use invoice.Type(), AsStandardInvoice(), and call
updateGatheringInvoice, updateMutableStandardInvoice, updateImmutableInvoice)
but use the batched-result map instead of calling
u.billingService.GetInvoiceById for each invoice.
---
Nitpick comments:
In `@openmeter/billing/charges/invoiceupdater/invoiceupdate.go`:
- Around line 318-321: The final "return err" in UpdateStandardInvoice is always
nil because the inner DeleteInvoice call used ":=" which shadowed the outer err;
change the DeleteInvoice invocation so it assigns to the existing err (use "="
instead of ":=") or simply return the DeleteInvoice error directly (e.g., if err
= i.DeleteInvoice(...); err != nil { return err }) so the function propagates
the actual DeleteInvoice error instead of returning nil.
🪄 Autofix (Beta)
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
Run ID: 76d409bb-1973-4e07-823f-7d319768eddb
📒 Files selected for processing (3)
openmeter/billing/charges/invoiceupdater/feehelper.goopenmeter/billing/charges/invoiceupdater/invoiceupdate.goopenmeter/billing/charges/invoiceupdater/patch.go
Overview
Copy lineupdater from subscription sync and drop the splitlinegroup handling.
This will be used later to change charges (that's why we are not reusing the previous version, which is on subscription sync granuality).
Notes for reviewer
Summary by CodeRabbit