Files
solon/scripts/places-fetch.ts
CatoandCursor 2904ac30d4 feat(places): Zürich tariffs and canton multiplier from ESTV — golden fixtures match the federal calculator (#217)
- POST retrievals: RetrievalRequest.body; the snapshot is an envelope of request
  and response text, since a tariff export does not name its year
- planning may probe the source; ESTV answers unpublished years with other
  years' figures, so the adapter asks the published year range first
- estv_tax_scales (thresholds or widths per table type, variants by group,
  splitting and unread table types skipped with reasons) and estv_simple_rates
- sources estv-income-tax-scales and estv-canton-multipliers (Art. 5 URG)
- golden: Zürich and Küsnacht 2025 at CHF 100,000, single and married, match
  API_calculateSimpleTaxes within its rounding
- docs: §6.2 limits of schema v1, §8.2 sources, §8.3 envelopes and probe, §11

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-09-29 23:06:31 +02:00

62 lines
2.0 KiB
TypeScript

/**
* Fetch a source as its adapter plans it for today, and import each retrieval
* in order (design §8.3). The scheduled runner calls this per the source's
* cadence; run by hand, it is how a source is first loaded.
*
* pnpm run places:fetch <source-key> [--backfill] [--dry-run] [--plan]
*
* --backfill loads the source's history first (once, when a source is first
* loaded; the schedule never passes it). --plan prints the requests it would
* make and stops. Exit 1 on a failed run, so a cron runner alerts.
*/
import { execFileSync } from "node:child_process";
import { placesConfig } from "@/lib/config/places";
import { db } from "@/lib/db/client";
import { describeRequest, fetchAndImport, plannedRetrievals } from "@/lib/places/importer/fetch";
import { defaultSnapshotStore } from "@/lib/places/importer/snapshots";
const args = process.argv.slice(2);
const [sourceKey] = args.filter((a) => !a.startsWith("--"));
const today = new Date().toISOString().slice(0, 10);
const backfill = args.includes("--backfill");
if (!sourceKey) {
console.error("usage: pnpm run places:fetch <source-key> [--backfill] [--dry-run] [--plan]");
process.exit(2);
}
function gitSha(): string | null {
try {
return execFileSync("git", ["rev-parse", "HEAD"], { encoding: "utf8" }).trim();
} catch {
return process.env.GIT_SHA ?? null;
}
}
async function main(): Promise<number> {
if (args.includes("--plan")) {
const requests = await plannedRetrievals(placesConfig, sourceKey!, today, { backfill });
console.log(requests.map(describeRequest).join("\n"));
return 0;
}
const reports = await fetchAndImport(db, {
config: placesConfig,
sourceKey: sourceKey!,
today,
snapshots: defaultSnapshotStore(),
dryRun: args.includes("--dry-run"),
backfill,
gitSha: gitSha(),
});
console.log(JSON.stringify(reports, null, 2));
return reports.some((r) => r.status === "failed") ? 1 : 0;
}
main().then(
(code) => process.exit(code),
(error: unknown) => {
console.error(error);
process.exit(2);
},
);