All files / src/lib spe-import.ts

100% Statements 13/13
80% Branches 12/15
100% Functions 8/8
100% Lines 12/12

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 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115                        5x           5x             24x                                                             7x         16x     16x         19x                   3x 3x                                         1x 2x 2x                      
import {
  LABEL_LAYER,
  LAYER_COLORS,
  NODE_ROLE_COLORS,
} from "@/lib/graph-constants";
 
/**
 * A transfer's data imported into an SPE workspace (#876). The neo4j-proxy
 * writes it (services/neo4j-proxy/src/spe-import.ts); these are the shapes
 * the hub's routes and pages share.
 */
 
export const IMPORT_SPES = [
  { id: "SPE-1", holder: "AlphaKlinik Berlin" },
  { id: "SPE-2", holder: "Limburg Medical Centre" },
] as const;
 
/** Where a transfer goes unless the user picks another workspace. */
export const DEFAULT_IMPORT_SPE = "SPE-1";
 
/**
 * The SPE id from the allowlist itself, never the caller's string, so a URL
 * built from it carries only a constant (CodeQL js/request-forgery).
 */
export function importSpeId(requested: string): string | null {
  return IMPORT_SPES.find((s) => s.id === requested)?.id ?? null;
}
 
export interface SpeImportResult {
  datasetId: string;
  speId: string;
  records: number;
  relationships: number;
  byType: Record<string, number>;
  importedAt: string;
  pseudonymised: boolean;
  /**
   * The workspace keeps it in memory until it stops (Azure, ADR-072); it is
   * not loaded through the Trustee as a sealed extract yet (#915).
   */
  untilStopped: boolean;
  /**
   * The transfer was terminated after the import: the data is in the SPE,
   * the access to the provider's data ends (Art. 73).
   */
  closed: boolean;
  /** The 3D overview of the import (/overview?import=…). */
  graphUrl: string;
}
 
/**
 * The reason the hub gives the connector when it closes a transfer after
 * importing it. EDC 1.0 lets a consumer terminate a transfer, not complete
 * it (Management API v5 has terminate, suspend and resume only).
 */
export function closedAfterImportReason(speId: string): string {
  return `Closed after import into ${speId}`;
}
 
/** The SPE a transfer was closed for, read from its termination reason. */
export function closedAfterImportInto(errorDetail: unknown): string | null {
  const m = /Closed after import into (SPE-\d+)/.exec(
    String(errorDetail ?? ""),
  );
  return m ? m[1] : null;
}
 
/** The 3D overview of one import: its nodes and relations only. */
export function importViewUrl(datasetId: string, speId: string): string {
  return `/overview?import=${encodeURIComponent(
    datasetId,
  )}&spe=${encodeURIComponent(speId)}`;
}
 
/**
 * The Retry-After of the proxy's 503 for a workspace that does not answer,
 * passed on so the caller knows when to ask again; a minute without one.
 */
export function proxyRetryAfter(res: Response): string {
  const seconds = Number(res.headers.get("retry-after"));
  return seconds > 0 ? String(Math.round(seconds)) : "60";
}
 
/** A transfer the SPEs hold already, as GET /api/spe-imports reports it. */
export interface ExistingImport {
  datasetId: string;
  speId: string;
  records: number;
  importedAt: string;
}
 
interface ProxyNode {
  id: string;
  label: string;
  name: string;
  resourceType?: string;
  properties?: Record<string, string | number | boolean>;
}
 
/** The proxy's nodes as the graph viewer draws them: a layer and a colour. */
export function toGraphNodes(nodes: ProxyNode[]) {
  return nodes.map((n) => {
    const layer = LABEL_LAYER[n.label] ?? 0;
    return {
      id: n.id,
      name: n.name,
      label: n.label,
      layer,
      color: NODE_ROLE_COLORS[n.label] ?? LAYER_COLORS[layer] ?? "#888",
      group: n.resourceType,
      properties: n.properties ?? {},
    };
  });
}