Skip to content

feat(billing): dual-write tax_code_id/tax_behavior on invoice line adapter paths#4049

Merged
borbelyr-kong merged 4 commits intomainfrom
feat/tax-codes-double-read-write-billinglines
Apr 1, 2026
Merged

feat(billing): dual-write tax_code_id/tax_behavior on invoice line adapter paths#4049
borbelyr-kong merged 4 commits intomainfrom
feat/tax-codes-double-read-write-billinglines

Conversation

@borbelyr-kong
Copy link
Copy Markdown
Contributor

@borbelyr-kong borbelyr-kong commented Apr 1, 2026

Summary by CodeRabbit

  • Bug Fixes

    • Enhanced tax code persistence and synchronization across invoice lines, gathering lines, and split line groups to ensure complete tax normalization data is retained.
    • Improved tax code and tax behavior retrieval to include comprehensive tax details during invoice queries and operations.
  • Tests

    • Expanded tax configuration tests to verify tax normalization fields are properly persisted in the database.

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai bot commented Apr 1, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 803ce7bb-91e5-4537-8f31-71d3c496a32d

📥 Commits

Reviewing files that changed from the base of the PR and between 190e8e3 and a8bdfab.

📒 Files selected for processing (3)
  • openmeter/billing/adapter/gatheringlines.go
  • openmeter/billing/adapter/stdinvoicelines.go
  • test/billing/tax_test.go
✅ Files skipped from review due to trivial changes (1)
  • openmeter/billing/adapter/stdinvoicelines.go

📝 Walkthrough

Walkthrough

Tax handling in billing adapters is expanded to persist and query tax code IDs and tax behavior alongside TaxConfig, with mapping logic refactored to use BackfillTaxConfig for reconstruction instead of direct conversion. Changes span invoice line persistence, split line groups, and query expansion across multiple adapter files.

Changes

Cohort / File(s) Summary
Standard Invoice Lines
openmeter/billing/adapter/stdinvoicelines.go
Extended upsert and create operations to persist TaxCodeID and TaxBehavior fields; updated line queries to eagerly load tax code edges via WithTaxCode().
Standard Invoice Mapping
openmeter/billing/adapter/stdinvoicelinemapper.go
Replaced direct TaxConfig assignment with productcatalog.BackfillTaxConfig() across multiple mapping functions; added helper functions taxCodeFromInvoiceLineEdge and taxCodeFromDetailedLineV2Edge to extract and map tax codes from DB edges.
Gathering Invoice Lines
openmeter/billing/adapter/gatheringlines.go
Updated persistence to sync TaxCodeID and TaxBehavior on create and upsert; changed read mapping to reconstruct TaxConfig via BackfillTaxConfig() instead of direct conversion.
Gathering Invoice Query
openmeter/billing/adapter/gatheringinvoice.go
Extended expanded line queries to include tax code edge loading via WithTaxCode().
Split Line Groups
openmeter/billing/adapter/invoicelinesplitgroup.go
Added conditional persistence of TaxCodeID and TaxBehavior during create and update; updated all split line group read paths to include WithTaxCode(); introduced taxCodeFromSplitLineGroupEdge() helper and switched mapping to BackfillTaxConfig().
Tax Configuration Tests
test/billing/tax_test.go
Enhanced test to directly verify persistence of tax normalization fields (tax_code_id, tax_behavior) in database rows across invoice lines and split line groups.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~22 minutes

Possibly related PRs

Suggested reviewers

  • GAlexIHU
  • tothandras
🚥 Pre-merge checks | ✅ 2 | ❌ 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 (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and specifically describes the main change: dual-writing tax_code_id and tax_behavior fields across invoice line adapter paths, which aligns perfectly with the changeset.

✏️ 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 feat/tax-codes-double-read-write-billinglines

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.

@borbelyr-kong borbelyr-kong added release-note/ignore Ignore this change when generating release notes area/billing labels Apr 1, 2026
Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
openmeter/billing/adapter/stdinvoicelines.go (1)

147-153: ⚠️ Potential issue | 🟠 Major

Conflict updates still leave the legacy tax_config stale.

These two upserts now refresh tax_code_id and tax_behavior, but they still do not update tax_config on conflict. Since BackfillTaxConfig only fills missing fields, an old JSON blob can keep the previous Stripe/behavior and mask clears or tax-code switches for top-level lines and schema-level-1 detailed lines. Adding UpdateTaxConfig() here keeps the dual-write path consistent.

💡 Suggested fix
 				UpdateQuantity().
 				UpdateChildUniqueReferenceID().
 				UpdateCreditsApplied().
 				UpdateChargeID().
+				UpdateTaxConfig().
 				UpdateTaxCodeID().
 				UpdateTaxBehavior().
 				Exec(ctx)
 				UpdateQuantity().
 				UpdateChildUniqueReferenceID().
 				UpdateCreditsApplied().
+				UpdateTaxConfig().
 				UpdateTaxCodeID().
 				UpdateTaxBehavior().
 				Exec(ctx)

Also applies to: 347-352

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@openmeter/billing/adapter/stdinvoicelines.go` around lines 147 - 153, The
upsert conflict update chain is missing UpdateTaxConfig(), leaving legacy
tax_config JSON stale; in the upsert chains that call
UpdateQuantity().UpdateChildUniqueReferenceID().UpdateCreditsApplied().UpdateChargeID().UpdateTaxCodeID().UpdateTaxBehavior().Exec(ctx)
add .UpdateTaxConfig() before .Exec(ctx) so tax_config is refreshed on conflict;
make the identical change to the other equivalent upsert chain that mirrors
these updates (the second block that currently omits UpdateTaxConfig()) so both
dual-write paths stay consistent.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@openmeter/billing/adapter/gatheringlines.go`:
- Around line 245-247: The conflict-resolution update chain
(UpdateChildUniqueReferenceID, UpdateTaxCodeID, UpdateTaxBehavior) omits
updating the legacy tax_config JSON, causing mapGatheringInvoiceLineFromDB
(which calls BackfillTaxConfig) to read stale tax info; add UpdateTaxConfig()
into that chained update so the tax_config JSON column is overwritten on
conflict and stays consistent with tax_code_id/tax_behavior changes.

In `@openmeter/billing/adapter/invoicelinesplitgroup.go`:
- Around line 168-178: The mapper taxCodeFromSplitLineGroupEdge assumes the
TaxCode edge is loaded but some callers (CreateSplitLineGroup,
UpdateSplitLineGroup and GetSplitLineGroupHeaders) do not include WithTaxCode(),
leading to partial TaxConfig; fix by either reloading the entity with
WithTaxCode() before mapping in those callers or add a fallback inside
taxCodeFromSplitLineGroupEdge that uses the raw dbGroup.TaxCodeID (or similar
tax_code_id field) to resolve/map a TaxCode via taxcodeadapter when the edge is
missing; update both the occurrences around lines mentioned (including the block
at 224-228) so all code paths return a consistent TaxCode-derived result.

---

Outside diff comments:
In `@openmeter/billing/adapter/stdinvoicelines.go`:
- Around line 147-153: The upsert conflict update chain is missing
UpdateTaxConfig(), leaving legacy tax_config JSON stale; in the upsert chains
that call
UpdateQuantity().UpdateChildUniqueReferenceID().UpdateCreditsApplied().UpdateChargeID().UpdateTaxCodeID().UpdateTaxBehavior().Exec(ctx)
add .UpdateTaxConfig() before .Exec(ctx) so tax_config is refreshed on conflict;
make the identical change to the other equivalent upsert chain that mirrors
these updates (the second block that currently omits UpdateTaxConfig()) so both
dual-write paths stay consistent.
🪄 Autofix (Beta)

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

Run ID: f63b03ac-7488-481a-9407-914237879b81

📥 Commits

Reviewing files that changed from the base of the PR and between bbdcb11 and 190e8e3.

📒 Files selected for processing (5)
  • openmeter/billing/adapter/gatheringinvoice.go
  • openmeter/billing/adapter/gatheringlines.go
  • openmeter/billing/adapter/invoicelinesplitgroup.go
  • openmeter/billing/adapter/stdinvoicelinemapper.go
  • openmeter/billing/adapter/stdinvoicelines.go

Comment thread openmeter/billing/adapter/gatheringlines.go
Comment on lines +168 to +178
func taxCodeFromSplitLineGroupEdge(dbGroup *db.BillingInvoiceSplitLineGroup) *taxcode.TaxCode {
tc, err := dbGroup.Edges.TaxCodeOrErr()
if err != nil {
return nil
}
mapped, err := taxcodeadapter.MapTaxCodeFromEntity(tc)
if err != nil {
return nil
}
return &mapped
}
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.

⚠️ Potential issue | 🟠 Major

Some split-line-group reads can still return a partial TaxConfig.

This mapper now relies on taxCodeFromSplitLineGroupEdge, but not every caller loads that edge. CreateSplitLineGroup and UpdateSplitLineGroup still map the row straight from Save(), and GetSplitLineGroupHeaders at Line 397 still queries without WithTaxCode(), so rows that rely on the new columns can come back without TaxCodeID or derived Stripe data. A refetch with WithTaxCode() or a fallback from the raw tax_code_id field would keep these responses consistent.

Also applies to: 224-228

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@openmeter/billing/adapter/invoicelinesplitgroup.go` around lines 168 - 178,
The mapper taxCodeFromSplitLineGroupEdge assumes the TaxCode edge is loaded but
some callers (CreateSplitLineGroup, UpdateSplitLineGroup and
GetSplitLineGroupHeaders) do not include WithTaxCode(), leading to partial
TaxConfig; fix by either reloading the entity with WithTaxCode() before mapping
in those callers or add a fallback inside taxCodeFromSplitLineGroupEdge that
uses the raw dbGroup.TaxCodeID (or similar tax_code_id field) to resolve/map a
TaxCode via taxcodeadapter when the edge is missing; update both the occurrences
around lines mentioned (including the block at 224-228) so all code paths return
a consistent TaxCode-derived result.

@borbelyr-kong borbelyr-kong marked this pull request as ready for review April 1, 2026 10:03
@borbelyr-kong borbelyr-kong requested a review from a team as a code owner April 1, 2026 10:03
@borbelyr-kong borbelyr-kong requested a review from turip April 1, 2026 10:04
@borbelyr-kong borbelyr-kong merged commit f64fb60 into main Apr 1, 2026
26 checks passed
@borbelyr-kong borbelyr-kong deleted the feat/tax-codes-double-read-write-billinglines branch April 1, 2026 11:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/billing release-note/ignore Ignore this change when generating release notes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants