Skip to content

chore: fix taxcode conflicts - #4408

Merged
turip merged 1 commit into
mainfrom
chore/taxcode-fixes
May 21, 2026
Merged

turip merged 1 commit into
mainfrom
chore/taxcode-fixes

Conversation

@turip

@turip turip commented May 21, 2026 •

Copy link
Copy Markdown
Member

Overview

This fixes tax code resolution when a namespace contains both a user-created tax code and a system-provisioned catalog tax code for the same provider tax code.

In that state, invoice views that calculate gathering invoices with live data could fail before returning invoice details, because the provider tax-code mapping was assumed to identify exactly one tax code. For customers, this meant an otherwise valid invoice list could become unavailable after tax-code catalog seeding introduced a managed tax code that overlapped with an existing custom entry.

The fix resolves that ambiguity from the business ownership model: when multiple active tax codes match the same provider mapping, OpenMeter now prefers the system-managed tax code. If there is still more than one candidate in the same ownership class, resolution falls back to deterministic ordering so invoice calculation remains stable.

Notes for reviewer

  • No tax codes are deleted or migrated; duplicate user-created tax codes remain available by ID/key.
  • The not-found behavior is preserved when no tax code matches the provider mapping.
  • Regression coverage verifies that system-managed tax codes are preferred over user-created duplicates for the same Stripe tax code.

@turip
turip requested a review from a team as a code owner May 21, 2026 14:21
@coderabbitai

coderabbitai Bot commented May 21, 2026 •

Copy link
Copy Markdown
Contributor
📝 Walkthrough

Walkthrough

The PR refactors GetTaxCodeByAppMapping to handle duplicate tax code mappings by fetching all matches and selecting the system-managed variant via stable sorting—first by managed-by status, then creation time, then ID. A test case verifies the system-managed preference is enforced when multiple codes share an app mapping.

Changes

Deterministic Tax Code Selection for Duplicate Mappings

Layer / File(s) Summary
Adapter import and GetTaxCodeByAppMapping refactoring
openmeter/taxcode/adapter/taxcode.go
Imports cmp and slices to enable stable sorting. GetTaxCodeByAppMapping switches from fetching a single result (Only) to fetching all matches (All), explicitly handles empty results, and deterministically selects the best match by system-managed status, then CreatedAt, then ID.
Test case for system-managed tax code preference
openmeter/taxcode/service/taxcode_test.go
Adds openmeter/app import and a new t.Run block that creates a user tax code and system-managed duplicate with identical Stripe mapping, verifies GetTaxCodeByAppMapping returns the system-managed variant, and confirms IsManagedBySystem() is true.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Suggested labels

release-note/ignore

🚥 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
Title check ✅ Passed The title refers to fixing taxcode conflicts, which aligns with the main change of improving tax code selection logic when multiple matches exist by prioritizing system-managed entities.
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.

✏️ 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 chore/taxcode-fixes

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 merged commit 1f4627a into main May 21, 2026
32 of 35 checks passed
@turip
turip deleted the chore/taxcode-fixes branch May 21, 2026 14:32

This branch was previously deployed

1 inactive deployment
dev — 362cb33c Deployed May 21, 2026 by turip via Migration Checks #14579
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants