Repository navigation
refactor(billing-sync): generate lines for first cadence for future s… - #3015
Conversation
📝 WalkthroughWalkthroughThis 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
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 ✨ 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
🧹 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
📒 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
timeimport is correctly added to support the new error type that usestime.Timeparameters.
122-150: Well-structured error type implementation.The new
BillingPeriodQueriedBeforeSubscriptionStartErrorfollows the established pattern in this file perfectly:
- Consistent struct design with embedded error
- Proper implementation of
Error()andUnwrap()methods- Meaningful constructor with descriptive error message
- Helper function using
errors.Asfor 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.ErrorfwithNewBillingPeriodQueriedBeforeSubscriptionStartErroris 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
BillingPeriodQueriedBeforeSubscriptionStartErroris 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
generationLimittosubs.Subscription.ActiveFrominstead 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.Frommight 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:
- Only a gathering invoice is created (no instant invoice)
- Both in-advance fees and usage-based items are deferred to the gathering invoice
- 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.
…ubscriptions
Overview
Fixes #(issue)
Notes for reviewer
Summary by CodeRabbit