Repository navigation
feat(productcatalog): support custom currencies - #4738
Conversation
|
Too many files changed for review. ( Bypass the limit by tagging |
|
Note Reviews pausedIt looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the Use the following commands to manage reviews:
Use the checkboxes below for quick actions:
📝 WalkthroughWalkthroughThis change adds managed custom-currency references and rate-card overrides across product-catalog models, persistence, validation, APIs, service wiring, subscription validation, and v1 compatibility handling. ChangesCustom currency model and storage
Estimated code review effort: 5 (Critical) | ~120 minutes Sequence Diagram(s)sequenceDiagram
participant API
participant PlanService
participant CurrencyResolver
participant ProductCatalogAdapter
participant Database
API->>PlanService: Submit plan currency references
PlanService->>CurrencyResolver: Resolve currency identities
CurrencyResolver->>Database: Load custom currency and cost basis
Database-->>CurrencyResolver: Return currency identity
CurrencyResolver-->>PlanService: Return resolved reference
PlanService->>ProductCatalogAdapter: Persist plan and rate-card currencies
ProductCatalogAdapter->>Database: Store fiat code or custom currency ID
Database-->>ProductCatalogAdapter: Reload currency relationships
ProductCatalogAdapter-->>API: Return catalog resource
Possibly related PRs
Suggested labels: Suggested reviewers: 🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
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. Comment |
There was a problem hiding this comment.
Actionable comments posted: 12
🧹 Nitpick comments (3)
openmeter/productcatalog/currencyresolver_test.go (1)
184-193: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick winAvoid adding the local
must...wrapper.Please inline these builder calls or use an existing currency fixture; this helper only hides a short operation.
As per coding guidelines, do not add local wrappers such as
mustwhen existing helpers or direct operations suffice.🤖 Prompt for 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. In `@openmeter/productcatalog/currencyresolver_test.go` around lines 184 - 193, Remove the local mustFiatCurrency helper and replace its call sites with direct currencyx.NewCurrencyBuilder(...).WithCode(...).Build() usage or an existing currency fixture. Preserve the current fiat currency construction and error assertions at each call site.Source: Coding guidelines
openmeter/productcatalog/addon/serializer_test.go (1)
32-43: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick winCover the usage-based serialization branch too.
The new serializer switches separately on flat-fee and usage-based rate cards, but this round-trip only exercises flat-fee. A table case using
UsageBasedRateCardwould catch regressions in that branch.As per path instructions, tests should comprehensively cover introduced behavior.
Also applies to: 67-73
🤖 Prompt for 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. In `@openmeter/productcatalog/addon/serializer_test.go` around lines 32 - 43, Extend the serializer round-trip test cases in the relevant test function to include a UsageBasedRateCard fixture alongside the existing FlatFeeRateCard case. Populate the usage-based card with the required metadata and usage pricing fields, then assert it round-trips successfully so the separate usage-based serialization branch is covered.Source: Path instructions
pkg/currencyx/code.go (1)
39-44: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low valueSkip custom length check for empty inputs to keep validation errors tidy.
Since an empty string will evaluate
IsFiat()asfalse, the code currently emits two errors when no currency is provided: "currency code is required" and "custom currency code must be between 3 and 64 characters".We can skip the latter check if the code is completely empty to keep the returned error slightly more concise!
💡 Suggested refactor
- if !c.IsFiat() { + if c != "" && !c.IsFiat() { if length := len(code); length < CustomCurrencyCodeMinLength || length > CustomCurrencyCodeMaxLength { errs = append(errs, fmt.Errorf("custom currency code must be between %d and %d characters", CustomCurrencyCodeMinLength, CustomCurrencyCodeMaxLength)) } }🤖 Prompt for 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. In `@pkg/currencyx/code.go` around lines 39 - 44, Update the custom currency length validation in the IsFiat check to run only when code is non-empty, so empty input produces only the existing required-code error. Preserve the current 3–64 character validation for non-empty custom currency codes.
🤖 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/currencies/currency.go`:
- Around line 83-87: The managed custom currency ID validation error in the
currency validation method is missing field context. Update the `c.IsCustom() &&
c.ID == ""` branch to wrap its error with the `code:` field prefix, matching the
existing code validation failure, while preserving collection through
`errors.Join(errs...)` and `models.NewNillableGenericValidationError`.
In `@openmeter/productcatalog/addon.go`:
- Around line 90-96: Update the currency validation branch in the addon
validation method to wrap and retain the concrete error returned by
m.Currency.Validate() alongside ErrCurrencyInvalid, while preserving the
existing currency field selector and collecting the error in errs. Keep the
nil-currency handling unchanged.
In `@openmeter/productcatalog/addon/service.go`:
- Around line 410-424: The currency-preservation logic around persistedCurrency
and updatedCurrency must only replace code-only currency values, not an
explicitly managed update identity. Before calling rateCard.ChangeMeta, detect
when updatedCurrency is already a ManagedCurrency with a non-empty ID and skip
preservation; retain the existing same-code behavior for code-only updates and
continue preserving the persisted identity there.
In `@openmeter/productcatalog/currencyresolver.go`:
- Around line 150-153: Update the existing.IsCustom branch in the currency
resolution flow to avoid returning ManagedCurrency values solely because GetID()
is non-empty; re-resolve managed custom currencies by their ID or verify their
concrete type belongs to the requested namespace before returning. Preserve the
current fast path only for currencies proven to match namespace, and add a test
covering a managed custom currency from another namespace.
In `@openmeter/productcatalog/currencyresolver/resolver_test.go`:
- Around line 22-27: Update mockCurrencyService.ListCurrencies to honor
params.Page and the requested page size instead of returning all currencies. Add
a regression test with two archived currencies before an active matching
currency, ensuring Resolve still finds the active result. Update Resolve to
query active currencies or continue paging until a valid match is found.
In `@openmeter/productcatalog/currencyresolver/resolver.go`:
- Around line 45-68: The currency lookup around ListCurrencies currently
paginates before excluding archived entries, so active matches can be missed.
Push the DeletedAt-is-nil predicate into the ListCurrencies filter for custom
currencies, or continue fetching pages until activeCurrencies establishes a
definitive not-found or unique result; preserve the existing uniqueness and
error handling behavior.
In `@openmeter/productcatalog/errors.go`:
- Around line 265-315: Rename the new error-code constants in this section and
the additionally referenced declaration from the ErrCode... form to the required
ErrorCode... form, including ErrorCodeRateCardCurrencyOverrideNotAllowed,
ErrorCodeRateCardCurrencyOverrideRedundant,
ErrorCodeRateCardCurrencyRequiresPrice, ErrorCodePlanMultipleFiatCurrencies,
ErrorCodeCurrencyNotFound, and ErrorCodeCurrencyCostBasisNotFound. Update every
corresponding reference while leaving the exported validation issue variables
unchanged.
In `@openmeter/productcatalog/plan.go`:
- Around line 382-388: Update the currency validation branches in the plan
validation method so both missing-currency and Currency.Validate() failures are
wrapped with the “currency” field context. Preserve the underlying validation
error as the cause when wrapping the Currency.Validate() result, rather than
replacing it with ErrCurrencyInvalid.
In `@openmeter/productcatalog/plan/ratecard.go`:
- Around line 103-224: Remove the map-based currency rewriting from RateCard
MarshalJSON and UnmarshalJSON. In MarshalJSON, delegate directly to json.Marshal
on r.RateCard; in UnmarshalJSON, unmarshal serialized.RateCard directly into the
type-specific rateCard selected by serialized.Type. Delete
marshalRateCardForJSON, rateCardJSONWithoutCurrency, and
setRateCardCurrencyIdentity, while preserving existing error handling and
managed-field assignment.
- Around line 103-224: Remove the map-based currency rewriting from
RateCard.MarshalJSON and RateCard.UnmarshalJSON. Delegate serialization and
deserialization directly to json.Marshal and json.Unmarshal on the concrete
FlatFeeRateCard and UsageBasedRateCard values, then delete
marshalRateCardForJSON, rateCardJSONWithoutCurrency, and
setRateCardCurrencyIdentity while preserving the existing type selection and
managed-field handling.
In `@openmeter/productcatalog/plan/service/service_test.go`:
- Around line 924-928: Strengthen the assertions in the stored rate-card test
around storedRateCard.Currency and EffectiveCurrency to validate the managed
currency resource identity, not only its code. Assert both values are
currencyx.ManagedCurrency instances and compare their IDs with the expected
custom managed currency ID, preserving the existing code checks and ensuring
hydration cannot resolve a different resource sharing CREDITS.
In `@openmeter/productcatalog/planaddon/service/planaddon.go`:
- Around line 148-150: Update the create flow around ValidateWith to first run
the full plan-addon validation, mirroring the update path, before executing the
resolver-backed currency validation. Ensure invalid phases, keys, quantities,
cadence, and overlays are rejected before persistence while retaining the
existing currency-only check afterward.
---
Nitpick comments:
In `@openmeter/productcatalog/addon/serializer_test.go`:
- Around line 32-43: Extend the serializer round-trip test cases in the relevant
test function to include a UsageBasedRateCard fixture alongside the existing
FlatFeeRateCard case. Populate the usage-based card with the required metadata
and usage pricing fields, then assert it round-trips successfully so the
separate usage-based serialization branch is covered.
In `@openmeter/productcatalog/currencyresolver_test.go`:
- Around line 184-193: Remove the local mustFiatCurrency helper and replace its
call sites with direct currencyx.NewCurrencyBuilder(...).WithCode(...).Build()
usage or an existing currency fixture. Preserve the current fiat currency
construction and error assertions at each call site.
In `@pkg/currencyx/code.go`:
- Around line 39-44: Update the custom currency length validation in the IsFiat
check to run only when code is non-empty, so empty input produces only the
existing required-code error. Preserve the current 3–64 character validation for
non-empty custom currency codes.
🪄 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: 37f5e766-91f8-4e02-be44-f8cad9ee161c
⛔ Files ignored due to path filters (37)
api/v3/openapi.yamlis excluded by!**/openapi.yamlopenmeter/ent/db/addon.gois excluded by!**/ent/db/**openmeter/ent/db/addon/addon.gois excluded by!**/ent/db/**openmeter/ent/db/addon/where.gois excluded by!**/ent/db/**openmeter/ent/db/addon_create.gois excluded by!**/ent/db/**openmeter/ent/db/addon_query.gois excluded by!**/ent/db/**openmeter/ent/db/addon_update.gois excluded by!**/ent/db/**openmeter/ent/db/addonratecard.gois excluded by!**/ent/db/**openmeter/ent/db/addonratecard/addonratecard.gois excluded by!**/ent/db/**openmeter/ent/db/addonratecard/where.gois excluded by!**/ent/db/**openmeter/ent/db/addonratecard_create.gois excluded by!**/ent/db/**openmeter/ent/db/addonratecard_query.gois excluded by!**/ent/db/**openmeter/ent/db/addonratecard_update.gois excluded by!**/ent/db/**openmeter/ent/db/client.gois excluded by!**/ent/db/**openmeter/ent/db/customcurrency.gois excluded by!**/ent/db/**openmeter/ent/db/customcurrency/customcurrency.gois excluded by!**/ent/db/**openmeter/ent/db/customcurrency/where.gois excluded by!**/ent/db/**openmeter/ent/db/customcurrency_create.gois excluded by!**/ent/db/**openmeter/ent/db/customcurrency_query.gois excluded by!**/ent/db/**openmeter/ent/db/customcurrency_update.gois excluded by!**/ent/db/**openmeter/ent/db/migrate/schema.gois excluded by!**/ent/db/**openmeter/ent/db/mutation.gois excluded by!**/ent/db/**openmeter/ent/db/plan.gois excluded by!**/ent/db/**openmeter/ent/db/plan/plan.gois excluded by!**/ent/db/**openmeter/ent/db/plan/where.gois excluded by!**/ent/db/**openmeter/ent/db/plan_create.gois excluded by!**/ent/db/**openmeter/ent/db/plan_query.gois excluded by!**/ent/db/**openmeter/ent/db/plan_update.gois excluded by!**/ent/db/**openmeter/ent/db/planratecard.gois excluded by!**/ent/db/**openmeter/ent/db/planratecard/planratecard.gois excluded by!**/ent/db/**openmeter/ent/db/planratecard/where.gois excluded by!**/ent/db/**openmeter/ent/db/planratecard_create.gois excluded by!**/ent/db/**openmeter/ent/db/planratecard_query.gois excluded by!**/ent/db/**openmeter/ent/db/planratecard_update.gois excluded by!**/ent/db/**openmeter/ent/db/runtime.gois excluded by!**/ent/db/**openmeter/ent/db/setorclear.gois excluded by!**/ent/db/**tools/migrate/migrations/atlas.sumis excluded by!**/*.sum,!**/*.sum
📒 Files selected for processing (117)
api/spec/packages/aip-client-javascript/src/index.tsapi/spec/packages/aip-client-javascript/src/models/schemas.tsapi/spec/packages/aip-client-javascript/src/models/types.tsapi/spec/packages/aip/src/currencies/currency.tspapi/spec/packages/aip/src/productcatalog/plan.tspapi/spec/packages/aip/src/productcatalog/ratecard.tspapi/v3/api.gen.goapi/v3/client/models_plans.goapi/v3/client/models_shared.goapi/v3/handlers/addons/convert.goapi/v3/handlers/plans/convert.goapi/v3/handlers/plans/convert_test.goapp/common/openmeter_billingworker.goapp/common/productcatalog.gocmd/billing-worker/wire_gen.gocmd/jobs/internal/wire.gocmd/jobs/internal/wire_gen.gocmd/server/wire_gen.goopenmeter/billing/derived.gen.goopenmeter/billing/models/stddetailedline/derived.gen.goopenmeter/billing/worker/subscriptionsync/service/base_test.goopenmeter/billing/worker/subscriptionsync/service/creditsonly_test.goopenmeter/billing/worker/subscriptionsync/service/sync_credittheninvoice_test.goopenmeter/billing/worker/subscriptionsync/service/sync_test.goopenmeter/billing/worker/subscriptionsync/service/syncbillinganchor_test.goopenmeter/currencies/adapter/currencies.goopenmeter/currencies/adapter/mapping.goopenmeter/currencies/adapter/mapping_test.goopenmeter/currencies/currency.goopenmeter/currencies/currency_test.goopenmeter/ent/schema/addon.goopenmeter/ent/schema/custom_currencies.goopenmeter/ent/schema/productcatalog.goopenmeter/ent/schema/ratecard.goopenmeter/productcatalog/adapter/feature_test.goopenmeter/productcatalog/addon.goopenmeter/productcatalog/addon/adapter/adapter_test.goopenmeter/productcatalog/addon/adapter/addon.goopenmeter/productcatalog/addon/adapter/mapping.goopenmeter/productcatalog/addon/assert.goopenmeter/productcatalog/addon/httpdriver/addon.goopenmeter/productcatalog/addon/httpdriver/mapping.goopenmeter/productcatalog/addon/ratecard.goopenmeter/productcatalog/addon/serializer.goopenmeter/productcatalog/addon/serializer_test.goopenmeter/productcatalog/addon/service.goopenmeter/productcatalog/addon/service/addon.goopenmeter/productcatalog/addon/service/service.goopenmeter/productcatalog/addon/service/service_test.goopenmeter/productcatalog/addon_test.goopenmeter/productcatalog/currency_test.goopenmeter/productcatalog/currencyresolver.goopenmeter/productcatalog/currencyresolver/resolver.goopenmeter/productcatalog/currencyresolver/resolver_test.goopenmeter/productcatalog/currencyresolver_test.goopenmeter/productcatalog/errors.goopenmeter/productcatalog/plan.goopenmeter/productcatalog/plan/adapter/adapter_test.goopenmeter/productcatalog/plan/adapter/mapping.goopenmeter/productcatalog/plan/adapter/phase.goopenmeter/productcatalog/plan/adapter/plan.goopenmeter/productcatalog/plan/assert.goopenmeter/productcatalog/plan/httpdriver/mapping.goopenmeter/productcatalog/plan/httpdriver/plan.goopenmeter/productcatalog/plan/plan.goopenmeter/productcatalog/plan/ratecard.goopenmeter/productcatalog/plan/ratecard_test.goopenmeter/productcatalog/plan/serializer.goopenmeter/productcatalog/plan/serializer_test.goopenmeter/productcatalog/plan/service.goopenmeter/productcatalog/plan/service/plan.goopenmeter/productcatalog/plan/service/service.goopenmeter/productcatalog/plan/service/service_test.goopenmeter/productcatalog/plan_test.goopenmeter/productcatalog/planaddon.goopenmeter/productcatalog/planaddon/adapter/planaddon.goopenmeter/productcatalog/planaddon/httpdriver/planaddon.goopenmeter/productcatalog/planaddon/service.goopenmeter/productcatalog/planaddon/service/planaddon.goopenmeter/productcatalog/planaddon/service/service.goopenmeter/productcatalog/planaddon/service/service_test.goopenmeter/productcatalog/planaddon_test.goopenmeter/productcatalog/ratecard.goopenmeter/productcatalog/ratecard_test.goopenmeter/productcatalog/subscription/http/mapping.goopenmeter/productcatalog/subscription/plan.goopenmeter/productcatalog/testutils/addon.goopenmeter/productcatalog/testutils/env.goopenmeter/productcatalog/testutils/plan.goopenmeter/subscription/addon/diff/restore_test.goopenmeter/subscription/addon/events_test.goopenmeter/subscription/addon/repo/subscriptionaddon.goopenmeter/subscription/serialize_test.goopenmeter/subscription/service/servicevalidation.goopenmeter/subscription/service/servicevalidation_test.goopenmeter/subscription/testutils/addon.goopenmeter/subscription/testutils/builder.goopenmeter/subscription/testutils/service.goopenmeter/subscription/workflow/service/addon_test.goopenmeter/subscription/workflow/service/subscription_test.gopkg/currencyx/code.gopkg/currencyx/code_test.gopkg/currencyx/currency.gotest/billing/subscription_suite.gotest/billing/subscription_test.gotest/customer/customer.gotest/customer/subject.gotest/customer/testenv.gotest/subscription/scenario_editaligned_test.gotest/subscription/scenario_editcancel_test.gotest/subscription/scenario_entinnextphase_test.gotest/subscription/scenario_firstofmonth_test.gotools/migrate/migrations/20260717143312_add_rate_card_currency.down.sqltools/migrate/migrations/20260717143312_add_rate_card_currency.up.sqltools/migrate/migrations/20260717143818_add_product_catalog_currency_references.down.sqltools/migrate/migrations/20260717143818_add_product_catalog_currency_references.up.sqltools/migrate/productcatalog_currency_references_test.go
There was a problem hiding this comment.
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 `@test/productcatalog/custom_currency_test.go`:
- Around line 28-31: Update newTestEnv to set POSTGRES_HOST to 127.0.0.1 before
calling pctestutils.NewTestEnv(t), ensuring the shared database-backed test
setup uses the explicit PostgreSQL host.
🪄 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: 3743cd13-8049-4302-a3ed-351a095cba2c
📒 Files selected for processing (4)
api/v3/handlers/addons/convert.goapi/v3/handlers/addons/convert_test.goe2e/v1readsurface_currencyoverride_test.gotest/productcatalog/custom_currency_test.go
6a58931 to
576a7a9
Compare
There was a problem hiding this comment.
Actionable comments posted: 2
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (1)
app/common/productcatalog.go (1)
88-138: 🎯 Functional Correctness | 🔴 Critical | 🏗️ Heavy liftBuild-breaking:
costBasisCheckerparam missing fromNewPlanService/NewAddonService/NewPlanAddonService, but wire_gen.go already calls them with it. One root cause —app/common/productcatalog.goonly addedcurrencyResolverto these three constructors this round, while both generated wire files were updated/regenerated expecting an additionalcostBasisCheckerargument that doesn't exist yet on the callee side. This won't compile.
app/common/productcatalog.go#L88-L138: add thecostBasisCheckerdependency toNewPlanService,NewAddonService, andNewPlanAddonService, threading it intoplanservice.Config/addonservice.Config/planaddonservice.Config.cmd/billing-worker/wire_gen.go#L328-L350: regenerate after the productcatalog.go fix (calls at L328, L339, L350 currently pass an extracostBasisCheckerarg the constructors don't accept) — do not hand-edit this generated file.cmd/server/wire_gen.go#L225-L350: regenerate after the productcatalog.go fix (calls at L225, L339, L350 have the same extra-argument problem) — do not hand-edit this generated file.🤖 Prompt for 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. In `@app/common/productcatalog.go` around lines 88 - 138, Add the costBasisChecker dependency to NewPlanService, NewAddonService, and NewPlanAddonService, and pass it into the corresponding planservice.Config, addonservice.Config, and planaddonservice.Config. Regenerate cmd/billing-worker/wire_gen.go lines 328-350 and cmd/server/wire_gen.go lines 225-350 after the constructor signatures are corrected; do not hand-edit generated files.Source: Path instructions
🧹 Nitpick comments (3)
openmeter/billing/charges/flatfee/charge_test.go (1)
139-157: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick winDuplicate
newCustomCurrencytest helper across two packages. Both files independently reimplement construction of a namespaced managed custom currency, whenopenmeter/currencies/testutils.NewManagedCurrency(already used elsewhere in this PR, e.g.mapping_test.go) does the same job.
openmeter/billing/charges/flatfee/charge_test.go#L139-L157: replace the localnewCustomCurrencybody with a call tocurrencies/testutils.NewManagedCurrency(t, namespace, ulid.Make().String(), "TOKENS")(or similar), dropping the localmodels/manual struct construction.openmeter/billing/charges/usagebased/charge_test.go#L138-L156: same replacement — drop the duplicate helper and delegate to the sharedNewManagedCurrencyconstructor.As per path instructions for
**/*_test.go: "avoid duplicate test helpers, and add helpers only when reused or when they express non-obvious domain semantics."🤖 Prompt for 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. In `@openmeter/billing/charges/flatfee/charge_test.go` around lines 139 - 157, Replace the duplicate newCustomCurrency helper with currencies/testutils.NewManagedCurrency in both openmeter/billing/charges/flatfee/charge_test.go:139-157 and openmeter/billing/charges/usagebased/charge_test.go:138-156, passing the test handle, namespace, generated ID, and "TOKENS" code. Remove now-unused manual currency/model construction imports while preserving existing callers.Source: Path instructions
openmeter/currencies/adapter/currencies.go (2)
369-451: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick winConsider deduplicating
WithActiveCostBasisandWithActiveAndScheduledCostBasis.These two helpers share ~90% of the same SQL predicate (deleted-currency retention branch is identical; the only difference is whether the
EffectiveFrom <= atcheck is applied). Extracting the shared "deleted-currency" branch and parameterizing the "include scheduled" behavior would reduce the chance of the two drifting apart (as nearly happened with the field-constant slip above).🤖 Prompt for 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. In `@openmeter/currencies/adapter/currencies.go` around lines 369 - 451, Deduplicate the nearly identical predicates in WithActiveCostBasis and WithActiveAndScheduledCostBasis by extracting their shared cost-basis filtering logic and parameterizing whether the active query applies the EffectiveFrom <= at constraint. Keep the deleted-currency retention branch identical in both helpers, and have each public helper delegate to the shared implementation with the appropriate scheduled behavior.
379-413: 🎯 Functional Correctness | 🔵 Trivial | 💤 Low valueUse the
customcurrencyfield constant on the joined currency table alias.
ctaliases thecustomcurrencytable here, so this comparison should usect.C(customcurrency.FieldDeletedAt)to match the adjacent predicates. As-is it’s still using the right db column name for now, but it makes the intent confusing and is easy to get wrong if the generated constants diverge.Applies to both
WithActiveCostBasisandWithActiveAndScheduledCostBasis.🤖 Prompt for 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. In `@openmeter/currencies/adapter/currencies.go` around lines 379 - 413, Update both WithActiveCostBasis and WithActiveAndScheduledCostBasis to reference the joined customcurrency table alias consistently: use ct.C(customcurrency.FieldDeletedAt) for the comparison identified by the review, matching the adjacent predicates and preserving the existing filtering behavior.
🤖 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 `@cmd/server/wire_gen.go`:
- Line 225: Update the source constructors NewAddonService, NewPlanService, and
NewPlanAddonService in productcatalog.go to accept the costBasisChecker argument
expected by their callers, then regenerate wire_gen.go using the appropriate
Make target. Do not edit the generated wire file directly.
In `@openmeter/currencies/currency.go`:
- Around line 115-141: Update WithCurrency to assign CustomCurrencyID only when
the resolved currency is custom, matching the guard used by
Currency.Reference(); leave it nil for fiat currencies so the subsequent
Validate call succeeds. Add a regression test in currency_test.go covering
successful WithCurrency resolution with a fiat Currency.
---
Outside diff comments:
In `@app/common/productcatalog.go`:
- Around line 88-138: Add the costBasisChecker dependency to NewPlanService,
NewAddonService, and NewPlanAddonService, and pass it into the corresponding
planservice.Config, addonservice.Config, and planaddonservice.Config. Regenerate
cmd/billing-worker/wire_gen.go lines 328-350 and cmd/server/wire_gen.go lines
225-350 after the constructor signatures are corrected; do not hand-edit
generated files.
---
Nitpick comments:
In `@openmeter/billing/charges/flatfee/charge_test.go`:
- Around line 139-157: Replace the duplicate newCustomCurrency helper with
currencies/testutils.NewManagedCurrency in both
openmeter/billing/charges/flatfee/charge_test.go:139-157 and
openmeter/billing/charges/usagebased/charge_test.go:138-156, passing the test
handle, namespace, generated ID, and "TOKENS" code. Remove now-unused manual
currency/model construction imports while preserving existing callers.
In `@openmeter/currencies/adapter/currencies.go`:
- Around line 369-451: Deduplicate the nearly identical predicates in
WithActiveCostBasis and WithActiveAndScheduledCostBasis by extracting their
shared cost-basis filtering logic and parameterizing whether the active query
applies the EffectiveFrom <= at constraint. Keep the deleted-currency retention
branch identical in both helpers, and have each public helper delegate to the
shared implementation with the appropriate scheduled behavior.
- Around line 379-413: Update both WithActiveCostBasis and
WithActiveAndScheduledCostBasis to reference the joined customcurrency table
alias consistently: use ct.C(customcurrency.FieldDeletedAt) for the comparison
identified by the review, matching the adjacent predicates and preserving the
existing filtering behavior.
🪄 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 Plus
Run ID: c3cdd22d-470b-4c9c-8946-9b45d7fef553
⛔ Files ignored due to path filters (13)
api/v3/openapi.yamlis excluded by!**/openapi.yamlopenmeter/ent/db/addon.gois excluded by!**/ent/db/**openmeter/ent/db/addon/addon.gois excluded by!**/ent/db/**openmeter/ent/db/addon/where.gois excluded by!**/ent/db/**openmeter/ent/db/addon_create.gois excluded by!**/ent/db/**openmeter/ent/db/addon_query.gois excluded by!**/ent/db/**openmeter/ent/db/addon_update.gois excluded by!**/ent/db/**openmeter/ent/db/addonratecard.gois excluded by!**/ent/db/**openmeter/ent/db/addonratecard/addonratecard.gois excluded by!**/ent/db/**openmeter/ent/db/addonratecard/where.gois excluded by!**/ent/db/**openmeter/ent/db/addonratecard_create.gois excluded by!**/ent/db/**openmeter/ent/db/addonratecard_query.gois excluded by!**/ent/db/**openmeter/ent/db/addonratecard_update.gois excluded by!**/ent/db/**
📒 Files selected for processing (43)
api/spec/packages/aip-client-javascript/src/index.tsapi/spec/packages/aip-client-javascript/src/models/schemas.tsapi/spec/packages/aip-client-javascript/src/models/types.tsapi/spec/packages/aip/src/currencies/currency.tspapi/spec/packages/aip/src/productcatalog/plan.tspapi/spec/packages/aip/src/productcatalog/ratecard.tspapi/v3/api.gen.goapi/v3/client/models_plans.goapi/v3/client/models_shared.goapi/v3/handlers/addons/convert.goapi/v3/handlers/addons/convert_test.goapi/v3/handlers/currencies/convert.goapi/v3/handlers/currencies/list_test.goapi/v3/handlers/plans/convert.goapi/v3/handlers/plans/convert_test.goapp/common/openmeter_billingworker.goapp/common/productcatalog.gocmd/billing-worker/wire_gen.gocmd/jobs/internal/wire_gen.gocmd/server/wire_gen.goe2e/plans_v3_test.goe2e/v1readsurface_currencyoverride_test.goopenmeter/billing/charges/flatfee/charge_test.goopenmeter/billing/charges/usagebased/charge_test.goopenmeter/billing/worker/subscriptionsync/service/base_test.goopenmeter/billing/worker/subscriptionsync/service/creditsonly_test.goopenmeter/billing/worker/subscriptionsync/service/sync_credittheninvoice_test.goopenmeter/billing/worker/subscriptionsync/service/sync_test.goopenmeter/billing/worker/subscriptionsync/service/syncbillinganchor_test.goopenmeter/currencies/adapter/currencies.goopenmeter/currencies/adapter/currencies_test.goopenmeter/currencies/adapter/mapping.goopenmeter/currencies/adapter/mapping_test.goopenmeter/currencies/costbasis.goopenmeter/currencies/costbasis_test.goopenmeter/currencies/currency.goopenmeter/currencies/currency_test.goopenmeter/currencies/currencyresolver/resolver.goopenmeter/currencies/currencyresolver/resolver_test.goopenmeter/currencies/service.goopenmeter/currencies/service/service.goopenmeter/currencies/service/service_test.goopenmeter/currencies/testutils/currency.go
🚧 Files skipped from review as they are similar to previous changes (15)
- app/common/openmeter_billingworker.go
- openmeter/billing/worker/subscriptionsync/service/base_test.go
- openmeter/billing/worker/subscriptionsync/service/syncbillinganchor_test.go
- api/spec/packages/aip/src/productcatalog/ratecard.tsp
- api/v3/client/models_shared.go
- e2e/plans_v3_test.go
- api/v3/client/models_plans.go
- api/v3/handlers/plans/convert_test.go
- openmeter/billing/worker/subscriptionsync/service/creditsonly_test.go
- api/v3/handlers/plans/convert.go
- api/v3/handlers/addons/convert.go
- openmeter/billing/worker/subscriptionsync/service/sync_test.go
- cmd/jobs/internal/wire_gen.go
- openmeter/billing/worker/subscriptionsync/service/sync_credittheninvoice_test.go
- api/v3/handlers/addons/convert_test.go
576a7a9 to
9fb97cf
Compare
There was a problem hiding this comment.
Actionable comments posted: 1
♻️ Duplicate comments (1)
openmeter/productcatalog/addon/service.go (1)
375-405: 🗄️ Data Integrity & Integration | 🟠 Major | ⚡ Quick winPreservation logic can silently clobber an explicit custom-currency ID on update.
When persisted and updated currencies share the same
Code, this always overwritesmeta.Currencywith the persisted identity — even ifupdatedCurrencyalready carries its own explicitCustomCurrencyIDpointing at a different (e.g. newly recreated) managed currency. That silently reverts an intentional identity change back to the old resource.This is the same overwrite risk flagged on an earlier version of this function (back when it used
currencyx.ManagedCurrency); the field names changed but the gap carried over into the newcurrencies.CurrencyReference-based logic. Worth guarding explicitly:🐛 Proposed guard
persistedCurrency := persistedRateCard.AsMeta().Currency updatedCurrency := rateCard.AsMeta().Currency if persistedCurrency == nil || !persistedCurrency.IsCustom() || persistedCurrency.CustomCurrencyID == nil { continue } if updatedCurrency == nil || persistedCurrency.Code != updatedCurrency.Code { continue } + if updatedCurrency.CustomCurrencyID != nil && *updatedCurrency.CustomCurrencyID != *persistedCurrency.CustomCurrencyID { + continue + }🤖 Prompt for 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. In `@openmeter/productcatalog/addon/service.go` around lines 375 - 405, Update preserveAddonRateCardCurrencyIdentities to preserve the persisted custom currency only when updatedCurrency does not already contain an explicit CustomCurrencyID. Keep the existing code-match and persisted-identity checks, but skip the ChangeMeta overwrite when the updated reference points to its own managed currency.
🧹 Nitpick comments (1)
openmeter/productcatalog/currencyresolver/resolver.go (1)
148-160: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low valueConsider pulling the currency conversion out of the
ChangeMetaclosure.The closure does a non-trivial domain conversion (
Currency.WithCurrency(...)+ error wrap) inline. As per coding guidelines, domain conversion should live in a named helper rather than inside a local closure — extracting a smallresolveRateCardCurrency(rc, resolvedCurrency)helper would keep this readable and testable in isolation.As per coding guidelines, "Do not hide non-trivial branching, validation, persistence mapping, or domain conversion inside local closures; use named helpers instead."
🤖 Prompt for 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. In `@openmeter/productcatalog/currencyresolver/resolver.go` around lines 148 - 160, The ChangeMeta closure in the rate-card currency resolution flow contains non-trivial currency conversion and error wrapping. Extract this logic into a named resolveRateCardCurrency helper accepting the rate card and resolved currency, then have the closure call that helper while retaining the existing error context and ChangeMeta behavior.Source: Coding guidelines
🤖 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/productcatalog/plan.go`:
- Around line 195-197: Update the empty currency-code branch in the plan
Validate() method to wrap ErrCurrencyInvalid with the same currency field
selector used by the subsequent validation branches. Preserve the existing
aggregated validation-error flow so API consumers receive the field context.
---
Duplicate comments:
In `@openmeter/productcatalog/addon/service.go`:
- Around line 375-405: Update preserveAddonRateCardCurrencyIdentities to
preserve the persisted custom currency only when updatedCurrency does not
already contain an explicit CustomCurrencyID. Keep the existing code-match and
persisted-identity checks, but skip the ChangeMeta overwrite when the updated
reference points to its own managed currency.
---
Nitpick comments:
In `@openmeter/productcatalog/currencyresolver/resolver.go`:
- Around line 148-160: The ChangeMeta closure in the rate-card currency
resolution flow contains non-trivial currency conversion and error wrapping.
Extract this logic into a named resolveRateCardCurrency helper accepting the
rate card and resolved currency, then have the closure call that helper while
retaining the existing error context and ChangeMeta behavior.
🪄 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 Plus
Run ID: 5fe45a1e-d134-4811-9262-616393dd35bf
📒 Files selected for processing (91)
api/v3/handlers/addons/convert.goapi/v3/handlers/addons/convert_test.goapi/v3/handlers/plans/convert.goapi/v3/handlers/plans/convert_test.goapp/common/productcatalog.gocmd/billing-worker/wire_gen.gocmd/jobs/internal/wire_gen.gocmd/server/wire_gen.goe2e/plans_v3_test.goe2e/v1readsurface_currencyoverride_test.goopenmeter/billing/worker/subscriptionsync/service/base_test.goopenmeter/billing/worker/subscriptionsync/service/creditsonly_test.goopenmeter/billing/worker/subscriptionsync/service/sync_credittheninvoice_test.goopenmeter/billing/worker/subscriptionsync/service/sync_test.goopenmeter/billing/worker/subscriptionsync/service/syncbillinganchor_test.goopenmeter/currencies/adapter/currencies.goopenmeter/currencies/adapter/currencies_test.goopenmeter/currencies/adapter/mapping.goopenmeter/currencies/adapter/mapping_test.goopenmeter/currencies/costbasis.goopenmeter/currencies/costbasis_test.goopenmeter/currencies/currency.goopenmeter/currencies/currency_test.goopenmeter/currencies/currencyresolver/resolver.goopenmeter/currencies/currencyresolver/resolver_test.goopenmeter/currencies/service/service_test.goopenmeter/currencies/testutils/currency.goopenmeter/productcatalog/addon.goopenmeter/productcatalog/addon/adapter/adapter_test.goopenmeter/productcatalog/addon/adapter/addon.goopenmeter/productcatalog/addon/adapter/mapping.goopenmeter/productcatalog/addon/httpdriver/mapping.goopenmeter/productcatalog/addon/ratecard.goopenmeter/productcatalog/addon/serializer.goopenmeter/productcatalog/addon/serializer_test.goopenmeter/productcatalog/addon/service.goopenmeter/productcatalog/addon/service/addon.goopenmeter/productcatalog/addon/service/service.goopenmeter/productcatalog/addon/service/service_test.goopenmeter/productcatalog/addon_test.goopenmeter/productcatalog/currency.goopenmeter/productcatalog/currency_test.goopenmeter/productcatalog/currencyresolver/resolver.goopenmeter/productcatalog/currencyresolver/resolver_test.goopenmeter/productcatalog/errors.goopenmeter/productcatalog/plan.goopenmeter/productcatalog/plan/adapter/adapter_test.goopenmeter/productcatalog/plan/adapter/mapping.goopenmeter/productcatalog/plan/adapter/plan.goopenmeter/productcatalog/plan/httpdriver/mapping.goopenmeter/productcatalog/plan/ratecard.goopenmeter/productcatalog/plan/ratecard_test.goopenmeter/productcatalog/plan/serializer.goopenmeter/productcatalog/plan/serializer_test.goopenmeter/productcatalog/plan/service.goopenmeter/productcatalog/plan/service/plan.goopenmeter/productcatalog/plan/service/service.goopenmeter/productcatalog/plan/service/service_test.goopenmeter/productcatalog/plan_test.goopenmeter/productcatalog/planaddon.goopenmeter/productcatalog/planaddon/adapter/planaddon.goopenmeter/productcatalog/planaddon/service/planaddon.goopenmeter/productcatalog/planaddon/service/service_test.goopenmeter/productcatalog/planaddon_test.goopenmeter/productcatalog/ratecard.goopenmeter/productcatalog/ratecard_test.goopenmeter/productcatalog/subscription/http/mapping.goopenmeter/productcatalog/testutils/addon.goopenmeter/productcatalog/testutils/currencyresolver.goopenmeter/productcatalog/testutils/env.goopenmeter/productcatalog/testutils/plan.goopenmeter/subscription/addon/diff/restore_test.goopenmeter/subscription/addon/events_test.goopenmeter/subscription/addon/repo/subscriptionaddon.goopenmeter/subscription/serialize_test.goopenmeter/subscription/service/servicevalidation_test.goopenmeter/subscription/testutils/addon.goopenmeter/subscription/testutils/builder.goopenmeter/subscription/testutils/service.goopenmeter/subscription/workflow/service/addon_test.goopenmeter/subscription/workflow/service/subscription_test.gotest/billing/subscription_suite.gotest/billing/subscription_test.gotest/customer/customer.gotest/customer/subject.gotest/customer/testenv.gotest/productcatalog/custom_currency_test.gotest/subscription/scenario_editaligned_test.gotest/subscription/scenario_editcancel_test.gotest/subscription/scenario_entinnextphase_test.gotest/subscription/scenario_firstofmonth_test.go
💤 Files with no reviewable changes (2)
- openmeter/productcatalog/addon/service/service.go
- openmeter/productcatalog/plan/service/service.go
🚧 Files skipped from review as they are similar to previous changes (51)
- openmeter/productcatalog/testutils/addon.go
- test/subscription/scenario_entinnextphase_test.go
- openmeter/subscription/addon/events_test.go
- openmeter/subscription/workflow/service/subscription_test.go
- openmeter/currencies/testutils/currency.go
- openmeter/subscription/testutils/addon.go
- test/subscription/scenario_editcancel_test.go
- test/billing/subscription_test.go
- e2e/plans_v3_test.go
- test/customer/customer.go
- openmeter/subscription/serialize_test.go
- openmeter/subscription/addon/diff/restore_test.go
- openmeter/productcatalog/testutils/plan.go
- openmeter/subscription/workflow/service/addon_test.go
- openmeter/productcatalog/plan/httpdriver/mapping.go
- test/customer/subject.go
- openmeter/currencies/adapter/mapping_test.go
- openmeter/productcatalog/addon/httpdriver/mapping.go
- openmeter/subscription/testutils/builder.go
- openmeter/productcatalog/subscription/http/mapping.go
- openmeter/subscription/addon/repo/subscriptionaddon.go
- openmeter/productcatalog/addon/adapter/adapter_test.go
- openmeter/subscription/service/servicevalidation_test.go
- openmeter/productcatalog/plan/serializer_test.go
- test/subscription/scenario_firstofmonth_test.go
- openmeter/currencies/costbasis_test.go
- api/v3/handlers/plans/convert.go
- openmeter/productcatalog/addon/serializer_test.go
- openmeter/productcatalog/addon/serializer.go
- openmeter/productcatalog/plan/adapter/adapter_test.go
- openmeter/currencies/adapter/mapping.go
- openmeter/currencies/currencyresolver/resolver.go
- openmeter/productcatalog/addon/ratecard.go
- openmeter/billing/worker/subscriptionsync/service/creditsonly_test.go
- openmeter/productcatalog/planaddon/service/service_test.go
- openmeter/currencies/currency_test.go
- app/common/productcatalog.go
- openmeter/productcatalog/addon/service/addon.go
- openmeter/productcatalog/addon/adapter/addon.go
- openmeter/billing/worker/subscriptionsync/service/syncbillinganchor_test.go
- openmeter/productcatalog/plan/ratecard.go
- openmeter/currencies/service/service_test.go
- openmeter/currencies/adapter/currencies_test.go
- openmeter/productcatalog/plan/adapter/plan.go
- openmeter/productcatalog/errors.go
- openmeter/currencies/adapter/currencies.go
- openmeter/currencies/currency.go
- openmeter/productcatalog/planaddon/service/planaddon.go
- openmeter/productcatalog/addon/adapter/mapping.go
- api/v3/handlers/plans/convert_test.go
- openmeter/billing/worker/subscriptionsync/service/sync_credittheninvoice_test.go
9955346 to
b710900
Compare
5437aab to
14ba071
Compare
What changed
Database migration
Validation
go test -tags=dynamic ./openmeter/productcatalog/... ./openmeter/subscription/servicego test -tags=dynamic ./tools/migrate -run '^TestProductCatalogCurrencyReferencesMigration$' -count=1go vet -tags=dynamic ./openmeter/productcatalog/... ./openmeter/subscription/service ./tools/migrateBuilt on the currency domain contract introduced by #4701.
Summary by CodeRabbit
New Features
Bug Fixes