Repository navigation
feat(charges): add intent override support - #4542
Conversation
|
Caution Review failedPull request was closed or merged during review No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Path: .coderabbit.yaml Review profile: CHILL Plan: Pro Run ID: ⛔ Files ignored due to path filters (16)
📒 Files selected for processing (17)
✅ Files skipped from review due to trivial changes (1)
🚧 Files skipped from review as they are similar to previous changes (13)
📝 WalkthroughWalkthroughIntroduces a new ChangesCharge Intent Override Feature
Sequence Diagram(s)sequenceDiagram
participant Adapter as Flat-fee/Usage-based<br/>Adapter
participant UpdateCharge
participant intentoverride as intentoverride pkg
participant EntDB as Ent DB Builder
participant MapBaseFromDB as Map Function
Adapter->>UpdateCharge: UpdateCharge(ctx, charge)
UpdateCharge->>EntDB: chargemeta.Update(builder)
UpdateCharge->>intentoverride: UpdateFlatFee/UpdateUsageBased<br/>(builder, charge.IntentOverride)
intentoverride->>intentoverride: Normalize + Validate override<br/>(or clear all if nil)
intentoverride->>EntDB: Set/Clear override columns
EntDB-->>UpdateCharge: built update statement
UpdateCharge->>EntDB: Exec() on DB
EntDB-->>UpdateCharge: dbUpdatedCharge entity
UpdateCharge->>MapBaseFromDB: MapChargeBaseFromDB(dbUpdatedCharge)
MapBaseFromDB->>intentoverride: MapFlatFeeFromDB/<br/>MapUsageBasedFromDB(entity)
intentoverride-->>MapBaseFromDB: *FlatFee/*UsageBased<br/>(nil if kind absent)
MapBaseFromDB->>MapBaseFromDB: return ChargeBase{<br/>IntentOverride: ...}
MapBaseFromDB-->>UpdateCharge: (ChargeBase, error)
UpdateCharge-->>Adapter: result with populated<br/>or cleared override
Estimated code review effort🎯 4 (Complex) | ⏱️ ~60 minutes Possibly related PRs
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Warning Review ran into problems🔥 ProblemsStopped waiting for pipeline failures after 30000ms. One of your pipelines takes longer than our 30000ms fetch window to run, so review may not consider pipeline-failure results for inline comments if any failures occurred after the fetch window. Increase the timeout if you want to wait longer or run a 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 |
Greptile SummaryAdds persisted intent override support for flat-fee and usage-based charges, enabling manual invoice edits that survive future subscription sync runs. A new
Confidence Score: 5/5Safe to merge — additive migration with nullable columns only, full-replacement update semantics are consistent, and all three encoding states round-trip correctly through the DB. All new columns are nullable and additive; no existing rows are modified. The three-state field encoding (NULL / empty-string / value) is applied uniformly across Create, Update, and Map paths for both charge types. The clearOnBaseUpdater path correctly clears every column when the override is removed. Tests cover write, read, explicit clear, and stale-column scenarios. No files require special attention. Important Files Changed
|
There was a problem hiding this comment.
Actionable comments posted: 4
🤖 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/charges/flatfee/adapter/intentoverride_test.go`:
- Around line 248-260: The test helper requireExplicitClearOverrideMatches
currently only asserts a subset of override fields (Description, TaxBehavior,
FeatureKey, PercentageDiscounts) have been cleared, which means regressions
where other fields are not properly cleared won't be caught. Add assertions for
all remaining base and flat-fee override fields that should be cleared in the
explicit clear update operation to ensure comprehensive coverage of the clear
behavior.
In `@openmeter/billing/charges/models/intentoverride/flatfee.go`:
- Around line 97-130: The FlatFee.Validate() method needs to add validation for
the FeatureKey field to reject empty-string values. Add a check that verifies if
FeatureKey is present and not an empty string, appending an error to the errs
slice if FeatureKey is set to an empty string. This prevents the tri-state
semantics issue where an empty string becomes indistinguishable from a cleared
value after database round-trips, ensuring set/clear behavior remains stable and
predictable.
In `@openmeter/billing/charges/models/intentoverride/mixin.go`:
- Around line 111-146: The OverrideBase.Validate() method currently doesn't
validate that the Name field is not empty, which defers validation failure to
persistence time instead of performing domain validation here. Add a validation
check in the Validate() method (after the Kind validation check) to ensure
o.Name is not empty, and if it is empty, append an error to the errs slice using
fmt.Errorf with an appropriate message prefix like "name:", following the same
pattern used for validating other fields like Kind, TaxBehavior, ServicePeriod,
FullServicePeriod, and BillingPeriod.
In `@openmeter/billing/charges/usagebased/adapter/intentoverride_test.go`:
- Around line 242-255: The test helper function
requireExplicitClearOverrideMatches needs to be extended to assert that all
omitted base override fields are properly cleared, not just the currently
validated subset. Review the intentoverride.UsageBased struct definition to
identify all fields that should be cleared during an explicit clear operation,
then add corresponding assertions in the helper function to verify each of these
fields is either nil or empty as appropriate. This ensures the helper
comprehensively validates the full-replace semantics of UpdateCharge on clear
updates.
🪄 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: 798a7bc6-ec35-4860-9a3f-de97154ec30b
⛔ Files ignored due to path filters (16)
openmeter/ent/db/chargeflatfee.gois excluded by!**/ent/db/**openmeter/ent/db/chargeflatfee/chargeflatfee.gois excluded by!**/ent/db/**openmeter/ent/db/chargeflatfee/where.gois excluded by!**/ent/db/**openmeter/ent/db/chargeflatfee_create.gois excluded by!**/ent/db/**openmeter/ent/db/chargeflatfee_update.gois excluded by!**/ent/db/**openmeter/ent/db/chargeusagebased.gois excluded by!**/ent/db/**openmeter/ent/db/chargeusagebased/chargeusagebased.gois excluded by!**/ent/db/**openmeter/ent/db/chargeusagebased/where.gois excluded by!**/ent/db/**openmeter/ent/db/chargeusagebased_create.gois excluded by!**/ent/db/**openmeter/ent/db/chargeusagebased_update.gois excluded by!**/ent/db/**openmeter/ent/db/entmixinaccessor.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/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 (17)
api/v3/handlers/customers/charges/convert.goopenmeter/billing/charges/flatfee/adapter/charge.goopenmeter/billing/charges/flatfee/adapter/intentoverride_test.goopenmeter/billing/charges/flatfee/adapter/mapper.goopenmeter/billing/charges/flatfee/charge.goopenmeter/billing/charges/models/intentoverride/flatfee.goopenmeter/billing/charges/models/intentoverride/mixin.goopenmeter/billing/charges/models/intentoverride/usagebased.goopenmeter/billing/charges/usagebased/adapter/charge.goopenmeter/billing/charges/usagebased/adapter/intentoverride_test.goopenmeter/billing/charges/usagebased/adapter/mapper.goopenmeter/billing/charges/usagebased/charge.goopenmeter/ent/schema/chargesflatfee.goopenmeter/ent/schema/chargesusagebased.gopkg/framework/entutils/entmixinaccessor/mixinaccessor.tpltools/migrate/migrations/20260618144808_add_charge_intent_overrides.down.sqltools/migrate/migrations/20260618144808_add_charge_intent_overrides.up.sql
66bc10c to
d2c2418
Compare
Summary
Adds persisted intent override support for flat-fee and usage-based charges.
Manual edits to flat-fee or usage-based invoice output need a durable marker so the charge can preserve the original subscription-generated intent while also remembering the manually accepted effective output. This PR stores that overlay on the charge row, maps it through the charge domain objects, and reads/writes it through the flat-fee and usage-based adapters.
Business Context
When a user manually edits invoice-backed charge output, subscription sync must not silently re-materialize the invoice line from the original rate card intent and erase that accepted manual change. Persisting an intent override lets the system distinguish the original charge intent from the manually overridden effective intent.
This is the foundation for invoice edits that should survive future subscription sync runs while keeping the original generated intent available for audit and support.
Changes
intentoverridemodels and Ent mixins for common override fields.override_kindis absent.Validation
direnv exec . make generatedirenv exec . atlas migrate --env local validatedirenv exec . atlas migrate --env local diff intent_override_schema_checkenv POSTGRES_HOST=127.0.0.1 go test -tags=dynamic -count=1 ./openmeter/billing/charges/flatfee/adapter ./openmeter/billing/charges/usagebased/adapterdirenv exec . golangci-lint run --fixmake test-nocachewas intentionally left for CI per request.Summary by CodeRabbit
New Features
Tests