Skip to content

fix: reject invalid flat price usage discounts - #4725

Merged
turip merged 2 commits into
mainfrom
feat/fix-flat-price-discount-validation
Jul 16, 2026
Merged

turip merged 2 commits into
mainfrom
feat/fix-flat-price-discount-validation

Conversation

@turip

@turip turip commented Jul 16, 2026 •

Copy link
Copy Markdown
Member

Summary

  • validate percentage and usage discounts independently
  • reject usage discounts for flat prices before invoice-line persistence
  • require non-negative usage-discount quantities in the API schema and generated clients
  • remove stored usage discounts from flat-price invoice lines while preserving other discount types

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

  • regenerated the API specifications and clients
  • ran focused billing and HTTP-driver tests
  • verified invalid flat-price usage discounts are rejected without persistence
  • replayed the data-repair migration, including mixed and deleted records and idempotency
  • ran focused Go vet checks
  • ran the complete migration schema, diff, lint, and validation checks

Summary by CodeRabbit

  • Bug Fixes
    • Discount usage quantities can now be zero, rather than requiring strictly positive values.
    • Invalid usage discounts are now rejected when applied to flat-price invoice lines.
    • Multiple discount validation errors are reported together for clearer feedback.
    • Existing flat-price invoice records are automatically cleaned up by removing incompatible usage discounts while preserving valid percentage discounts.

Greptile Summary

This PR prevents invalid usage discounts on flat-priced invoice lines and repairs existing data. The main changes are:

  • Validate percentage and usage discounts independently.
  • Reject incompatible discounts before invoice-line persistence.
  • Remove stored usage discounts from affected flat-priced lines while preserving other discounts.
  • Add focused validation, persistence, and migration tests.

Confidence Score: 5/5

This looks safe to merge.

  • The affected creation paths now reject usage discounts on flat prices.
  • The migration repairs mixed, usage-only, and soft-deleted invoice-line records.
  • No blocking issue remains in the changed code.

Important Files Changed

Filename Overview
openmeter/billing/discount.go Validates percentage and usage discounts independently and joins their validation errors.
openmeter/billing/httpdriver/deprecations.go Validates mapped discounts against the resolved price before returning the invoice-line data.
tools/migrate/migrations/20260716121405_repair_flat_price_usage_discounts.up.sql Removes incompatible usage discounts from flat-priced invoice lines and verifies the repair.
tools/migrate/repair_flat_price_usage_discounts_test.go Covers mixed, usage-only, unaffected, deleted, and idempotent migration cases.

Reviews (2): Last reviewed commit: "refactor: rely on discount validation" | Re-trigger Greptile

Context used (3)

  • Context used - CLAUDE.md (source)
  • Context used - AGENTS.md (source)
  • Context used - api/spec/AGENTS.md (source)

@turip
turip requested a review from a team as a code owner July 16, 2026 14:08
@turip turip added release-note/bug-fix Release note: Bug Fixes area/billing labels Jul 16, 2026
@coderabbitai

coderabbitai Bot commented Jul 16, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

Discount 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.

Changes

Discount validation and repair

Layer / File(s) Summary
Non-negative discount quantity contract
api/spec/packages/legacy/src/types.tsp, api/spec/packages/legacy/src/productcatalog/discounts.tsp, api/api.gen.go
Adds NonNegativeNumeric, applies it to discount quantity fields, and refreshes the embedded Swagger specification.
Runtime discount validation
openmeter/billing/discount.go, openmeter/billing/discount_test.go, openmeter/billing/httpdriver/*, test/billing/discount_test.go
Joins percentage and usage validation errors, validates parsed rate-card discounts against prices, and tests rejection and non-persistence of invalid flat-price usage discounts.
Flat-price discount data repair
tools/migrate/migrations/20260716121405_repair_flat_price_usage_discounts.*.sql, tools/migrate/repair_flat_price_usage_discounts_test.go
Removes usage entries from affected invoice-line discount JSON and verifies preserved, NULL, soft-deleted, and already-correct cases.

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
Loading

Suggested reviewers: chrisgacsal, tothandras, borosr

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
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.
Title check ✅ Passed The title matches the main change: rejecting invalid usage discounts on flat-price lines.
✨ 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/fix-flat-price-discount-validation

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.

@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.

🧹 Nitpick comments (1)
tools/migrate/migrations/20260716121405_repair_flat_price_usage_discounts.up.sql (1)

4-30: 🗄️ Data Integrity & Integration | 🔵 Trivial

Run this repair after the rollout finishes. This only cleans existing rows; during a rolling deploy, old pods can recreate the invalid usage discount 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

📥 Commits

Reviewing files that changed from the base of the PR and between 74a5b4b and eef5aba.

⛔ Files ignored due to path filters (6)
  • api/client/go/client.gen.go is excluded by !api/client/**
  • api/client/javascript/src/client/schemas.ts is excluded by !api/client/**
  • api/client/javascript/src/zod/index.ts is excluded by !api/client/**
  • api/openapi.cloud.yaml is excluded by !**/openapi.cloud.yaml
  • api/openapi.yaml is excluded by !**/openapi.yaml
  • tools/migrate/migrations/atlas.sum is excluded by !**/*.sum, !**/*.sum
📒 Files selected for processing (11)
  • api/api.gen.go
  • api/spec/packages/legacy/src/productcatalog/discounts.tsp
  • api/spec/packages/legacy/src/types.tsp
  • openmeter/billing/discount.go
  • openmeter/billing/discount_test.go
  • openmeter/billing/httpdriver/deprecations.go
  • openmeter/billing/httpdriver/deprecations_test.go
  • test/billing/discount_test.go
  • tools/migrate/migrations/20260716121405_repair_flat_price_usage_discounts.down.sql
  • tools/migrate/migrations/20260716121405_repair_flat_price_usage_discounts.up.sql
  • tools/migrate/repair_flat_price_usage_discounts_test.go

@turip

turip commented Jul 16, 2026

Copy link
Copy Markdown
Member Author

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.

@turip
turip enabled auto-merge (squash) July 16, 2026 14:35
@turip
turip merged commit 601784b into main Jul 16, 2026
27 checks passed
@turip
turip deleted the feat/fix-flat-price-discount-validation branch July 16, 2026 14:39
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