Skip to content

refactor(tax-packs): version tax registration validation metadata with country packs #393

Closed

Description

@khaira777

Refs: #388

Context

FloCafe uses signed, versioned Country Tax Packs to distribute regional tax categories, rules, and regulatory labels (such as GSTIN for India, CUIT for Argentina, RFC for Mexico). When a merchant saves business settings in Settings, the tax registration number is validated against a country-specific pattern.

Verified Current Behavior

  • CountryPack Schema (main/tax-packs/types.ts lines 42-63): CountryPack declares registrationNumberLabel: string (e.g. "GSTIN", "CUIT"), but contains no validation pattern metadata.
  • Static Country Profiles (main/countries.ts lines 3-7, 27-33, 50-97): TaxIdFormat (pattern, description) is hardcoded statically in application source code.
  • Format Resolution (main/services/tax.ts lines 117-135):
    export function resolveTaxIdFormat(country: string): TaxIdFormat | null {
      if (!isTaxModuleActiveForCountry(country)) return null;
      return getCountryByCode(country)?.taxIdFormat || null;
    }
    resolveTaxIdFormat queries getCountryByCode(country)?.taxIdFormat from static application memory.

Problem

Because tax registration format regex patterns are embedded in the static application binary:

  1. An official signed country tax pack cannot update or correct a regulatory tax identifier validation rule without requiring a full FloCafe desktop release.
  2. Regulatory validation metadata remains split across two separate systems (CountryPack for rates/labels, countries.ts for regex formats).

Architectural Decision

  • Allow CountryPack to optionally declare versioned tax registration number validation metadata (e.g. registrationNumberFormat?: TaxIdFormat).
  • Additive / Backward-Compatible Resolution Chain:
    1. Active signed tax pack registrationNumberFormat (if present and valid).
    2. Static country profile taxIdFormat fallback (if present).
    3. No enforced pattern (null / pass-through; never reject valid inputs if no format is declared).
  • Additive Schema Extension: Keep schemaVersion: 1 with an optional field so existing installed packs continue loading without validation failures or breaking migrations.
  • Validation Scope: Registration number validation applies to current merchant configuration saves (PUT /api/settings/business). Historical transactions, orders, and bills are immutable snapshots and must never be retroactively invalidated by pack updates.
  • TaxEngine Arithmetic: Calculation logic is separate and remains untouched.

Exact Implementation Scope

  1. Type Definitions (main/tax-packs/types.ts):
    • Add optional registrationNumberFormat?: TaxIdFormat to CountryPack.
  2. Format Resolution Service (main/services/tax.ts):
    • Update resolveTaxIdFormat(country) to:
      1. Check getActiveCountryPack(country)?.registrationNumberFormat.
      2. If absent, fall back to getCountryByCode(country)?.taxIdFormat.
      3. If neither exists, return null.
  3. Compatibility & Integrity:
    • Ensure pack signature checks and pack loader accept the optional field cleanly.
    • Maintain compatibility across existing local/custom packs and older databases.
  4. Tests:
    • Extend tests/tax-components.test.ts and tests/tax-pack-management.test.ts to verify pack-provided format resolution, fallback behavior, and backward compatibility with v1 packs lacking this field.

Required Behavior

  • An official tax pack defining registrationNumberFormat enforces that pattern during Settings tax registration number updates.
  • An installed tax pack omitting registrationNumberFormat falls back gracefully to the static countries.ts pattern.
  • A country with no declared pattern allows saving any non-empty registration string without format error.
  • Existing installed schema-v1 tax packs load and activate without schema errors.
  • Historical bills and transactions remain unaffected.

Backward Compatibility / Upgrade Behavior

  • Fully backward-compatible additive field.
  • No database migration or pack invalidation required.

Offline-First Requirements

  • 100% offline. Operates using local SQLite-persisted tax pack manifests and local validators.

Non-Goals / What NOT to Do

  • Do NOT rewrite TaxEngine calculation, rounding, or surcharge logic.
  • Do NOT introduce a breaking schemaVersion: 2 bump that invalidates installed schema-v1 packs.
  • Do NOT retroactively validate or reject historical bills/orders.
  • Do NOT make registrationNumberFormat mandatory.

Acceptance Criteria

  • CountryPack type definition includes optional registrationNumberFormat.
  • resolveTaxIdFormat(country) prioritizes the active country tax pack format when present, falling back to static countries.ts.
  • Existing installed tax packs lacking the format field continue to load and function normally.
  • Settings business update route (PUT /api/settings/business) validates tax IDs against the resolved pattern.
  • Historical tax snapshots and transaction records remain unchanged.

Verification

npm run lint:backend
npm run test:tax-engine
npm run test:tax-components
npm run test:tax-pack-catalog
npm run test:tax-pack-management
npm test

Dependencies

Relationship to Epic

Activity

  1. added
    enhancement
    area:backend
    on Aug 18, 2026
  2. itsbkm commented on Aug 20, 2026

    @itsbkm
    Contributor

    Implemented and merged via #405 ("refactor(tax-packs): version tax registration validation metadata with country packs"), which was referenced with "Refs #393" rather than a closing keyword so this wasn't auto-closed. Verified in code: registrationNumberFormat is present on CountryPack (main/tax-packs/types.ts) and resolveTaxIdFormat() in main/services/tax.ts prefers the active pack's format before falling back to the static countries.ts profile, per the acceptance criteria.

  3. khaira777 commented on Aug 20, 2026

    @khaira777
    ContributorAuthor

    Status update — 2026-08-20

    ✅ Complete. This issue was implemented and merged via PR #405, and remains closed as completed.

  4. khaira777 commented on Aug 20, 2026

    @khaira777
    ContributorAuthor

    Status update — 2026-08-20

    ✅ Original issue complete and closed. PRs #405 and #407 are merged. Post-merge hardening is tracked separately in open PR #434, currently at 75883b36 with CI in progress; the original issue remains closed because its implementation was already merged.

  5. added a commit that references this issue on Aug 21, 2026
  6. khaira777 commented on Aug 21, 2026

    @khaira777
    ContributorAuthor

    Final status update — 2026-08-21

    ✅ Complete, including post-merge hardening. Follow-up PR #434 merged at 8730fc04 after all required CI and Greptile checks passed. The tax-registration validation, silent-save format propagation, and submitted-country format association are now delivered. This issue remains closed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area:backendenhancement

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions