All files / scripts/fetch-calendar/mcp normaliser.ts

100% Statements 16/16
97.43% Branches 38/39
100% Functions 1/1
100% Lines 15/15

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                                        14x   14x 14x 14x 14x 14x   14x 14x 84x 84x 6x 78x 1x 2x         14x                    
/**
 * @module scripts/fetch-calendar/mcp/normaliser
 * @description Normalize raw events from the MCP `get_calendar_events`
 * response into the canonical `CalendarEvent` shape.
 *
 * The riksdag-regering server uses iCalendar field names (`DTSTART`,
 * `DTEND`, `SUMMARY`, etc.) with either upper-case or lower-case keys — both
 * are handled.
 *
 * @author Hack23 AB
 * @license Apache-2.0
 */
 
import type { CalendarEvent } from '../types.js';
 
/**
 * Normalize a raw event object from the MCP `get_calendar_events` response
 * into the canonical `CalendarEvent` shape.
 */
export function normalizeMcpCalendarEvent(raw: unknown): CalendarEvent {
  const r = (raw ?? {}) as Record<string, unknown>;
 
  const dtstart = String(r['dtstart'] ?? r['DTSTART'] ?? r['start'] ?? '').trim();
  const dtend = String(r['dtend'] ?? r['DTEND'] ?? r['end'] ?? '').trim() || undefined;
  const org = String(r['organ'] ?? r['org'] ?? r['ORG'] ?? r['location'] ?? '').trim();
  const akt = String(r['akt'] ?? r['AKT'] ?? r['type'] ?? r['kategori'] ?? '').trim();
  const summary = String(r['summary'] ?? r['SUMMARY'] ?? r['titel'] ?? r['title'] ?? '').trim();
 
  const docRefs: string[] = [];
  for (const key of ['dok_id', 'dokid', 'url', 'href', 'beteckning', 'doc_id']) {
    const val = r[key];
    if (typeof val === 'string' && val.trim()) {
      docRefs.push(val.trim());
    } else if (Array.isArray(val)) {
      for (const item of val) {
        Eif (typeof item === 'string' && item.trim()) docRefs.push(item.trim());
      }
    }
  }
 
  return {
    dtstart,
    ...(dtend ? { dtend } : {}),
    org,
    akt,
    summary,
    doc_refs: docRefs,
    source: 'mcp-primary',
  };
}