Skip to content

feat: persist invoice id for ubp runs - #4306

Merged
turip merged 1 commit into
mainfrom
split/rating-scaffolding
May 6, 2026
Merged

turip merged 1 commit into
mainfrom
split/rating-scaffolding

Conversation

@turip

@turip turip commented May 6, 2026 •

Copy link
Copy Markdown
Member

Overview

Fixes #(issue)

Notes for reviewer

Summary by CodeRabbit

  • New Features

    • Added support for associating realization runs with invoices, enabling better tracking of charge realizations linked to specific invoices.
  • Tests

    • Added assertions to verify correct linkage between realization runs and their corresponding invoices.

@turip
turip requested a review from a team as a code owner May 6, 2026 15:15
@coderabbitai

coderabbitai Bot commented May 6, 2026 •

Copy link
Copy Markdown
Contributor

Caution

Review failed

Failed to post review comments

📝 Walkthrough

Walkthrough

This PR adds optional invoice association tracking to usage-based realization runs by introducing an InvoiceID field throughout the realization run pipeline—from database schema and entity definitions through the service layer to test assertions, enabling runs to be linked to their corresponding invoices.

Changes

Invoice Linkage for Realization Runs

Layer / File(s) Summary
Database Schema
tools/migrate/migrations/20260506134501_add_usage_based_run_invoice_id.{up,down}.sql
Adds nullable invoice_id column (char(26)) to charge_usage_based_runs with a foreign key to billing_invoices(id), with ON DELETE SET NULL behavior.
Entity Schema
openmeter/ent/schema/chargesusagebased.go, openmeter/ent/schema/billing.go
Adds invoice_id field and edge to ChargeUsageBasedRuns; adds reverse edge from BillingInvoice to ChargeUsageBasedRuns.
Type Definitions
openmeter/billing/charges/usagebased/realizationrun.go
Introduces InvoiceID *string field to both CreateRealizationRunInput and RealizationRunBase with validation to ensure non-empty when provided.
Adapter & Mapper
openmeter/billing/charges/usagebased/adapter/mapper.go, openmeter/billing/charges/usagebased/adapter/realizationrun.go
Maps InvoiceID from the database row into RealizationRunBase and wires it into the realization run creation chain via SetNillableBillingInvoiceID.
Service Integration
openmeter/billing/charges/usagebased/service/run/create.go, openmeter/billing/charges/usagebased/service/creditheninvoice.go, openmeter/billing/charges/usagebased/service/lineengine.go
Adds InvoiceID field to CreateRatedRunInput, carries it through from invoiceCreatedInput in the credit-then-invoice flow, and passes it into realization run creation.
Tests & Validation
openmeter/billing/charges/service/usagebased_test.go, openmeter/billing/charges/usagebased/service/creditheninvoice_test.go
Adds assertions verifying realization runs are linked to their invoices; updates test fixtures to include the new InvoiceID field.

Sequence Diagram

sequenceDiagram
    participant LineEngine
    participant CreditThenInvoiceService
    participant RunService
    participant Database

    LineEngine->>LineEngine: OnStandardInvoiceCreated
    LineEngine->>CreditThenInvoiceService: FireAndActivate with invoiceCreatedInput<br/>(includes InvoiceID from Invoice.ID)
    CreditThenInvoiceService->>RunService: startInvoiceCreatedRun with<br/>CreateRatedRunInput<br/>(carries InvoiceID forward)
    RunService->>RunService: CreateRatedRun validates<br/>and propagates InvoiceID
    RunService->>Database: CreateRealizationRunInput<br/>(InvoiceID set)
    Database->>Database: Inserts charge_usage_based_runs<br/>with invoice_id foreign key
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Possibly related PRs

  • openmeterio/openmeter#4143: Adds line_id foreign key and linkage to ChargeUsageBasedRuns following the same pattern of associating runs with invoice line items.
  • openmeterio/openmeter#4254: Modifies the same service and run creation paths (creditheninvoice.go, run/create.go) to handle invoice-related run persistence and detailed-line upserts.
  • openmeterio/openmeter#4255: Extends ChargeUsageBasedRuns entity and realization-run creation/mapping with additional persisted fields alongside this PR's invoice_id.

Suggested labels

area/billing, release-note/misc

Suggested reviewers

  • tothandras
  • GAlexIHU
  • chrisgacsal
🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% 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
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'feat: persist invoice id for ubp runs' clearly and concisely summarizes the main change: adding persistent invoice ID tracking for usage-based pricing (UBP) runs across the codebase.
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.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ 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 split/rating-scaffolding

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 and usage tips.

@turip turip added release-note/misc Miscellaneous changes area/billing labels May 6, 2026
@turip
turip enabled auto-merge (squash) May 6, 2026 15:20
@turip
turip merged commit e4ccf0b into main May 6, 2026
28 of 30 checks passed
@turip
turip deleted the split/rating-scaffolding branch May 6, 2026 15:25

This branch was previously deployed

1 inactive deployment
dev — b132f693 Deployed May 6, 2026 by turip via Migration Checks #14156
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