Skip to content

fix: implement settlement mode in productcatalog - #4026

Merged
turip merged 1 commit into
mainfrom
fix/settlement-mode
Mar 30, 2026
Merged

turip merged 1 commit into
mainfrom
fix/settlement-mode

Conversation

@turip

@turip turip commented Mar 29, 2026 •

Copy link
Copy Markdown
Member

Overview

Wire up SettlementMode persistence in plan creation adapter, fixing a bug where non-default modes (e.g. credit_only) were silently stored as
credit_then_invoice due to the DB default

Add service-layer defaulting of empty SettlementMode to credit_then_invoice, ensuring existing API callers and tests that don't set the field
continue to work without breaking

Fix NextPlan and addon adapter's FromPlanRow to correctly copy/read SettlementMode, closing gaps where the field was dropped during plan
versioning and addon plan resolution

Enforce SettlementMode immutability on running subscriptions at the application layer (sync service), complementing the existing DB-level
Immutable() constraint

Add comprehensive tests covering plan create/update/default, plan-to-subscription propagation, custom subscription creation, and immutability
enforcement

Test plan

  • Plan adapter: create with default credit_then_invoice, create with explicit credit_only, update settlement mode, verify persistence via GetPlan
  • Plan service: create with empty settlement mode defaults to credit_then_invoice, create with explicit credit_only
  • Subscription: CreateFromPlan propagates default and non-default modes, custom subscription with credit_only, EditRunning rejects settlement
    mode changes
  • All existing plan, subscription, addon, and billing tests pass without modification (service-layer defaulting prevents breakage)

Summary by CodeRabbit

  • New Features

    • Added upfront validation to prevent changing settlement mode on active subscriptions.
  • Bug Fixes

    • Persisted settlement mode correctly during plan creation and mapping.
    • Applied a deterministic default settlement mode when not specified.
    • Ensured settlement mode is preserved when creating new plan versions.
  • Tests

    • Expanded tests covering settlement mode creation, updates, propagation, defaults, immutability and persistence across plans and subscriptions.

@turip
turip requested a review from a team as a code owner March 29, 2026 20:17
@coderabbitai

coderabbitai Bot commented Mar 29, 2026 •

Copy link
Copy Markdown
Contributor

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 80fabf74-8e02-4956-9f6a-b3cb44df791a

📥 Commits

Reviewing files that changed from the base of the PR and between 189795d and 304ee36.

📒 Files selected for processing (12)
  • openmeter/productcatalog/addon/adapter/mapping.go
  • openmeter/productcatalog/plan/adapter/adapter_test.go
  • openmeter/productcatalog/plan/adapter/plan.go
  • openmeter/productcatalog/plan/assert.go
  • openmeter/productcatalog/plan/service/plan.go
  • openmeter/productcatalog/plan/service/service_test.go
  • openmeter/productcatalog/testutils/plan.go
  • openmeter/subscription/addon/diff/restore_test.go
  • openmeter/subscription/service/sync.go
  • openmeter/subscription/testutils/builder.go
  • openmeter/subscription/workflow/service/addon_test.go
  • openmeter/subscription/workflow/service/subscription_test.go
✅ Files skipped from review due to trivial changes (8)
  • openmeter/productcatalog/testutils/plan.go
  • openmeter/productcatalog/plan/assert.go
  • openmeter/productcatalog/plan/service/plan.go
  • openmeter/subscription/testutils/builder.go
  • openmeter/subscription/workflow/service/addon_test.go
  • openmeter/subscription/addon/diff/restore_test.go
  • openmeter/subscription/service/sync.go
  • openmeter/productcatalog/plan/service/service_test.go
🚧 Files skipped from review as they are similar to previous changes (3)
  • openmeter/productcatalog/plan/adapter/plan.go
  • openmeter/productcatalog/addon/adapter/mapping.go
  • openmeter/productcatalog/plan/adapter/adapter_test.go

📝 Walkthrough

Walkthrough

Adds end-to-end support for the Plan/Subscription SettlementMode field: DB mapping, persistence on create/update, service defaults and propagation, an immutability sanity check during subscription sync, and expanded tests and test fixtures.

Changes

Cohort / File(s) Summary
Database Mapping & Persistence
openmeter/productcatalog/addon/adapter/mapping.go, openmeter/productcatalog/plan/adapter/plan.go
Map SettlementMode from DB rows into plan objects and persist SettlementMode on plan creation and update.
Plan Service Layer
openmeter/productcatalog/plan/service/plan.go
Default empty SettlementMode to CreditThenInvoiceSettlementMode on create and copy SettlementMode when creating next plan version.
Subscription Sync Validation
openmeter/subscription/service/sync.go
Reject sync if existing subscription view and incoming spec have differing SettlementMode (early sanity check).
Plan Assertion & Test Adapters
openmeter/productcatalog/plan/assert.go, openmeter/productcatalog/plan/adapter/adapter_test.go
Add assertion for SettlementMode in plan create assertions; add adapter tests for create/update persistence of SettlementMode.
Plan Tests & Testutils
openmeter/productcatalog/plan/service/service_test.go, openmeter/productcatalog/testutils/plan.go
Add tests for explicit/default settlement mode behavior; test plan builder now sets default SettlementMode.
Subscription Tests & Builders
openmeter/subscription/workflow/service/subscription_test.go, openmeter/subscription/workflow/service/addon_test.go, openmeter/subscription/testutils/builder.go, openmeter/subscription/addon/diff/restore_test.go
Extend fixtures and multiple test cases to include SettlementMode (defaults, propagation, immutability, and related addon/restore scenarios).

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Possibly related PRs

Suggested reviewers

  • tothandras
  • chrisgacsal
🚥 Pre-merge checks | ✅ 2 | ❌ 1

❌ Failed checks (1 warning)

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.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately captures the main focus: implementing settlement mode handling in the productcatalog layer, which aligns with the core changes across plan creation, persistence, and subscription propagation.

✏️ 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 fix/settlement-mode

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 the release-note/bug-fix Release note: Bug Fixes label Mar 29, 2026
@turip turip changed the title fix: settlement mode fix: actually implement settlement mode Mar 29, 2026

@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

🧹 Nitpick comments (1)
openmeter/productcatalog/plan/service/service_test.go (1)

214-238: Consider adding one invalid-settlement-mode test case.

Nice coverage on explicit and defaulted success paths. Adding a failing case for an unsupported mode would lock down the validation side too.

As per coding guidelines **/*_test.go: “Make sure the tests are comprehensive and cover the changes.”

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@openmeter/productcatalog/plan/service/service_test.go` around lines 214 -
238, Add a new subtest (e.g., t.Run("InvalidSettlementMode", ...)) that uses
pctestutils.NewTestPlan to build a plan, sets .Key to a unique value and sets
.SettlementMode to an unsupported string (e.g., "invalid_mode"), then call
env.Plan.CreatePlan(ctx, ...) and assert that it returns an error and does not
return a valid plan (use require.Error/require.Nil or assert.Error/assert.Nil).
Reference the same symbols used in existing tests: pctestutils.NewTestPlan,
env.Plan.CreatePlan, and the SettlementMode field and productcatalog constants
to locate where to add this failing-case test.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@openmeter/subscription/workflow/service/subscription_test.go`:
- Around line 2079-2122: The test asserts EditRunning returns an error when
attempting to mutate SettlementMode but doesn't verify the subscription state
stayed unchanged; after the failing call to deps.WorkflowService.EditRunning
(using subView.Subscription.NamespacedID), re-fetch the subscription (e.g., via
deps.WorkflowService.View or the appropriate get/read method on WorkflowService)
and assert that the subscription's SettlementMode remains
productcatalog.CreditThenInvoiceSettlementMode (checking
subscription.Subscription.SettlementMode) to ensure the failed edit didn't
mutate state.

---

Nitpick comments:
In `@openmeter/productcatalog/plan/service/service_test.go`:
- Around line 214-238: Add a new subtest (e.g., t.Run("InvalidSettlementMode",
...)) that uses pctestutils.NewTestPlan to build a plan, sets .Key to a unique
value and sets .SettlementMode to an unsupported string (e.g., "invalid_mode"),
then call env.Plan.CreatePlan(ctx, ...) and assert that it returns an error and
does not return a valid plan (use require.Error/require.Nil or
assert.Error/assert.Nil). Reference the same symbols used in existing tests:
pctestutils.NewTestPlan, env.Plan.CreatePlan, and the SettlementMode field and
productcatalog constants to locate where to add this failing-case test.
🪄 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: 575d325d-5df6-4c63-9077-06799df43469

📥 Commits

Reviewing files that changed from the base of the PR and between 155027e and 189795d.

📒 Files selected for processing (12)
  • openmeter/productcatalog/addon/adapter/mapping.go
  • openmeter/productcatalog/plan/adapter/adapter_test.go
  • openmeter/productcatalog/plan/adapter/plan.go
  • openmeter/productcatalog/plan/assert.go
  • openmeter/productcatalog/plan/service/plan.go
  • openmeter/productcatalog/plan/service/service_test.go
  • openmeter/productcatalog/testutils/plan.go
  • openmeter/subscription/addon/diff/restore_test.go
  • openmeter/subscription/service/sync.go
  • openmeter/subscription/testutils/builder.go
  • openmeter/subscription/workflow/service/addon_test.go
  • openmeter/subscription/workflow/service/subscription_test.go

@turip turip changed the title fix: actually implement settlement mode fix: implement settlement mode in productcatalog Mar 30, 2026
@turip
turip merged commit a31ed79 into main Mar 30, 2026
22 of 23 checks passed
@turip
turip deleted the fix/settlement-mode branch March 30, 2026 09:43

This branch was previously deployed

1 inactive deployment
dev — 304ee364 Deployed Mar 30, 2026 by turip via Migration Checks #12965
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants