Repository navigation
fix: phase iterator in advance/in arreas - #2860
Conversation
📝 Walkthrough## Walkthrough
The changes centralize invoice date calculation logic for subscription billing periods, introduce a struct to encapsulate generated period metadata, and update iteration logic to account for invoice dates in termination checks. Corresponding test cases are adjusted and extended to reflect the new logic, including additional billing periods and scenarios involving mixed in-advance and in-arrears billing rules.
## Changes
| Files/Paths | Change Summary |
|-----------------------------------------------------------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| openmeter/billing/worker/subscription/phaseiterator.go | Centralized invoice date calculation with a new `getInvoiceAt` method; introduced `generatedVersionPeriodItem` struct; updated period generation and iteration logic to consider invoice dates; refined method signatures. |
| openmeter/billing/worker/subscription/phaseiterator_test.go | Renamed test cases for clarity; extended existing tests with additional billing periods triggered by invoice dates; added new tests covering mixed in-advance and in-arrears billing rules for aligned and unaligned subscriptions. |
| openmeter/billing/worker/subscription/sync_test.go | Extended expected billing period ranges and invoice timestamps by one unit in multiple assertions across various in-advance and in-arrears billing scenarios, including aligned subscriptions and prorated amounts. |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 Note ⚡️ AI Code Reviews for VS Code, Cursor, WindsurfCodeRabbit now has a plugin for VS Code, Cursor and Windsurf. This brings AI code reviews directly in the code editor. Each commit is reviewed immediately, finding bugs before the PR is raised. Seamless context handoff to your AI code agent ensures that you can easily incorporate review feedback. Note ⚡️ Faster reviews with cachingCodeRabbit now supports caching for code and dependencies, helping speed up reviews. This means quicker feedback, reduced wait times, and a smoother review experience overall. Cached data is encrypted and stored securely. This feature will be automatically enabled for all accounts on May 30th. To opt out, configure 📜 Recent review detailsConfiguration used: CodeRabbit UI 📒 Files selected for processing (3)
🚧 Files skipped from review as they are similar to previous changes (2)
🧰 Additional context used🧬 Code Graph Analysis (1)openmeter/billing/worker/subscription/phaseiterator_test.go (3)
⏰ Context from checks skipped due to timeout of 90000ms (12)
🔇 Additional comments (7)
✨ 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 (
|
215b21e to
ff00f90
Compare
ff00f90 to
a2de83e
Compare
There was a problem hiding this comment.
Actionable comments posted: 1
🧹 Nitpick comments (1)
openmeter/billing/worker/subscription/phaseiterator.go (1)
625-640: Good abstraction – consider promoting to a utility package
getInvoiceAtcleanly encapsulates invoice-date rules and eliminates scattered conditionals elsewhere (once the duplication above is removed).
If other parts of the billing pipeline need the same logic, consider moving this helper to a shared util to prevent future drift.
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (3)
openmeter/billing/worker/subscription/phaseiterator.go(8 hunks)openmeter/billing/worker/subscription/phaseiterator_test.go(7 hunks)openmeter/billing/worker/subscription/sync_test.go(7 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (1)
openmeter/billing/worker/subscription/phaseiterator_test.go (3)
openmeter/ent/db/subscriptionitem/where.go (1)
Key(105-107)api/api.gen.go (2)
FlatPriceType(3171-3171)UnitPriceType(6925-6925)api/client/go/client.gen.go (2)
FlatPriceType(2876-2876)UnitPriceType(6348-6348)
⏰ Context from checks skipped due to timeout of 90000ms (5)
- GitHub Check: CI
- GitHub Check: E2E
- GitHub Check: Lint
- GitHub Check: Test
- GitHub Check: Analyze (go)
🔇 Additional comments (19)
openmeter/billing/worker/subscription/phaseiterator_test.go (9)
72-73: Improved test case naming for clarity.The test case names have been clarified to explicitly indicate they're testing unaligned scenarios, which helps improve test readability and understanding of what each test is validating.
78-79: Improved test case naming for clarity.Renamed "empty-aligned" to "aligned empty" for better consistency in naming conventions.
311-312: Improved test name for clarity of testing context.Renamed "flat-fee recurring" to "unaligned flat-fee recurring" to explicitly identify the test's context, making it clearer what scenario is being tested.
331-337: Added extra test period for correct invoice date handling.This addition properly tests the fix for the phase iterator to account for invoice dates in termination checks. The extra period represents the case where the in-advance item for the next phase should be included because the end date is greater than or equal to the invoice date.
391-396: Added additional test period for "flat-fee recurring, edited" test.This change extends the test to include an additional billing period, correctly validating that the phase iterator continues iteration when the invoice date is considered, even for edited items.
434-440: Added extra test period for aligned in-advance billing.This change adds an important test case to verify that aligned subscriptions correctly generate the in-advance item for the next phase when the invoice date is considered in the termination check.
492-498: Added test period for aligned one-time with cadence.The additional test period correctly validates that an extra billing period is generated based on the invoice date logic, ensuring that the phase iterator continues until both the period start and invoice date exceed the iteration end.
555-588: Added critical test case for mixed in-advance and in-arrears billing.This new test case directly addresses the PR objective by testing the scenario where a subscription has both in-advance and in-arrears items in an aligned context. It verifies that the phase iterator correctly generates two periods for the in-advance item (current and next) and one period for the in-arrears item.
589-621: Added complementary test for unaligned mixed billing items.This test complements the previous aligned test case by validating the same functionality in an unaligned context. Together, these two tests comprehensively cover the fix for the issue where in-advance and in-arrears items were being incorrectly separated into different invoices.
openmeter/billing/worker/subscription/sync_test.go (9)
703-704: Updated test expectations for additional billing period.The PeriodMax value has been increased from 4 to 5 to account for the additional billing period that should be generated when invoice dates are considered in the termination check.
708-710: Updated period and invoice timestamp expectations.Extended the expected periods and invoice timestamps arrays with an additional entry to reflect the corrected phase iterator logic that now continues iteration until both the period start and invoice date exceed the iteration end.
799-800: Updated PeriodMax for gathering sync test expectations.Increased PeriodMax to 5 to correctly account for the extended period generation in the phase iterator, ensuring the test validates the fixed behavior where in-advance items are properly included.
804-806: Updated period and invoice timestamp arrays for non-billable amount test.Extended the expected periods and invoice timestamps arrays to reflect the corrected phase iterator behavior, validating that in-advance items are properly generated for the next billing cycle.
992-993: Updated PeriodMax for prorated test expectations.Increased the PeriodMax value from 3 to 4 to account for the extended period generation in the phase iterator when handling prorated billing scenarios.
997-999: Added expected periods and invoice timestamps for prorated test.Extended the expected test data to validate that the phase iterator correctly handles prorated billing with the fixed iteration logic.
1103-1109: Updated test expectations for draft invoice prorated test.Increased the PeriodMax value and extended the expected periods and invoice timestamps arrays to account for the additional billing period, ensuring the corrected phase iterator behavior is properly validated for draft invoices.
1245-1252: Updated test expectations for issued invoice prorating test.Increased the PeriodMax value and extended the expected periods and invoice timestamps arrays to account for the fixed phase iterator behavior, ensuring that issued invoices with prorated items are correctly handled.
1523-1533: Updated expected periods for aligned subscription invoicing test.Extended the expected period and invoice timestamp arrays to include an additional weekly period and invoice date for in-advance items, properly validating the fix for the phase iterator when handling aligned subscriptions.
openmeter/billing/worker/subscription/phaseiterator.go (1)
273-276: Iteration-end guard may still over-generate for mixed in-advance / in-arrears linesThe loop exits only when both
at(period end) andinvoiceAtare not beforeiterationEnd.
For in-advance itemsinvoiceAt == period.From, so the loop will keep running untilperiod.From ≥ iterationEnd.
That behaviour matches the comment at the top of the file, but please double-check that downstream code indeed needs both dates to pass the boundary – otherwise you may continue generating extra periods for usage-based items whose invoice is already due.No change suggested; just confirm the requirement.
If in advance and in arreas items are mixed on an invoice, the current phase iterator does not generate the in advance item for the next phase. This results in the in advance and in arreas items to be included in seperate invoices. This patch fixes this by considering the invoice_at property of the soon to be generated lines.
a2de83e to
258ce09
Compare
Overview
If in advance and in arreas items are mixed on an invoice, the current phase iterator does not generate the in advance item for the next phase.
This results in the in advance and in arreas items to be included in seperate invoices.
This patch fixes this by considering the invoice_at property of the soon to be generated lines.
Summary by CodeRabbit