feat/tax-regime
safeviewing older commita72b534 · incrementalPR #302reviewed 2026-07-13 19:28 UTC0H · 0M · 0L · 5I- Purpose
- Per-contract IVA regime for calculated bills — northern-border sites pay 8% (IVA Frontera Norte) not the hardcoded 16%
- Goal
- NaN-proof the resolveTaxRate decision function and findRateByCode query boundary; close the zero-rate false-falsy and undefined-passthrough gaps
- Sub-goals
- SG-1: tax_regime column + migration on utility_contracts
- SG-2: TAX_REGIMES/TAX_REGIME_RATES threaded through type, mapper, decisions
- SG-3: Shell wiring — resolveSiteTariffContext → computeBillShell (drops MX_IVA constant)
- SG-4: NaN-proof at DB boundary (findRateByCode) and decision layer (resolveTaxRate)
- SG-5: Test coverage — zero-rate hit, NaN/undefined miss, fallback chain
- What
- resolveTaxRate guard widened from !== null to typeof+isFinite; findRateByCode NaN-filters at DB boundary; two new unit tests added
- Why
- Prior review (92e5afa) identified zero-rate tableRate=0 as untested and undefined passthrough as unguarded; this commit closes both gaps
- Areas
- domains/utility+155−4domains/cross-domain+53−4packages/api+7−2packages/database+67−0
- Blast
- 27 files, +13263/-8 total (bulk is drizzle meta snapshots); functional billing changes in 8 files
Findings · 5
correctness1
findAllRates bulk map skips NaN-filter
domains/utility/src/tax-rate/tax-rate.queries.ts:22
findAllRates (unchanged) maps rows without isFinite guard. Low real-world risk (numeric Postgres type; main billing path uses findRateByCode), but bulk callers could get NaN from malformed DB row.
security1
Default 16% fallback is the safe (over-tax) direction
domains/utility/src/tax-rate/tax-rate.decisions.ts
TAX_REGIME_RATES.iva = 0.16 as ultimate default over-taxes unrecognised regimes; shell warns on source:'default'. Correct compliance stance.
conventions1
undefined cast in tests is intentional defensive coverage
domains/utility/src/tax-rate/__tests__/tax-rate.decisions.test.ts:14
undefined as unknown as number proves the guard works under TypeScript erasure. Acceptable pattern.
tests1
Infinity / -Infinity not explicitly asserted
domains/utility/src/tax-rate/__tests__/tax-rate.decisions.test.ts
Number.isFinite correctly rejects both; guard is provably right. Consider adding if this ever changes.
improvement1
typeof guard is slightly redundant but legitimate defense-in-depth
domains/utility/src/tax-rate/tax-rate.decisions.ts:23
Number.isFinite(null) is false so typeof check is redundant per type sig. But adds safety against TypeScript erasure. Intentional — not a target for simplification.
History · 6 commits
- a1f0898needs attentionincremental1H · 1M · 4L2026-07-13 20:37
- a72b534safeincremental0H · 0M · 0L2026-07-13 19:28current
- 92e5afaneeds attentionincremental1H · 1M · 8L2026-07-13 16:47
- 7f40d56needs attentionincremental1H · 4M · 5L2026-07-13 16:38
- 5088e00needs attentionincremental3H · 4M · 4L2026-07-13 02:47
- 1bda877blockedfull12H · 12M · 9L2026-07-13 02:30