Skip to content

feat: add usage quantity detail to surface unit config on invoices api - OM-410 - #4858

Merged
rolosp merged 1 commit into
mainfrom
feat/om-410-surface-unitconfig-on-invoice-api
Aug 6, 2026
Merged

rolosp merged 1 commit into
mainfrom
feat/om-410-surface-unitconfig-on-invoice-api

Conversation

@rolosp

@rolosp rolosp commented Aug 5, 2026 •

Copy link
Copy Markdown
Contributor

What

Surface the UnitConfig quantity audit trail on the v3 invoice standard line. A new read-only usage_quantity_detail field (BillingInvoiceUsageQuantityDetail: raw_quantity, invoiced_quantity, display_unit) is emitted per standard line whenever the line carries a unit_config. Wires the previously-orphaned InvoiceUsageQuantityDetail TypeSpec model onto the standard line and populates it in the v3 converter (mapStandardLine → new mapUsageQuantityDetail). Additive; identity when no unit_config.

Why

We exposed (#4730) the unit_config rule on the invoice line's rate card, but the invoice API surfaced no quantities to go with it — the only quantity anywhere was the already-converted Quantity on each detailed line, with no raw meter value and no unit. So a converted line ("2 GB @ $5") could not be traced back to the meter reading it came from. usage_quantity_detail provides that trace — raw metered → invoiced (billed) quantity + display unit — completing the audit trail, and it reads identically across both billing paths.

How

Pure read-side exposure of data we already persists — no schema, migration, adapter, or engine changes. raw_quantity and invoiced_quantity map straight from the stored UsageBased.MeteredQuantity / .Quantity on the domain standard line, so no recompute is needed (the linear UnitConfig.Apply isn't even called on this path). It's populated in the single v3 converter both engines funnel through, so legacy and charges are covered by one mapping; gated on GetUnitConfig() != nil so non unit_config lines are unchanged — which also covers the charges custom-currency path (no config). Two fields were deliberately trimmed from the orphan model: converted_quantity (the pre-rounding value, derivable as raw × factor and omitted to keep this a lean stored-fields-only object) and applied_unit_config (a full duplicate of rate_card.unit_config, already on the same line since item A — kept a single source of truth for the config). Covered by converter unit tests plus a DB-backed integration test that runs a real invoice through ToAPIStandardInvoice and asserts the audit-trail math end to end (7400 raw → ceil(7400/1000) = 8 GB).

Summary by CodeRabbit

  • New Features

    • Invoice lines can now include raw quantity, invoiced quantity, and display-unit details for usage-based billing with unit conversion.
    • Added support for accessing usage quantity details through the billing invoice API and client models.
  • Updates

    • Simplified usage details by removing converted quantity and unit-configuration snapshot fields.
  • Tests

    • Added coverage for displaying quantity details and omitting them when required data is unavailable.

Greptile Summary

The PR adds a read-only usage quantity audit trail to v3 standard invoice lines when unit conversion is configured.

  • Exposes raw metered quantity, net invoiced quantity, and the optional display unit.
  • Maps persisted billing quantities through the shared v3 invoice converter.
  • Regenerates the OpenAPI, Go client, and JavaScript client models.
  • Adds converter-level and database-backed integration coverage.

Confidence Score: 5/5

The PR appears safe to merge.

No blocking failure remains.

Important Files Changed

Filename Overview
api/spec/packages/aip/src/invoices/invoice.tsp Adds the optional read-only usage quantity detail to standard invoice responses.
api/spec/packages/aip/src/productcatalog/unitconfig.tsp Defines the response-only three-field quantity audit model used by invoice lines.
api/v3/handlers/billinginvoices/convert.go Maps stored raw and invoiced quantities plus the display unit, omitting the detail when conversion or quantities are absent.
api/v3/handlers/billinginvoices/convert_test.go Covers populated, no-unit-config, and missing-quantity converter behavior.
test/billing/unitconfig_legacy_test.go Verifies the persisted legacy billing quantities appear correctly through the v3 invoice converter.
api/v3/openapi.yaml Publishes the optional usage quantity detail in the generated v3 API contract.
api/spec/packages/aip-client-javascript/src/models/types.ts Regenerates JavaScript client types for the new invoice response field and its simplified detail model.
api/v3/client/models_invoices.go Regenerates Go client invoice models with the optional quantity detail.

Sequence Diagram

sequenceDiagram
  participant Billing as Billing domain
  participant Converter as v3 invoice converter
  participant API as Invoice API
  participant Client as API client
  Billing->>Converter: StandardLine with UnitConfig and stored quantities
  Converter->>Converter: Map MeteredQuantity to raw_quantity
  Converter->>Converter: Map Quantity to invoiced_quantity
  Converter->>Converter: Copy UnitConfig display_unit
  Converter->>API: BillingInvoiceStandardLine
  API-->>Client: Optional usage_quantity_detail
Loading

Reviews (3): Last reviewed commit: "feat: add usage quantity detail to surfa..." | Re-trigger Greptile

Context used:

@coderabbitai

coderabbitai Bot commented Aug 5, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

The invoice API now exposes optional usage quantity details on standard invoice lines. The detail contains raw quantity, invoiced quantity, and display unit. The converter omits it when unit configuration or required quantities are missing. Generated models and tests were updated.

Changes

Invoice usage quantity details

Layer / File(s) Summary
Usage quantity contract models
api/spec/packages/aip/..., api/spec/packages/aip-client-javascript/src/models/*, api/spec/packages/aip-client-javascript/src/index.ts
The contract and JavaScript models add optional usage quantity details. The detail uses raw quantity, invoiced quantity, and display unit. Converted quantity, applied unit configuration, and the obsolete input type are removed.
Generated and client API models
api/v3/api.gen.go, api/v3/client/models_invoices.go
Generated and client Go models add UsageQuantityDetail and InvoiceUsageQuantityDetail. The embedded OpenAPI specification is regenerated.
Invoice conversion mapping
api/v3/handlers/billinginvoices/convert.go
Invoice conversion includes usage quantity details when the line has usage configuration, unit configuration, and both metered and rated quantities.
Conversion and end-to-end validation
api/v3/handlers/billinginvoices/convert_test.go, test/billing/unitconfig_legacy_test.go
Tests cover populated details, omitted details, missing quantities, and an end-to-end unit conversion invoice.

Estimated code review effort: 3 (Moderate) | ~25 minutes

Sequence Diagram(s)

sequenceDiagram
  participant InvoiceLine
  participant BillingInvoiceConverter
  participant V3BillingInvoiceAPI
  InvoiceLine->>BillingInvoiceConverter: provide usage configuration and rated quantities
  BillingInvoiceConverter->>BillingInvoiceConverter: validate configuration and quantities
  BillingInvoiceConverter->>V3BillingInvoiceAPI: attach UsageQuantityDetail
  V3BillingInvoiceAPI->>V3BillingInvoiceAPI: expose raw quantity, invoiced quantity, and display unit
Loading

Possibly related PRs

Suggested labels: kind/feature, area/billing

Suggested reviewers: tothandras, borosr

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 12.50% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
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 clearly identifies the main change: adding usage quantity details to the invoice API.
✨ Finishing Touches 💡 1
📝 Generate docstrings 💡
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch feat/om-410-surface-unitconfig-on-invoice-api

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.

@rolosp
rolosp force-pushed the feat/om-410-surface-unitconfig-on-invoice-api branch from 5039f53 to 17d0298 Compare August 5, 2026 15:29
@rolosp rolosp added the release-note/feature Release note: Exciting New Features label Aug 5, 2026
@rolosp
rolosp marked this pull request as ready for review August 5, 2026 15:30
@rolosp
rolosp requested a review from a team as a code owner August 5, 2026 15:30
@turip
turip requested review from borosr and tothandras August 5, 2026 15:32

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

Actionable comments posted: 1

🧹 Nitpick comments (1)
test/billing/unitconfig_legacy_test.go (1)

156-217: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚖️ Poor tradeoff

Optional: pull the shared invoice setup into one suite fixture.

This block repeats the setup from TestRatesConvertedQuantity almost verbatim: billing profile, service period, pending line, the 7400 event, and the invoice collection. Only the display unit and the namespace differ. A suite helper that takes the *productcatalog.UnitConfig and returns the invoiced standard line would remove roughly sixty duplicated lines and keep the two tests focused on their distinct assertions. Fine to defer if you prefer each e2e test to read start to finish on its own.

🤖 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 `@test/billing/unitconfig_legacy_test.go` around lines 156 - 217, Extract the
repeated billing setup in this test block and the nearby
TestRatesConvertedQuantity path into a shared suite helper that provisions the
billing profile, creates the pending invoice line, adds the 7400 usage event,
and returns the invoiced standard line. Keep the helper parameterized by the
existing unit-config symbols, especially the *productcatalog.UnitConfig, so the
tests only differ in their display unit and namespace while preserving the
current assertions.
🤖 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 `@api/v3/handlers/billinginvoices/convert.go`:
- Around line 321-326: Update the stale prose around
BillingInvoiceUsageQuantityDetail so it no longer describes the removed
applied_unit_config field: in toAPIUsageQuantityDetail, revise the comment to
say the detail carries raw_quantity, invoiced_quantity, and the configured
display unit, and explicitly note that the config itself remains on
rate_card.unit_config; in the matching convert_test then-comment, remove the
reference to a config snapshot so it matches the three assertions that remain.

---

Nitpick comments:
In `@test/billing/unitconfig_legacy_test.go`:
- Around line 156-217: Extract the repeated billing setup in this test block and
the nearby TestRatesConvertedQuantity path into a shared suite helper that
provisions the billing profile, creates the pending invoice line, adds the 7400
usage event, and returns the invoiced standard line. Keep the helper
parameterized by the existing unit-config symbols, especially the
*productcatalog.UnitConfig, so the tests only differ in their display unit and
namespace while preserving the current assertions.
🪄 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: e086cf31-38ad-4478-a406-fdeef6ec1c54

📥 Commits

Reviewing files that changed from the base of the PR and between 0f8b7cc and 17d0298.

⛔ Files ignored due to path filters (1)
  • api/v3/openapi.yaml is excluded by !**/openapi.yaml
📒 Files selected for processing (10)
  • api/spec/packages/aip-client-javascript/src/index.ts
  • api/spec/packages/aip-client-javascript/src/models/schemas.ts
  • api/spec/packages/aip-client-javascript/src/models/types.ts
  • api/spec/packages/aip/src/invoices/invoice.tsp
  • api/spec/packages/aip/src/productcatalog/unitconfig.tsp
  • api/v3/api.gen.go
  • api/v3/client/models_invoices.go
  • api/v3/handlers/billinginvoices/convert.go
  • api/v3/handlers/billinginvoices/convert_test.go
  • test/billing/unitconfig_legacy_test.go

@rolosp
rolosp force-pushed the feat/om-410-surface-unitconfig-on-invoice-api branch from 17d0298 to 8cf8b87 Compare August 5, 2026 16:13
@coderabbitai

coderabbitai Bot commented Aug 5, 2026

Copy link
Copy Markdown
Contributor

Note

GitHub couldn't provide a complete incremental comparison for this pull request, so CodeRabbit is performing a full review instead. This review may take a little longer.

@rolosp rolosp changed the title feat: add usage quantity detail to surface unit config on invoices api feat: add usage quantity detail to surface unit config on invoices api - OM-410 Aug 5, 2026
@rolosp
rolosp merged commit 5aa70a1 into main Aug 6, 2026
30 checks passed
@rolosp
rolosp deleted the feat/om-410-surface-unitconfig-on-invoice-api branch August 6, 2026 11:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

release-note/feature Release note: Exciting New Features

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants