Skip to content

refactor(billing-sync): generate lines for first cadence for future s… - #3015

Merged
GAlexIHU merged 1 commit into
mainfrom
galexi/bill-future-subs
Jun 19, 2025
Merged

GAlexIHU merged 1 commit into
mainfrom
galexi/bill-future-subs

Conversation

@GAlexIHU

@GAlexIHU GAlexIHU commented Jun 19, 2025 •

Copy link
Copy Markdown
Contributor

…ubscriptions

Overview

Fixes #(issue)

Notes for reviewer

Summary by CodeRabbit

  • Bug Fixes
    • Improved handling of billing periods that are queried before a subscription's start date, ensuring accurate invoice line generation for subscriptions starting in the future.
  • New Features
    • Introduced specific error messaging for cases when billing is attempted before a subscription begins, improving clarity in error reporting.
  • Tests
    • Added a new test to verify correct invoice generation for subscriptions that start in the future, ensuring in-advance fees are included appropriately.

@GAlexIHU
GAlexIHU requested a review from a team as a code owner June 19, 2025 12:02
@GAlexIHU GAlexIHU added the release-note/misc Miscellaneous changes label Jun 19, 2025
@coderabbitai

coderabbitai Bot commented Jun 19, 2025 •

Copy link
Copy Markdown
Contributor
📝 Walkthrough

Walkthrough

This change introduces a new error type to represent attempts to query billing periods before a subscription's start date, refines the error handling logic in the subscription synchronization process, and updates related tests. It also adjusts the logic for generating upcoming billing lines for aligned subscriptions and adds a new test for subscriptions starting in the future.

Changes

Files/Paths Change Summary
openmeter/subscription/errors.go Added a new error type BillingPeriodQueriedBeforeSubscriptionStartError with constructor and helper for detecting this error type.
openmeter/subscription/subscriptionspec.go Modified GetAlignedBillingPeriodAt to return the new error type when queried before subscription start.
openmeter/billing/worker/subscription/sync.go Updated error handling in collectUpcomingLines to recognize and handle the new error type, refining billing line generation logic.
openmeter/billing/worker/subscription/sync_test.go Added a test for future-starting subscriptions to verify correct invoice and billing line generation.

Possibly related PRs

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.

@GAlexIHU GAlexIHU self-assigned this Jun 19, 2025
@GAlexIHU
GAlexIHU enabled auto-merge (squash) June 19, 2025 12:03

@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

🧹 Nitpick comments (1)
openmeter/billing/worker/subscription/sync_test.go (1)

3505-3505: Update comment to reflect actual test behavior.

The comment states "Gathering invoice should have the UBP line" but the test actually expects both the in-advance fee and usage-based line in the gathering invoice.

-	// Gathering invoice should have the UBP line
+	// Gathering invoice should have both the in-advance fee and usage-based line
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between ef3f681 and 66e8f7e.

📒 Files selected for processing (4)
  • openmeter/billing/worker/subscription/sync.go (1 hunks)
  • openmeter/billing/worker/subscription/sync_test.go (1 hunks)
  • openmeter/subscription/errors.go (2 hunks)
  • openmeter/subscription/subscriptionspec.go (1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (13)
  • GitHub Check: Artifacts / Container image
  • GitHub Check: Developer environment
  • GitHub Check: Artifacts / Benthos Collector Container image
  • GitHub Check: E2E
  • GitHub Check: Quickstart
  • GitHub Check: CI
  • GitHub Check: Commit hooks
  • GitHub Check: Lint
  • GitHub Check: Migration Checks
  • GitHub Check: Test
  • GitHub Check: Build
  • GitHub Check: Analyze (javascript)
  • GitHub Check: Analyze (go)
🔇 Additional comments (6)
openmeter/subscription/errors.go (2)

6-6: LGTM: Required import for new error type.

The time import is correctly added to support the new error type that uses time.Time parameters.


122-150: Well-structured error type implementation.

The new BillingPeriodQueriedBeforeSubscriptionStartError follows the established pattern in this file perfectly:

  • Consistent struct design with embedded error
  • Proper implementation of Error() and Unwrap() methods
  • Meaningful constructor with descriptive error message
  • Helper function using errors.As for type checking
  • Clear parameter names that indicate the purpose

This specific error type enables more granular error handling in the billing synchronization logic.

openmeter/subscription/subscriptionspec.go (1)

233-233: Excellent refactoring to use specific error type.

Replacing the generic fmt.Errorf with NewBillingPeriodQueriedBeforeSubscriptionStartError is a significant improvement:

  • Enables type-safe error handling downstream
  • Maintains the same error information while providing better structure
  • Allows the billing sync logic to handle this specific case gracefully
  • Follows best practices for error design in Go
openmeter/billing/worker/subscription/sync.go (2)

385-395: Excellent error handling for future subscription scenarios.

The specific handling of BillingPeriodQueriedBeforeSubscriptionStartError is well-implemented:

  • Uses type-safe error checking instead of generic error handling
  • Provides clear logging with relevant context (subscription ID, asOf time, subscription start)
  • Gracefully adjusts generationLimit to subs.Subscription.ActiveFrom instead of failing
  • Enables billing line generation for subscriptions that start in the future

This change significantly improves the robustness of the billing synchronization process.


398-400: Proper handling of zero value edge case.

The condition update correctly handles the scenario where currBillingPeriod.From might be zero due to the error handling above. Using !currBillingPeriod.From.IsZero() ensures the logic works correctly when the billing period couldn't be determined due to querying before subscription start.

openmeter/billing/worker/subscription/sync_test.go (1)

3448-3539: Comprehensive test for future subscription billing behavior - well implemented!

This test method correctly validates the billing synchronization logic for subscriptions that start in the future. The test design is sound and follows established patterns in the codebase.

Key strengths:

  • Clear test scenario documentation
  • Proper clock manipulation to simulate future vs present time
  • Accurate expectations for gathering invoice content
  • Good contrast with the existing immediate subscription test

The test correctly validates that when a subscription starts in the future:

  1. Only a gathering invoice is created (no instant invoice)
  2. Both in-advance fees and usage-based items are deferred to the gathering invoice
  3. Invoice timing aligns with the subscription start date

This complements the changes mentioned in the AI summary for handling billing periods queried before subscription start dates.

@GAlexIHU
GAlexIHU merged commit c50be79 into main Jun 19, 2025
@GAlexIHU
GAlexIHU deleted the galexi/bill-future-subs branch June 19, 2025 12:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

release-note/misc Miscellaneous changes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants