All files / scripts/parliamentary-data/persistence statskontoret.ts

100% Statements 8/8
100% Branches 1/1
100% Functions 1/1
100% Lines 8/8

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61                                                                      4x 4x   4x 4x 4x           4x 4x                     4x    
/**
 * @module parliamentary-data/persistence/statskontoret
 * @description Statskontoret open-data persistence.
 *
 * @author Hack23 AB
 * @license Apache-2.0
 */
 
import fs from 'node:fs';
import path from 'node:path';
 
import { sanitizeDokId } from './shared/sanitize.js';
import { DATA_ROOT, ensureDir } from './shared/meta-sidecar.js';
 
/**
 * Persist Statskontoret open-data responses and derived datasets.
 *
 * Stored under `analysis/data/statskontoret/{dataset}/{artifact}.json`.
 * Statskontoret data is public and unauthenticated; provenance sidecars record
 * the source dataset and the TypeScript client/CLI used to retrieve or derive
 * the artifact.
 *
 * @param dataset   - Statskontoret source key (e.g. 'myndighetsforteckning').
 * @param artifact  - Logical artifact name (e.g. 'downloads',
 *                    'headcount-by-department').
 * @param response  - Raw or derived Statskontoret payload.
 * @param dataRoot  - Override for the data root directory (for testing).
 * @returns Absolute path to the persisted data file.
 */
export function persistStatskontoretData(
  dataset: string,
  artifact: string,
  response: unknown,
  dataRoot: string = DATA_ROOT,
): string {
  const dir = path.join(dataRoot, 'statskontoret', sanitizeDokId(dataset));
  ensureDir(dir);
 
  const sanitizedArtifact = sanitizeDokId(artifact);
  const filename = `${sanitizedArtifact}.json`;
  fs.writeFileSync(
    path.join(dir, filename),
    JSON.stringify(response, null, 2),
    'utf8',
  );
 
  const metaFilename = `${sanitizedArtifact}.meta.json`;
  fs.writeFileSync(
    path.join(dir, metaFilename),
    JSON.stringify({
      fetchedAt: new Date().toISOString(),
      mcpTool: 'statskontoret-ts-client',
      dataset,
      artifact,
    }, null, 2),
    'utf8',
  );
 
  return path.join(dir, filename);
}