Skip to content

fix: do not sync old invoices - #3031

Merged
turip merged 1 commit into
mainfrom
fix/do-not-sync-old-invoices
Jun 24, 2025
Merged

turip merged 1 commit into
mainfrom
fix/do-not-sync-old-invoices

Conversation

@turip

@turip turip commented Jun 24, 2025 •

Copy link
Copy Markdown
Member

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

    • Improved handling of invoice updates to ensure invoice dates are only modified for invoices that are still being gathered, preventing changes to finalized invoices.
  • Tests

    • Updated test cases to standardize and improve the handling of optional invoice date fields, enhancing test reliability and consistency.

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.
@turip
turip requested a review from a team as a code owner June 24, 2025 09:21
@turip turip added release-note/bug-fix Release note: Bug Fixes area/billing labels Jun 24, 2025
@coderabbitai

coderabbitai Bot commented Jun 24, 2025 •

Copy link
Copy Markdown
Contributor
📝 Walkthrough

Walkthrough

The changes introduce a new InvoiceByID type to track invoice statuses and update related patch generation logic to conditionally modify invoice dates only for invoices in a "gathering" state. Test code was refactored to represent InvoiceAt fields as optional values using mo.Option[[]time.Time], standardizing their handling across test cases.

Changes

Files/Paths Change Summary
openmeter/billing/worker/subscription/sync.go Introduced InvoiceByID type with IsGatheringInvoice method; updated patch generation logic and method signatures to use this map for conditional updates based on invoice status.
openmeter/billing/worker/subscription/sync_test.go Refactored tests to wrap InvoiceAt fields in mo.Option[[]time.Time]; added helper methods for generating these option values; updated assertions and struct field types accordingly.

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
Failed executing command with error: you are using a configuration file for golangci-lint v2 with golangci-lint v1: please use golangci-lint v2

✨ Finishing Touches
  • 📝 Generate Docstrings

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
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need 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)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 asserting InvoiceAt slices

expectLines indexes into expectedLine.InvoiceAt.OrEmpty()[idx] without checking that the slice is at least len(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

generateDailygenerateInvoiceAtTimestamps contains the word “generate” twice, which is a typo and makes the method harder to scan for.
A concise name such as generateDailyInvoiceAtTimestamps is 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

📥 Commits

Reviewing files that changed from the base of the PR and between 5334011 and 071256f.

📒 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 InvoiceByID type and IsGatheringInvoice method are well-implemented. Returning true when 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 InvoiceAt only 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

@turip
turip enabled auto-merge (squash) June 24, 2025 09:36
@turip
turip merged commit 8e626ed into main Jun 24, 2025
@turip
turip deleted the fix/do-not-sync-old-invoices branch June 24, 2025 09:42
@coderabbitai coderabbitai Bot mentioned this pull request Feb 3, 2026
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