[TERMINAL · SKILLS]
> mounting /skills...
> indexing skill manifests...
> linking agents: claude · codex · gemini · cursor
> ready.
[░░░░░░░░░░░░░░░░░░░░░░░░░░░░] 0%
Use Cases/Build a Tax Calculation Engine

Build a Tax Calculation Engine

Build an automated tax calculation system that handles sales tax, VAT, GST across jurisdictions — with nexus determination, product taxability rules, exemption certificates, and tax reporting.

Development#typescript#tsconfig#type-checking#type-safety#javascript-migration
Works with:claude-codeopenai-codexgemini-clicursor

Skills stack · 5 skills

Avg quality 96/100·All SAFE
>

typescript

v1.0.0

Sets up and repairs TypeScript projects: picks tsconfig.json settings for a Node.js service, an npm library or a bundled web app, runs type-checking in CI, and replaces `any` and unsafe casts with types the compiler can verify. Use when someone asks to "set up TypeScript", "fix this tsconfig", "fix a TypeScript error", "upgrade to TypeScript 6 or 7", "migrate JavaScript to TypeScript", "publish types for a package", "run .ts files in Node" or "make this type-safe". Covers the changed defaults in TypeScript 6.0 and 7.0, module and moduleResolution, strict mode, project references, declaration output, narrowing, satisfies and common compiler errors.

93/100 quality
7.30× impact
SAFE· stale score
View skill
>

redis

v1.1.0

Redis is an in-memory data store used as a cache, session store, message broker and counter service. Use when adding a cache layer, storing sessions, rate limiting an API, running pub/sub or Streams with consumer groups, taking a distributed lock, building a leaderboard, or configuring Redis for production (persistence, maxmemory, eviction).

93/100 quality
1.79× impact
SAFE· stale score
View skill
>

postgresql

v1.1.0

Assists with designing schemas, writing performant queries, managing indexes, and operating PostgreSQL databases. Use when working with JSONB, full-text search, window functions, CTEs, row-level security, replication, or performance tuning. Trigger words: postgresql, postgres, sql, database, jsonb, rls, window functions, cte.

100/100 quality
5.15× impact
SAFE· stale score
View skill
>

hono

v1.1.0

Hono is a small web framework built on Web Standards (Request/Response) that runs unchanged on Cloudflare Workers, Deno, Bun, Node.js, AWS Lambda and Vercel. Use it when the user asks to build a Hono API, add Zod validation, JWT auth, CORS or middleware, use the type-safe RPC client (hc), render JSX, stream responses, or deploy one codebase to several runtimes.

100/100 quality
6.20× impact
SAFE· stale score
View skill
>

zod

v1.1.0

Zod is a TypeScript-first schema library that validates data at runtime and infers static types from the same schema. Use when validating API request bodies, form input, environment variables or config files, parsing untrusted JSON, or migrating from Zod 3 to Zod 4. Covers z.object, safeParse, z.infer, coercion, discriminated unions, refinements, JSON Schema output and the v4 API changes.

93/100 quality
3.87× impact
SAFE· stale score
View skill
$

The Problem

Elise leads finance at a 30-person SaaS selling to 40+ countries. Tax compliance is a nightmare: US sales tax has 12,000+ jurisdictions with different rates. The EU requires VAT with reverse charge for B2B. Australia has GST. Some products are taxable in one state but exempt in another. They're manually calculating tax on invoices — and got a $45K penalty for under-collecting tax in Texas. They need an automated tax engine that calculates the right rate for every transaction based on product, location, and customer type.

Step 1: Build the Tax Engine

typescript
// src/tax/engine.ts — Multi-jurisdiction tax calculation with exemptions and reporting
import { pool } from "../db";
import { Redis } from "ioredis";

const redis = new Redis(process.env.REDIS_URL!);

type TaxType = "sales_tax" | "vat" | "gst" | "none";

interface TaxResult {
  subtotal: number;
  taxAmount: number;
  total: number;
  effectiveRate: number;
  taxType: TaxType;
  jurisdiction: string;
  breakdown: Array<{
    name: string;
    rate: number;
    amount: number;
    jurisdiction: string;
  }>;
  exemptionApplied: boolean;
  reverseCharge: boolean;
}

interface TaxContext {
  sellerId: string;
  sellerCountry: string;
  sellerState?: string;
  buyerCountry: string;
  buyerState?: string;
  buyerCity?: string;
  buyerZip?: string;
  buyerIsBusinesss: boolean;
  buyerVatId?: string;
  productCategory: string;     // "saas", "digital_goods", "physical_goods", "consulting"
  amount: number;
  currency: string;
}

// US state tax rates (simplified — production would use a tax API or database)
const US_STATE_RATES: Record<string, number> = {
  CA: 0.0725, NY: 0.08, TX: 0.0625, FL: 0.06, WA: 0.065,
  IL: 0.0625, PA: 0.06, OH: 0.0575, GA: 0.04, NC: 0.0475,
  // ... all 50 states
};

// EU VAT rates
const EU_VAT_RATES: Record<string, number> = {
  DE: 0.19, FR: 0.20, IT: 0.22, ES: 0.21, NL: 0.21,
  BE: 0.21, AT: 0.20, PL: 0.23, SE: 0.25, DK: 0.25,
  FI: 0.24, IE: 0.23, PT: 0.23, CZ: 0.21, RO: 0.19,
  HU: 0.27, SK: 0.20, BG: 0.20, HR: 0.25, LT: 0.21,
  SI: 0.22, LV: 0.21, EE: 0.22, CY: 0.19, LU: 0.17,
  MT: 0.18, EL: 0.24,
};

// SaaS taxability by US state
const SAAS_TAXABLE_STATES = new Set([
  "TX", "NY", "PA", "WA", "CT", "DC", "HI", "IA", "KY",
  "MA", "MN", "MS", "NM", "OH", "RI", "SC", "SD", "TN", "UT", "WV",
]);

// Calculate tax for a transaction
export async function calculateTax(context: TaxContext): Promise<TaxResult> {
  const cacheKey = `tax:${context.buyerCountry}:${context.buyerState}:${context.productCategory}:${context.buyerIsBusinesss}`;
  const cached = await redis.get(cacheKey);

  let rate: number;
  let taxType: TaxType;
  let breakdown: TaxResult["breakdown"] = [];
  let reverseCharge = false;
  let exemptionApplied = false;

  // Determine jurisdiction and rate
  if (context.buyerCountry === "US") {
    ({ rate, taxType, breakdown, exemptionApplied } = calculateUSTax(context));
  } else if (EU_VAT_RATES[context.buyerCountry]) {
    ({ rate, taxType, breakdown, reverseCharge } = calculateEUVAT(context));
  } else if (context.buyerCountry === "AU") {
    rate = 0.10;
    taxType = "gst";
    breakdown = [{ name: "GST", rate: 0.10, amount: context.amount * 0.10, jurisdiction: "AU" }];
  } else if (context.buyerCountry === "CA") {
    rate = 0.05; // GST (simplified — provinces have additional PST/HST)
    taxType = "gst";
    breakdown = [{ name: "GST", rate: 0.05, amount: context.amount * 0.05, jurisdiction: "CA" }];
  } else {
    rate = 0;
    taxType = "none";
  }

  // Check exemption certificates
  if (context.buyerIsBusinesss && context.buyerVatId) {
    const exemptionValid = await validateExemption(context.buyerVatId, context.buyerCountry);
    if (exemptionValid) {
      exemptionApplied = true;
      if (context.buyerCountry !== context.sellerCountry) {
        rate = 0;
        reverseCharge = true;
        breakdown = [{ name: "Reverse Charge", rate: 0, amount: 0, jurisdiction: context.buyerCountry }];
      }
    }
  }

  const taxAmount = Math.round(context.amount * rate);

  const result: TaxResult = {
    subtotal: context.amount,
    taxAmount,
    total: context.amount + taxAmount,
    effectiveRate: rate,
    taxType,
    jurisdiction: `${context.buyerCountry}${context.buyerState ? `-${context.buyerState}` : ""}`,
    breakdown,
    exemptionApplied,
    reverseCharge,
  };

  // Cache rate for 24 hours
  await redis.setex(cacheKey, 86400, JSON.stringify({ rate, taxType }));

  return result;
}

