Repository navigation
fix: do not sync old invoices - #3031
Conversation
With the recent changes the invoice at is not truncated anymore (which is a good behavior). Given previously it was truncated the sync logic tries to reconcile the old already issued invoices. This is done by checking the total sum and if there is any difference a warning is added. Given the invoice at is not used for non-gathering invoices this is just a waste of resources. This patch makes sure that we are not managing the invoice at fields for lines that are not on gathering invoices. Going forward we should drop the invoice_at from the lines on the non-gathering invoices.
📝 WalkthroughWalkthroughThe changes introduce a new Changes
Warning There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure. 🔧 golangci-lint (1.64.8)Error: you are using a configuration file for golangci-lint v2 with golangci-lint v1: please use golangci-lint v2 ✨ Finishing Touches
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. 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
SupportNeed help? Create a ticket on our support page for assistance with any issues or questions. Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this comment.
Actionable comments posted: 0
🔭 Outside diff range comments (1)
openmeter/billing/worker/subscription/sync_test.go (1)
4019-4033: Add length guard to avoid panic when assertingInvoiceAtslices
expectLinesindexes intoexpectedLine.InvoiceAt.OrEmpty()[idx]without checking that the slice is at leastlen(childIDs)long.
If a test writer accidentally supplies fewer timestamps than periods, the test suite will panic with an out-of-bounds instead of producing a clean assertion failure.@@ func (s *SubscriptionHandlerTestSuite) expectLines( ... ) { - for idx, childID := range childIDs { + // Ensure we have matching InvoiceAt data before the per-item loop + if expectedLine.InvoiceAt.IsPresent() { + s.Equal(len(childIDs), len(expectedLine.InvoiceAt.OrEmpty()), + "InvoiceAt entry count must match childIDs for matcher %+v", expectedLine.Matcher) + } + + for idx, childID := range childIDs {This simple guard turns a panic into an assertion with a helpful message, improving debuggability.
Also applies to: 4063-4067
🧹 Nitpick comments (2)
openmeter/billing/worker/subscription/sync_test.go (1)
4224-4239: Remove the duplicated word in helper name
generateDailygenerateInvoiceAtTimestampscontains the word “generate” twice, which is a typo and makes the method harder to scan for.
A concise name such asgenerateDailyInvoiceAtTimestampsis clearer and keeps the helper list tidy.-func (s *SubscriptionHandlerTestSuite) generateDailygenerateInvoiceAtTimestamps(startStr string, n int) mo.Option[[]time.Time] { +func (s *SubscriptionHandlerTestSuite) generateDailyInvoiceAtTimestamps(startStr string, n int) mo.Option[[]time.Time] {Remember to update the four call-sites further up in this file (search for
generateDailygenerateInvoiceAtTimestamps).
No functional impact, purely readability.openmeter/billing/worker/subscription/sync.go (1)
182-194: Consider pagination for customers with many invoices.The implementation is correct, but fetching all invoices for a customer could be a performance concern for customers with a large invoice history. Consider adding pagination or limiting the query to recent/relevant invoices if this becomes a bottleneck.
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (2)
openmeter/billing/worker/subscription/sync.go(11 hunks)openmeter/billing/worker/subscription/sync_test.go(45 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (10)
- GitHub Check: Developer environment
- GitHub Check: Commit hooks
- GitHub Check: CI
- GitHub Check: E2E
- GitHub Check: Quickstart
- GitHub Check: Test
- GitHub Check: Lint
- GitHub Check: Migration Checks
- GitHub Check: Build
- GitHub Check: Analyze (go)
🔇 Additional comments (2)
openmeter/billing/worker/subscription/sync.go (2)
66-77: LGTM! Good defensive programming.The
InvoiceByIDtype andIsGatheringInvoicemethod are well-implemented. Returningtruewhen an invoice is not found is a safe default that prevents accidental modifications to unknown invoices.
647-649: Excellent implementation of the PR objective.The conditional updates to
InvoiceAtonly for gathering invoices are implemented consistently across all three locations. This effectively prevents unnecessary updates to finalized invoices, achieving the performance optimization goal.Also applies to: 734-736, 767-769
With the recent changes the invoice at is not truncated anymore (which is a good behavior).
Given previously it was truncated the sync logic tries to reconcile the old already issued invoices. This is done by checking the total sum and if there is any difference a warning is added.
Given the invoice at is not used for non-gathering invoices this is just a waste of resources. This patch makes sure that we are not managing the invoice at fields for lines that are not on gathering invoices.
Going forward we should drop the invoice_at from the lines on the non-gathering invoices.
Summary by CodeRabbit
Bug Fixes
Tests