Repository navigation
refactor: use invoicing api for creating upcoming changes - #3773
Conversation
📝 WalkthroughWalkthroughPre-generates ULID IDs for pending invoice lines, appends new lines to gathering invoices and updates them in-place (removing reload), maps final lines by those IDs using lo.SliceToMap, expands invoice fetches to include Lines, and timestamps lines when soft-deleting them on invoice deletion. Changes
Sequence Diagram(s)sequenceDiagram
participant Caller
participant InvoiceService
participant Repo/DB
participant Clock
Caller ->> InvoiceService: Request to create pending invoice lines
InvoiceService ->> InvoiceService: Pre-generate ULID IDs for lines
InvoiceService ->> Repo/DB: Fetch/Upsert gathering invoice (Expand Lines)
InvoiceService ->> InvoiceService: Append new lines to invoice.Lines
InvoiceService ->> Repo/DB: Update gathering invoice (with new Lines)
Repo/DB -->> InvoiceService: Updated gathering invoice (with Lines)
InvoiceService ->> InvoiceService: Map lines by ID (lo.SliceToMap)
InvoiceService ->> Caller: Return created/selected lines
alt invoice deleted with remaining lines
InvoiceService ->> Clock: Now()
InvoiceService ->> Repo/DB: Timestamp lines as deleted
end
Estimated code review effort🎯 4 (Complex) | ⏱️ ~45 minutes Possibly related PRs
Suggested reviewers
🚥 Pre-merge checks | ✅ 3✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing touches
🧪 Generate unit tests (beta)
📜 Recent review detailsConfiguration used: Path: .coderabbit.yaml Review profile: CHILL Plan: Pro 📒 Files selected for processing (1)
🧰 Additional context used📓 Path-based instructions (1)**/*.go⚙️ CodeRabbit configuration file
Files:
🧬 Code graph analysis (1)openmeter/billing/service/invoiceline.go (5)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (8)
🔇 Additional comments (5)
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 |
To make billing more uniform we should start using the Invoice object to manage specific invoices (we are already doing this on the subscription sync side). This patch makes sure that we are using the invoicing API for this instead of bypassing it and directly manipulating lines.
d9f97cf to
b418f68
Compare
There was a problem hiding this comment.
Actionable comments posted: 0
🧹 Nitpick comments (2)
openmeter/billing/service/invoiceline.go (2)
118-136: Two sequentialUpdateInvoicecalls - intentional?I see you're doing:
UpdateInvoice(line 122) to persist the appended linesCalculateGatheringInvoice(line 129)UpdateInvoiceagain (line 133) to persist calculation resultsIf the calculation doesn't depend on DB-generated values from the first update, you might be able to consolidate this into a single DB round-trip by calculating before the first persist. Not a blocker, but worth considering if this path gets hit frequently.
238-247: Comment could be clearer about the intent.The current comment says "to prevent them from being associated with the deleted invoice" but we're actually restoring the invoice here (clearing its
DeletedAt). The logic is sound - cleaning up stale lines when reusing a previously-deleted gathering invoice - but the explanation is a bit confusing.📝 Suggested comment clarification
- // If the invoice was deleted, but has lines, we need to delete those lines to prevent - // them from being associated with the deleted invoice. + // If the invoice was previously deleted but still has non-deleted lines, + // soft-delete them to start fresh when reusing this gathering invoice.
📜 Review details
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
openmeter/billing/service/invoiceline.go
🧰 Additional context used
📓 Path-based instructions (1)
**/*.go
⚙️ CodeRabbit configuration file
**/*.go: In general when reviewing the Golang code make readability and maintainability a priority, even potentially suggest restructuring the code to improve them.Performance should be a priority in critical code paths. Anything related to event ingestion, message processing, database operations (regardless of database) should be vetted for potential performance bottlenecks.
Files:
openmeter/billing/service/invoiceline.go
🧬 Code graph analysis (1)
openmeter/billing/service/invoiceline.go (3)
openmeter/billing/invoiceline.go (2)
Lines(557-557)Line(231-242)openmeter/billing/service/lineservice/service.go (2)
Lines(211-211)Line(194-209)openmeter/billing/invoice.go (1)
InvoiceExpand(220-229)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (7)
- GitHub Check: Quickstart
- GitHub Check: E2E
- GitHub Check: Code Generators
- GitHub Check: Lint
- GitHub Check: Test
- GitHub Check: Build
- GitHub Check: Analyze (go)
🔇 Additional comments (5)
openmeter/billing/service/invoiceline.go (5)
8-8: LGTM on the new imports!Nice choices -
ulid/v2for sortable unique IDs andclockfor testable time handling are solid additions.Also applies to: 15-15
144-153: LGTM on thelo.SliceToMaprefactor.Clean way to map lines by ID for the lookup. The iteration over
linesToCreate(line 149) correctly ensures we only return the lines created in this operation.
194-196: Good catch includingLines: truein the expand.Essential for the new flow - without this, appending to an existing gathering invoice would effectively discard its current lines.
64-66: Nice approach pre-assigning line IDs with ULID.This makes tracking lines through the create flow much cleaner. Great news on the consistency front too —
ulid.Make().String()is the standard pattern across the entire billing service, so you're in good company here. Keeping it uniform with the rest of the codebase.
120-120: No issue here—Append already handles nil safely.The method has a pointer receiver and defensively calls
OrEmpty()to handle uninitialized state, sogatheringInvoice.Lines.Append(...)will work fine even for brand new invoices.
Overview
To make billing more uniform we should start using the Invoice object to manage specific invoices (we are already doing this on the subscription sync side).
This patch makes sure that we are using the invoicing API for this instead of bypassing it and directly manipulating lines.
Note: no additional tests, as we have ample of tests for this codepath.
Notes for reviewer
Summary by CodeRabbit
✏️ Tip: You can customize this high-level summary in your review settings.