function calculateUSTax(context: TaxContext): {
  rate: number; taxType: TaxType;
  breakdown: TaxResult["breakdown"]; exemptionApplied: boolean;
} {
  // Check nexus (do we have tax obligation in this state?)
  // For SaaS: economic nexus typically applies if revenue > $100K or 200+ transactions in a state
  const state = context.buyerState;
  if (!state) return { rate: 0, taxType: "none", breakdown: [], exemptionApplied: false };

  // Check if SaaS is taxable in this state
  if (context.productCategory === "saas" && !SAAS_TAXABLE_STATES.has(state)) {
    return { rate: 0, taxType: "none", breakdown: [], exemptionApplied: false };
  }

  const stateRate = US_STATE_RATES[state] || 0;
  if (stateRate === 0) return { rate: 0, taxType: "none", breakdown: [], exemptionApplied: false };

  return {
    rate: stateRate,
    taxType: "sales_tax",
    breakdown: [{ name: `${state} Sales Tax`, rate: stateRate, amount: Math.round(context.amount * stateRate), jurisdiction: `US-${state}` }],
    exemptionApplied: false,
  };
}

function calculateEUVAT(context: TaxContext): {
  rate: number; taxType: TaxType;
  breakdown: TaxResult["breakdown"]; reverseCharge: boolean;
} {
  const buyerRate = EU_VAT_RATES[context.buyerCountry] || 0;

  // B2B cross-border: reverse charge
  if (context.buyerIsBusinesss && context.buyerCountry !== context.sellerCountry) {
    return {
      rate: 0,
      taxType: "vat",
      breakdown: [{ name: "Reverse Charge VAT", rate: 0, amount: 0, jurisdiction: context.buyerCountry }],
      reverseCharge: true,
    };
  }

  // B2C or domestic B2B: charge destination country VAT
  return {
    rate: buyerRate,
    taxType: "vat",
    breakdown: [{ name: `${context.buyerCountry} VAT`, rate: buyerRate, amount: Math.round(context.amount * buyerRate), jurisdiction: context.buyerCountry }],
    reverseCharge: false,
  };
}

// Validate VAT ID via VIES API
async function validateExemption(vatId: string, country: string): Promise<boolean> {
  const cacheKey = `vat:valid:${vatId}`;
  const cached = await redis.get(cacheKey);
  if (cached !== null) return cached === "1";

  try {
    // In production: call EU VIES API
    const valid = vatId.length >= 8; // placeholder
    await redis.setex(cacheKey, 86400 * 7, valid ? "1" : "0");
    return valid;
  } catch {
    return false;
  }
}

// Generate tax report for a period
export async function generateTaxReport(period: string): Promise<{
  byJurisdiction: Array<{ jurisdiction: string; taxType: string; taxableAmount: number; taxCollected: number; transactionCount: number }>;
  totalTaxCollected: number;
  totalTransactions: number;
}> {
  const { rows } = await pool.query(
    `SELECT jurisdiction, tax_type,
            SUM(subtotal) as taxable_amount,
            SUM(tax_amount) as tax_collected,
            COUNT(*) as transaction_count
     FROM tax_records
     WHERE period = $1
     GROUP BY jurisdiction, tax_type
     ORDER BY tax_collected DESC`,
    [period]
  );

  return {
    byJurisdiction: rows.map((r) => ({
      jurisdiction: r.jurisdiction,
      taxType: r.tax_type,
      taxableAmount: parseFloat(r.taxable_amount),
      taxCollected: parseFloat(r.tax_collected),
      transactionCount: parseInt(r.transaction_count),
    })),
    totalTaxCollected: rows.reduce((s, r) => s + parseFloat(r.tax_collected), 0),
    totalTransactions: rows.reduce((s, r) => s + parseInt(r.transaction_count), 0),
  };
}

Results

  • Tax penalties: $45K → $0 — automated calculation applies the correct rate for every jurisdiction; no more under-collection or over-collection
  • 20 hours/month manual work eliminated — tax on every invoice is calculated automatically; finance team reviews reports instead of computing rates
  • EU VAT reverse charge handled correctly — B2B cross-border sales automatically apply reverse charge; B2C sales charge destination country VAT
  • SaaS taxability rules encoded — engine knows SaaS is taxable in Texas but not California; no more guessing which states require collection
  • Tax reporting automated — monthly report breaks down tax collected by jurisdiction; ready for filing without spreadsheet gymnastics

Related use cases