Repository navigation
fix: reject invalid flat price usage discounts - #4725
Conversation
📝 WalkthroughWalkthroughDiscount quantity schemas now allow non-negative values. Runtime validation accumulates discount errors and rejects usage discounts on flat prices. A transactional migration removes existing invalid usage discounts, with unit, integration, persistence, and migration coverage. ChangesDiscount validation and repair
Estimated code review effort: 4 (Complex) | ~45 minutes Sequence Diagram(s)sequenceDiagram
participant RateCardParser
participant DiscountsValidateForPrice
participant Price
participant BillingPersistence
RateCardParser->>DiscountsValidateForPrice: ValidateForPrice(price)
DiscountsValidateForPrice->>Price: Validate percentage and usage discounts
Price-->>DiscountsValidateForPrice: Validation errors
DiscountsValidateForPrice-->>RateCardParser: Joined validation result
RateCardParser->>BillingPersistence: Reject invalid flat-price usage discount
Suggested reviewers: 🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches📝 Generate docstrings
🧪 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.
🧹 Nitpick comments (1)
tools/migrate/migrations/20260716121405_repair_flat_price_usage_discounts.up.sql (1)
4-30: 🗄️ Data Integrity & Integration | 🔵 TrivialRun this repair after the rollout finishes. This only cleans existing rows; during a rolling deploy, old pods can recreate the invalid
usagediscount after the cleanup. Gate legacy writers first or apply the repair once all writers are on the fixed build.🤖 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 `@tools/migrate/migrations/20260716121405_repair_flat_price_usage_discounts.up.sql` around lines 4 - 30, Ensure this repair migration runs only after all legacy writers are upgraded or otherwise gated, since older pods can recreate the invalid usage discount. Update the rollout or migration sequencing around the repair so it is applied after the fixed build is fully deployed.
🤖 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.
Nitpick comments:
In
`@tools/migrate/migrations/20260716121405_repair_flat_price_usage_discounts.up.sql`:
- Around line 4-30: Ensure this repair migration runs only after all legacy
writers are upgraded or otherwise gated, since older pods can recreate the
invalid usage discount. Update the rollout or migration sequencing around the
repair so it is applied after the fixed build is fully deployed.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
Run ID: cd57421d-bd6c-46aa-8328-e9a8b0435a49
⛔ Files ignored due to path filters (6)
api/client/go/client.gen.gois excluded by!api/client/**api/client/javascript/src/client/schemas.tsis excluded by!api/client/**api/client/javascript/src/zod/index.tsis excluded by!api/client/**api/openapi.cloud.yamlis excluded by!**/openapi.cloud.yamlapi/openapi.yamlis excluded by!**/openapi.yamltools/migrate/migrations/atlas.sumis excluded by!**/*.sum,!**/*.sum
📒 Files selected for processing (11)
api/api.gen.goapi/spec/packages/legacy/src/productcatalog/discounts.tspapi/spec/packages/legacy/src/types.tspopenmeter/billing/discount.goopenmeter/billing/discount_test.goopenmeter/billing/httpdriver/deprecations.goopenmeter/billing/httpdriver/deprecations_test.gotest/billing/discount_test.gotools/migrate/migrations/20260716121405_repair_flat_price_usage_discounts.down.sqltools/migrate/migrations/20260716121405_repair_flat_price_usage_discounts.up.sqltools/migrate/repair_flat_price_usage_discounts_test.go
|
CodeRabbit rollout note: this rolling-deployment window is known and accepted for this change. The combination is extremely rare, and the surrounding operation still fails, so no additional migration sequencing safeguard is warranted here. |
Summary
Issue
The legacy validation flow returned after validating a percentage discount. When percentage and usage discounts were both present, an invalid usage discount could therefore bypass price compatibility validation and be stored on a flat-price invoice line.
Flat-price rating does not apply usage discounts, so these stored values were invalid configuration rather than an effective price adjustment.
Validation
Summary by CodeRabbit
Greptile Summary
This PR prevents invalid usage discounts on flat-priced invoice lines and repairs existing data. The main changes are:
Confidence Score: 5/5
This looks safe to merge.
Important Files Changed
Reviews (2): Last reviewed commit: "refactor: rely on discount validation" | Re-trigger Greptile
Context used (3)