Repository navigation
fix: add charge discount correlation IDs - #4890
Conversation
📝 WalkthroughWalkthroughThe change adds stable correlation IDs for percentage and usage discounts. New charge writes assign missing IDs, existing eligible invoice lines receive a transactional backfill, and tests verify ID preservation through pending-line and invoice flows. ChangesDiscount correlation ID persistence
Estimated code review effort: 4 (Complex) | ~45 minutes Sequence Diagram(s)sequenceDiagram
participant ChargeIntent
participant ChargeService
participant ChargeAdapter
participant PersistedCharge
participant InvoiceLine
ChargeIntent->>ChargeService: normalized intent
ChargeService->>ChargeService: upsert discount correlation IDs
ChargeService->>ChargeAdapter: create or mutate charge
ChargeAdapter->>PersistedCharge: persist normalized discounts
PersistedCharge->>InvoiceLine: propagate discount correlation ID
Possibly related PRs
Suggested reviewers: 🚥 Pre-merge checks | ✅ 4✅ Passed checks (4 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.
Actionable comments posted: 1
🧹 Nitpick comments (2)
openmeter/billing/discount.go (1)
47-58: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick winDocument the correlation ID contract.
Add Go doc comments for these helpers. State that they preserve an existing ID, generate an ID only when it is empty, and do not mutate the input value.
As per coding guidelines, "Document domain helpers whose names compress important business semantics, including observable behavior and why excluded cases are excluded."
Also applies to: 99-110, 203-207
🤖 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/discount.go` around lines 47 - 58, Add Go doc comments to the correlation-ID helper methods, including PercentageDiscount.UpsertCorrelationID and the corresponding helpers around the referenced ranges. Document that each preserves an existing ID, generates one only when the ID is empty, and returns a copy without mutating the input value.Source: Coding guidelines
openmeter/billing/charges/usagebased/service/statemachine.go (1)
144-152: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick winMove discount-correlation normalization into a named helper.
The local closure applies a domain mutation to three intent-layer paths. Move this rule to a private named helper and keep the switch focused on layer selection. This makes the persistence invariant easier to find and test.
As per coding guidelines, do not hide meaningful domain translation inside local closures; use named helpers and reserve inline callbacks for obvious, tiny logic.
Also applies to: 156-169
🤖 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/usagebased/service/statemachine.go` around lines 144 - 152, The local editWithDiscountCorrelationIDs closure should be replaced with a private named helper that applies UpsertCorrelationIDs to usagebased.IntentMutableFields after invoking the provided edit function. Update all three intent-layer paths, including the switch cases around the referenced closure, to call the named helper while keeping the switch focused on selecting the appropriate layer.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
`@tools/migrate/migrations/20260809152600_backfill_charge_discount_correlation_ids.up.sql`:
- Around line 8-34: Update the candidate SELECTs in
tools/migrate/migrations/20260809152600_backfill_charge_discount_correlation_ids.up.sql
(lines 8-34) to use l.id as correlation_id and select rows when the applicable
discount object exists, removing the requirement for a nested correlationID.
Update tools/migrate/charge_discount_correlation_ids_test.go (lines 29-41) to
expect source invoice-line IDs for repaired discounts, and add source discount
objects without correlation IDs in lines 102-118 to verify each invoice-line ID
is reused.
---
Nitpick comments:
In `@openmeter/billing/charges/usagebased/service/statemachine.go`:
- Around line 144-152: The local editWithDiscountCorrelationIDs closure should
be replaced with a private named helper that applies UpsertCorrelationIDs to
usagebased.IntentMutableFields after invoking the provided edit function. Update
all three intent-layer paths, including the switch cases around the referenced
closure, to call the named helper while keeping the switch focused on selecting
the appropriate layer.
In `@openmeter/billing/discount.go`:
- Around line 47-58: Add Go doc comments to the correlation-ID helper methods,
including PercentageDiscount.UpsertCorrelationID and the corresponding helpers
around the referenced ranges. Document that each preserves an existing ID,
generates one only when the ID is empty, and returns a copy without mutating the
input value.
🪄 Autofix
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: 8a9515c1-8bec-4b9e-87d4-447a471d92cf
⛔ Files ignored due to path filters (1)
tools/migrate/migrations/atlas.sumis excluded by!**/*.sum,!**/*.sum
📒 Files selected for processing (15)
openmeter/billing/charges/README.mdopenmeter/billing/charges/flatfee/adapter/charge.goopenmeter/billing/charges/flatfee/adapter/intentoverride.goopenmeter/billing/charges/flatfee/service/create.goopenmeter/billing/charges/flatfee/service/statemachine.goopenmeter/billing/charges/service/pendinglines_test.goopenmeter/billing/charges/usagebased/adapter/charge.goopenmeter/billing/charges/usagebased/adapter/intentoverride.goopenmeter/billing/charges/usagebased/service/create.goopenmeter/billing/charges/usagebased/service/statemachine.goopenmeter/billing/discount.goopenmeter/billing/discount_test.gotools/migrate/charge_discount_correlation_ids_test.gotools/migrate/migrations/20260809152600_backfill_charge_discount_correlation_ids.down.sqltools/migrate/migrations/20260809152600_backfill_charge_discount_correlation_ids.up.sql
|
Review follow-up in 75e048c:
Focused Go tests, the PostgreSQL migration regression, Atlas validation, and Atlas lint all pass locally. |
|
approved |
Summary
Root cause
billing.PercentageDiscountandbilling.UsageDiscountextend product-catalog discounts with a correlation ID, but their promoted validators only validate the embedded commercial fields. Charge creation could therefore persist billing discounts without IDs. After charge-backed rating began consuming the persisted discounts directly, detailed-line generation rejected those entities withdiscount has no correlation ID.Repair scope
The migration repairs only active
billing_gathering_invoice_linesrecords wherecharge_id IS NOT NULL. For each present percentage or usage discount with a missing ID, it assigns an arbitrary generated ULID while preserving existing IDs. Standard invoice lines, charge rows, and override rows are intentionally not migrated.New charge writes allocate IDs immediately. Existing base and override intents self-heal on their next persisted state-machine update because the charge and override adapters defensively upsert missing IDs at their write boundaries.
Impact
Existing affected gathering lines can be rated after migration. New and subsequently updated charge intents persist stable discount identities, and rerating, invoice projection, and corrections reuse those persisted IDs.
Validation
go test ./openmeter/billing -run 'Test.*Correlation' -count=1go test ./openmeter/billing/charges/flatfee/service ./openmeter/billing/charges/usagebased/service -count=1POSTGRES_HOST=127.0.0.1 go test ./tools/migrate -run TestBackfillChargeDiscountCorrelationIDs -count=1make migrate-check-lintmake migrate-check-validate