Skip to content

fix: subscription sync zero prorating cases (6/35) - #4364

Merged
turip merged 8 commits into
mainfrom
feat/credit-then-invoice-subscription-sync-tests
May 15, 2026
Merged

turip merged 8 commits into
mainfrom
feat/credit-then-invoice-subscription-sync-tests

Conversation

@turip

@turip turip commented May 15, 2026 •

Copy link
Copy Markdown
Member

Summary

  • Add credit-then-invoice subscription sync coverage for provisioning, progressive invoicing, cancellation/continue resync, in-arrears prorating, and ledger balance assertions.
  • Fix flat-fee credit-then-invoice shrink/extend handling when proration produces a zero amount: skip recreating zero gathering lines, delete mutable standard lines, detach/archive the current realization run, reset the charge to created, and set the next advance timestamp.
  • Refactor subscription invoice-line loading so charge-managed lines are excluded at query time through GetLinesForSubscriptionInput.IncludeChargeManaged.
  • Expose GetChargeID() on generic invoice line readers and both gathering/standard line bases so callers can reason about charge-owned invoice lines consistently.
  • Update the subscription sync persisted-state loader to request non-charge-managed lines only, with an inline comment documenting that charge-managed invoice lines are edited through charge patches.
  • Remove the skipped credit-then-invoice subscription sync test cases; the file now contains only active validation coverage.
  • Address review follow-ups by simplifying the charge-managed filter to a nil charge_id check and fixing the CodeRabbit typo comment.

Verification

  • env POSTGRES_HOST=127.0.0.1 go test -count=1 -tags=dynamic ./openmeter/billing/worker/subscriptionsync/service -run '^TestCreditThenInvoiceScenarios$'
  • CodeRabbit review re-polled after the follow-up fixes; final CodeRabbit status was SUCCESS with no current actionable findings.

Summary by CodeRabbit

  • New Features

    • Option to exclude charge-managed invoice lines from subscription line queries.
  • Bug Fixes

    • Flat-fee billing now skips or removes zero-prorated invoice lines and resets related charge state.
    • Subscription sync avoids reconciling charge-managed lines, keeping charge edits separate.
  • Tests

    • Added comprehensive credit-then-invoice scenario tests covering billing flows, proration, invoicing, and lifecycle resync.

Review Change Stack

@coderabbitai

coderabbitai Bot commented May 15, 2026 •

Copy link
Copy Markdown
Contributor
📝 Walkthrough

Walkthrough

Adds ChargeID getters and an IncludeChargeManaged flag to invoice line contracts, filters DB queries to optionally exclude charge-managed lines, updates the subscription persisted-state loader to request non-charge-managed lines, implements zero-proration cleanup in flat-fee invoice patching, and adds a credit-then-invoice test suite.

Changes

Charge-aware invoice line separation and zero-amount handling

Layer / File(s) Summary
ChargeID accessors and filtering contract
openmeter/billing/invoiceline.go, openmeter/billing/gatheringinvoice.go, openmeter/billing/stdinvoiceline.go
New IncludeChargeManaged boolean on GetLinesForSubscriptionInput and GetChargeID() *string added to GenericInvoiceLineReader and both GatheringLineBase and StandardLineBase, exposing charge ownership for invoice lines.
Query filtering by charge management
openmeter/billing/adapter/stdinvoicelines.go
GetLinesForSubscription conditionally filters results when IncludeChargeManaged is false to return only invoice lines with ChargeID = NULL.
Subscription loader uses charge-line exclusion
openmeter/billing/worker/subscriptionsync/service/persistedstate/loader.go
LoadForSubscription sets IncludeChargeManaged:false when loading subscription lines and documents that charge-managed lines are reconciled via charge patches and separate charge entities.
Flat-fee zero-amount state cleanup
openmeter/billing/charges/flatfee/service/creditheninvoice.go
When prorated amount becomes zero in pre-active or mutable runs, the code short-circuits or deletes the invoice line, detaches/archives the current run, clears CurrentRun, resets charge status to StatusCreated, sets State.AdvanceAfter from the service period start, and returns; adds github.com/samber/lo import.
Credit-then-invoice tests and helpers
openmeter/billing/worker/subscriptionsync/service/sync_credittheninvoice_test.go
Adds CreditThenInvoiceTestSuite with scenario tests and many helpers/assertions to validate promotional credit funding, subscription sync, progressive billing, resync behavior, and multiple proration cases with ledger balance assertions.

Sequence Diagram(s)

No sequence diagram generated.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Possibly related PRs

  • openmeterio/openmeter#4327: Related invoice patch plumbing for deleting gathering lines by charge and charge-targeted invoice updates.
  • openmeterio/openmeter#4354: Related work adding IncludeChargeManaged/GetChargeID accessors and charge-managed line handling.

Suggested reviewers

  • tothandras
  • GAlexIHU
🚥 Pre-merge checks | ✅ 3 | ❌ 2

❌ Failed checks (1 warning, 1 inconclusive)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
Title check ❓ Inconclusive The title focuses on 'zero prorating cases' but the PR's actual main objective is adding a comprehensive 'credit-then-invoice' subscription sync test suite with multiple scenario validations. Consider if the title should emphasize the test suite additions and scenario coverage, or clarify how zero prorating relates to the primary changes.
✅ Passed checks (3 passed)
Check name Status Explanation
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch feat/credit-then-invoice-subscription-sync-tests

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

Comment @coderabbitai help to get the list of available commands and usage tips.

@turip turip added area/billing release-note/misc Miscellaneous changes labels May 15, 2026
Base automatically changed from codex/flatfee-handler-renames to main May 15, 2026 09:14
@turip
turip force-pushed the feat/credit-then-invoice-subscription-sync-tests branch from 680082d to 490ad65 Compare May 15, 2026 09:32
@turip
turip marked this pull request as ready for review May 15, 2026 09:38
@turip
turip requested a review from a team as a code owner May 15, 2026 09:38

@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: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In
`@openmeter/billing/worker/subscriptionsync/service/sync_credittheninvoice_test.go`:
- Line 802: Fix the typo in the inline comment next to the duration check in the
test (the expression using line.ServicePeriod.Duration() != time.Hour*24);
change "1 dqy" to "1 day" so the comment correctly reads that all other lines
will be 1 day.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: a520143c-a7ef-4ffb-b4fa-b3a72a35e085

📥 Commits

Reviewing files that changed from the base of the PR and between 490ad65 and 4e7c765.

📒 Files selected for processing (1)
  • openmeter/billing/worker/subscriptionsync/service/sync_credittheninvoice_test.go

@turip turip changed the title test: add credit-then-invoice subscription sync coverage fix: subscription sync zero prorating cases May 15, 2026
@turip turip changed the title fix: subscription sync zero prorating cases fix: subscription sync zero prorating cases (6/35) May 15, 2026
@turip
turip merged commit ff255ce into main May 15, 2026
26 checks passed
@turip
turip deleted the feat/credit-then-invoice-subscription-sync-tests branch May 15, 2026 12:10

This branch was previously deployed

1 inactive deployment
dev — f64c5ded Deployed May 15, 2026 by turip via Migration Checks #14407
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