All files / src/lib/overview hdab.ts

96.66% Statements 203/210
76.25% Branches 228/299
100% Functions 40/40
97.81% Lines 179/183

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 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826                                                                                                                                                                                                                                      4x 201x       1896x         75x 75x             75x                           45x 33x                       251x 251x 6x 1x   250x 251x   499x 251x 948x 250x   1x     4x                 4x 4x 4x                       4x 4x 4x 4x 4x 145x 145x 145x 145x 145x   4x       169x   4x 32x 4x 27x           27x               66x 16x 4x 27x           4x 28x   4x                                   4x 48x 48x 48x 48x   192x       48x   4x                             4x 4x                 16x     16x                             4x 4x 32x 24x     4x 28x   27x 4x 52x 52x   4x 4x 52x           4x 28x 28x 189x 28x 28x 148x 28x 364x   28x 196x   28x 28x   12x   8x   28x 7x     11x 11x     28x                     28x                                   32x                                 28x 22x                     28x   1x 28x   6x               20x           28x   28x 28x 27x 27x 27x 6x 27x   28x 9x 9x             28x       4x 28x 28x 28x               28x 28x 28x 28x             28x                                                                                 28x 28x             28x 12x                                           28x 12x   12x                 12x                                             12x           12x 12x           12x 12x           12x                             4x 52x 52x 52x   52x             52x 52x                                             52x         52x 8x                       52x 8x                 4x 1x 1x 1x                         1x 1x           1x 1x                 16x 27x 4x 163x   27x 4x     21x   4x 20x 20x   135x 27x   20x                                 20x 20x 4x                           20x 6x               4x 4x                       145x 4x                   169x                          
/**
 * The access body overview (issue #271 M2): "which operations are
 * non-compliant right now, and are my own decisions on time?"
 *
 * A pure builder over the shapes the existing routes return: the compliance
 * matrix (`/api/compliance`), the public permit register (`/api/permits`),
 * the credential wallet (`/api/credentials`), the contracts and the access
 * log of the last twelve months from the graph. Trust is computed as a
 * chain per consumer (credentials, permit, contract, log) with
 * `chainOfTrust`; the body's own duties come from `decisionClock`.
 *
 * Article numbers are those of Regulation (EU) 2025/327 as adopted.
 */
import {
  aggregateMonthly,
  chainOfTrust,
  credentialState,
  decisionClock,
  monthRange,
  permitState,
  rankSignals,
  type AccessEvent,
  type PermitLike,
  type Severity,
} from "./derive";
import { attachSeries, raiseStatus } from "./series";
import type {
  OverviewLayer,
  OverviewLink,
  OverviewNode,
  OverviewSignal,
  OverviewView,
} from "./types";
 
export interface ParticipantShape {
  id: string;
  name: string;
  type?: string | null;
}
 
export interface MatrixRow {
  consumerId: string;
  consumerName?: string | null;
  consumerType?: string | null;
  hasApplication?: boolean;
  applicationStatus?: string | null;
  hasApproval?: boolean;
  approvalStatus?: string | null;
  hasContract?: boolean;
  datasetId?: string | null;
  applicationId?: string | null;
  approvalId?: string | null;
  validUntil?: string | null;
}
 
export interface RegisterEntry {
  kind: "application" | "request";
  applicationId: string;
  applicant?: string | null;
  applicantDid?: string | null;
  accessBody?: string | null;
  purpose?: string | null;
  datasetId?: string | null;
  datasetTitle?: string | null;
  submittedAt?: string | null;
  outcome?: string | null;
  permitId?: string | null;
  decidedAt?: string | null;
  validUntil?: string | null;
  revokedAt?: string | null;
  revocationReason?: string | null;
  decisionDue?: string | null;
  justification?: string | null;
}
 
export interface CredentialEntry {
  credentialId: string;
  credentialType: string;
  status?: string | null;
  issuedAt?: string | null;
  expiresAt?: string | null;
  holderName?: string | null;
  holderDid?: string | null;
  participantRole?: string | null;
  datasetId?: string | null;
  conformance?: number | null;
  purpose?: string | null;
}
 
export interface ContractShape {
  contractId: string;
  consumerDid?: string | null;
  datasetId?: string | null;
  validUntil?: string | null;
  status?: string | null;
}
 
export interface HdabAccessEvent extends AccessEvent {
  consumerName?: string | null;
  providerName?: string | null;
  assetTitle?: string | null;
}
 
export interface HdabViewInput {
  asOf: string;
  me: { did: string; name: string };
  consumers: ParticipantShape[];
  datasets: { id: string; title: string }[];
  matrix: MatrixRow[];
  register: RegisterEntry[];
  credentials: CredentialEntry[];
  contracts: ContractShape[];
  events: HdabAccessEvent[];
}
 
const fmtDate = (iso?: string | null) =>
  iso ? String(iso).slice(0, 10) : "n/a";
// Null-safe: a :Participant can reach the graph without an id (the catalog
// route used to create one from a publisher's display name, #349), and a
// crash here takes the whole access-body overview down with a 502.
const compact = (s: string | null | undefined) =>
  (s ?? "").toLowerCase().replace(/[^a-z0-9]/g, "");
 
/** The permit register's outcome words as a permit state input. */
export function registerPermit(e: RegisterEntry): PermitLike {
  const o = (e.outcome ?? "").toLowerCase();
  const status = e.revokedAt
    ? "REVOKED"
    : o.includes("refus") || o.includes("reject")
      ? "REJECTED"
      : o.includes("issued") || o.includes("approved")
        ? "APPROVED"
        : "PENDING";
  return {
    permitId: e.permitId ?? e.applicationId,
    status,
    validUntil: e.validUntil,
    revokedAt: e.revokedAt,
  };
}
 
/**
 * The approval the compliance matrix records for a consumer, as a permit.
 * Older approvals have no register entry and sometimes no id; the matrix's
 * word still stands, keyed by the consumer when the id is missing.
 */
export function matrixPermit(row: MatrixRow | undefined): PermitLike | null {
  if (!row?.hasApproval) return null;
  return {
    permitId: row.approvalId ?? `approval:${row.consumerId}`,
    status: row.approvalStatus ?? "APPROVED",
    validUntil: row.validUntil ?? null,
  };
}
 
/** The participant a credential belongs to: by holder name, else by the id's tail. */
export function holderOf(
  cred: CredentialEntry,
  participants: ParticipantShape[],
): string | null {
  Iif (cred.holderDid) return cred.holderDid;
  if (cred.holderName) {
    const hit = participants.find((p) => p.name === cred.holderName);
    Eif (hit) return hit.id;
  }
  const tail = cred.credentialId.split(":").pop() ?? "";
  const tokens = tail
    .split("-")
    .filter((t) => t.length >= 3 && !["cro", "clinic", "hdab"].includes(t));
  for (const t of tokens) {
    const hit = participants.find((p) => compact(p.id).includes(compact(t)));
    if (hit) return hit.id;
  }
  return null;
}
 
export const KIND: Record<string, string> = {
  DATA_USER: "Data user",
  DATA_HOLDER: "Data holder",
  HDAB: "Access body",
  CLINIC: "Data holder",
  CRO: "Data user",
};
 
export function buildHdabView(input: HdabViewInput): OverviewView {
  const { me, asOf } = input;
  const months = monthRange(asOf, 12);
  const layers: OverviewLayer[] = [
    { id: "me", name: "My decisions and their clocks", z: 2, color: "#fbbf24" },
    {
      id: "chain",
      name: "Credentials, permits, contracts",
      z: 1,
      color: "#f97316",
    },
    { id: "users", name: "Data users and holders", z: 0, color: "#38bdf8" },
    { id: "data", name: "Datasets", z: -1, color: "#a78bfa" },
    { id: "time", name: "Over time: unfolded months", z: -2, color: "#93c5fd" },
  ];
  const nodes: OverviewNode[] = [];
  const links: OverviewLink[] = [];
  const signals: OverviewSignal[] = [];
  const byId = new Map<string, OverviewNode>();
  const add = (n: OverviewNode) => {
    const had = byId.get(n.id);
    Iif (had) return had;
    byId.set(n.id, n);
    nodes.push(n);
    return n;
  };
  const link = (
    source: string,
    target: string,
    o: Partial<OverviewLink> = {},
  ) => links.push({ source, target, ...o });
 
  const participants = new Map<string, ParticipantShape>();
  for (const p of input.consumers) participants.set(p.id, p);
  for (const e of input.events) {
    Iif (!participants.has(e.consumerDid)) {
      participants.set(e.consumerDid, {
        id: e.consumerDid,
        name: e.consumerName ?? e.consumerDid,
      });
    }
    Iif (e.providerDid && !participants.has(e.providerDid)) {
      participants.set(e.providerDid, {
        id: e.providerDid,
        name: e.providerName ?? e.providerDid,
        type: "DATA_HOLDER",
      });
    }
  }
  const nameOf = (did: string) => participants.get(did)?.name ?? did;
  const datasetTitle = new Map(input.datasets.map((d) => [d.id, d.title]));
  for (const e of input.events) {
    Iif (e.datasetId && e.assetTitle && !datasetTitle.has(e.datasetId)) {
      datasetTitle.set(e.datasetId, e.assetTitle);
    }
  }
 
  // ── me: the body and its queue ────────────────────────────────────────────
  const mine = input.register.filter(
    (e) => !e.accessBody || e.accessBody === me.name,
  );
  const meNode = add({
    id: "me",
    label: me.name,
    layer: "me",
    kind: "Access body",
    size: 4,
    color: "#fbbf24",
    pin: [0, 0],
    title: me.name,
    sub: "health data access body, Art. 55",
    article:
      "Art. 57 tasks, Art. 68(4) decision within three months of a complete application, Art. 57(1)(j) publication, Art. 63 enforcement",
    links: [
      { href: "/compliance", text: "Applications and permits" },
      { href: "/permits", text: "Public register" },
      { href: "/activity-report", text: "Activity report (Art. 59)" },
    ],
  });
  const queue = months.map((m) => {
    const end = new Date(m);
    end.setUTCMonth(end.getUTCMonth() + 1);
    const endIso = end.toISOString();
    const open = mine.filter(
      (e) =>
        e.submittedAt &&
        e.submittedAt < endIso &&
        (!e.decidedAt || e.decidedAt >= endIso),
    ).length;
    return { date: m, value: open };
  });
  const queueSev = attachSeries(
    meNode,
    {
      series: queue,
      unit: "applications",
      measure: "Pending applications at month end",
      range: {
        low: 0,
        high: 2,
        text: "0 to 2 open applications, the load one case officer clears within the Art. 68 deadline",
      },
      higherIsWorse: true,
    },
    "time",
  );
  raiseStatus(meNode, queueSev);
  signals.push({
    severity: queueSev === "ok" ? "info" : queueSev,
    code: "pending-queue",
    nodeId: "me",
    text: `${
      queue[queue.length - 1].value
    } applications were waiting for my permit decision at the end of ${months[11].slice(
      0,
      7,
    )}; ${mine.filter((e) => !e.decidedAt).length} are undecided today, ${
      mine.filter(
        (e) =>
          !e.decidedAt &&
          decisionClock(
            {
              submittedAt: e.submittedAt ?? asOf,
              decidedAt: e.decidedAt,
              decisionDue: e.decisionDue,
            },
            asOf,
          ).overdue,
      ).length
    } of them past the three-month deadline.`,
    article: "Art. 68(4); click to see the queue by month",
  });
 
  // ── consumers and their chain of trust ────────────────────────────────────
  const consumerIds = new Set<string>();
  for (const r of input.matrix) {
    if (r.hasApplication || r.hasApproval || r.hasContract) {
      consumerIds.add(r.consumerId);
    }
  }
  for (const e of input.register) {
    Eif (e.applicantDid) consumerIds.add(e.applicantDid);
  }
  for (const e of input.events) consumerIds.add(e.consumerDid);
  for (const c of input.credentials) {
    const h = holderOf(c, [...participants.values()]);
    Eif (h) consumerIds.add(h);
  }
  consumerIds.delete(me.did);
  const credHolder = new Map<string, string | null>(
    input.credentials.map((c) => [
      c.credentialId,
      holderOf(c, [...participants.values()]),
    ]),
  );
 
  for (const did of [...consumerIds].sort()) {
    const p = participants.get(did) ?? { id: did, name: did };
    const id = `p:${did}`;
    const events = input.events.filter((e) => e.consumerDid === did);
    const served = events.filter((e) => (e.statusCode ?? 200) < 400);
    const refused = events.filter((e) => (e.statusCode ?? 200) >= 400);
    const row = input.matrix.find((r) => r.consumerId === did);
    const creds = input.credentials.filter(
      (c) => credHolder.get(c.credentialId) === did,
    );
    const permits = input.register
      .filter((e) => e.kind === "application" && e.applicantDid === did)
      .map(registerPermit);
    const fromMatrix = matrixPermit(row);
    if (
      fromMatrix &&
      !permits.some((x) => x.permitId === fromMatrix.permitId)
    ) {
      permits.push(fromMatrix);
    }
    const contracts = [
      ...input.contracts.filter((c) => !c.consumerDid || c.consumerDid === did),
      ...(row?.hasContract
        ? served
            .filter((e) => e.contractId)
            .map((e) => ({ contractId: e.contractId! }))
        : []),
    ];
    const chain = chainOfTrust(
      {
        consumerDid: did,
        consumerName: p.name,
        credentials: creds,
        permits,
        contracts,
        events,
      },
      asOf,
    );
    const node = add({
      id,
      label: p.name,
      layer: "users",
      kind: KIND[p.type ?? ""] ?? "Participant",
      size: 2.4,
      status: chain.severity === "ok" ? "ok" : chain.severity,
      pulse: chain.severity === "bad",
      title: p.name,
      sub: `${KIND[p.type ?? ""] ?? "participant"}, ${
        chain.trusted ? "chain of trust intact" : "chain of trust broken"
      }`,
      facts: [
        ["did", did],
        ["credentials", `${creds.length} in the wallet`],
        [
          "permits",
          permits
            .map((x) => `${x.permitId} ${permitState(x, asOf)}`)
            .join(", ") || "none",
        ],
        [
          "accesses",
          `${served.length} served, ${refused.length} refused in 12 months`,
        ],
      ],
      article:
        "Trust is a chain: DCP credentials, an Art. 68 permit, a DSP contract, and a log that agrees with all three (Art. 61(1), 73(1)(e))",
      links: [
        {
          href: `/admin/audit?consumerDid=${encodeURIComponent(did)}`,
          text: "Access log",
        },
      ],
    });
    for (const f of chain.findings) {
      signals.push({
        severity: f.severity,
        code: f.code,
        nodeId: id,
        text: f.text,
        article: f.article,
      });
    }
    // The refusals are the series that matters once an applicant was refused
    // and keeps trying; otherwise the served accesses.
    const showRefused =
      refused.length > 0 &&
      (served.length === 0 ||
        chain.findings.some((f) => f.code === "access-attempt-after-refusal"));
    const monthly = showRefused
      ? {
          series: aggregateMonthly(refused, months, () => "x")["x"],
          unit: "refusals",
          measure: "Refused accesses per month",
          range: { low: 0, high: 0, text: "0 refusals expected" },
          higherIsWorse: true,
        }
      : served.length > 0
        ? {
            series: aggregateMonthly(served, months, () => "x")["x"],
            unit: "accesses",
            measure: "Accesses per month",
            higherIsWorse: false,
          }
        : null;
    if (monthly) raiseStatus(node, attachSeries(node, monthly, "time"));
    // flows to the datasets
    const pairs = new Map<string, { served: number; refused: number }>();
    for (const e of events) {
      Iif (!e.datasetId) continue;
      const k = pairs.get(e.datasetId) ?? { served: 0, refused: 0 };
      if ((e.statusCode ?? 200) < 400) k.served++;
      else k.refused++;
      pairs.set(e.datasetId, k);
    }
    for (const [ds, k] of pairs) {
      const bad = k.served === 0 && k.refused > 0;
      link(id, `ds:${ds}`, {
        kind: bad ? "refused" : "accesses",
        status: bad ? "bad" : chain.severity === "bad" ? "bad" : "ok",
        particles: 2,
        distance: 90,
      });
    }
    link("me", id, { kind: "supervises", color: "#334155", distance: 110 });
  }
 
  // ── applications, permits and the clocks ──────────────────────────────────
  for (const e of input.register) {
    const isMine = mine.includes(e);
    const id = `app:${e.applicationId}`;
    const clock = decisionClock(
      {
        submittedAt: e.submittedAt ?? asOf,
        decidedAt: e.decidedAt,
        decisionDue: e.decisionDue,
      },
      asOf,
    );
    const state = registerPermit(e);
    const ps = permitState(state, asOf);
    const isRequest = e.kind === "request";
    const status: Severity | "none" = clock.overdue
      ? "bad"
      : !clock.decided
        ? "info"
        : ps === "refused" || ps === "revoked"
          ? "warn"
          : "ok";
    add({
      id,
      label: `${isRequest ? "request" : "application"} ${
        e.applicant ?? nameOf(e.applicantDid ?? "")
      }`,
      layer: isMine ? "me" : "chain",
      kind: isRequest ? "Health data request" : "Access application",
      size: 1.6,
      status,
      pulse: clock.overdue,
      title: `${isRequest ? "Health data request" : "Access application"} by ${
        e.applicant ?? e.applicantDid ?? "unknown"
      }`,
      sub: `${e.applicationId}: ${e.purpose ?? ""}, ${
        e.datasetTitle ?? e.datasetId ?? ""
      }${isMine ? "" : `, decided by ${e.accessBody}`}`,
      description: isRequest
        ? "A health data request asks the access body for an answer in anonymised statistical format only. The body answers within three months (Art. 69(4))."
        : "An access application asks the access body for a data permit. The body assesses the Art. 68(1) criteria and issues or refuses the permit within three months of a complete application (Art. 68(4)); the applicant may access data only under the permit (Art. 61(1)). The clock below is the body's own duty.",
      facts: [
        ["submitted", fmtDate(e.submittedAt)],
        [
          "decision due",
          `${clock.dueAt} (${
            clock.decided
              ? "decided " + fmtDate(e.decidedAt)
              : clock.overdue
                ? `${-clock.daysLeft} days overdue`
                : `${clock.daysLeft} days left`
          })`,
        ],
        ["outcome", e.outcome ?? "pending"],
        ...(e.justification
          ? [["justification", e.justification] as [string, string]]
          : []),
      ],
      article: isRequest
        ? "Art. 69: a health data request is answered in anonymised statistical format within three months"
        : "Art. 67 application, Art. 68(4) decision within three months of a complete application, Art. 57(1)(j) publication",
      links: [{ href: "/compliance", text: "Decide" }],
    });
    Eif (e.applicantDid) {
      link(`p:${e.applicantDid}`, id, {
        kind: "applied",
        color: "#94a3b8",
        distance: 70,
        arrow: true,
      });
    }
    if (!clock.decided && isMine) {
      signals.push({
        severity: clock.overdue ? "bad" : "info",
        code: clock.overdue ? "decision-overdue" : "decision-due",
        nodeId: id,
        text: clock.overdue
          ? `${e.applicant ?? e.applicationId} applied on ${fmtDate(
              e.submittedAt,
            )} for ${e.datasetTitle ?? e.datasetId ?? "data"}${
              e.purpose
                ? ` (${e.purpose.toLowerCase().replace(/_/g, " ")})`
                : ""
            }. My ${isRequest ? "answer" : "permit decision"} was due ${
              clock.dueAt
            }, three months later, and is ${-clock.daysLeft} days late: neither issued nor refused. The applicant may not access any data until I decide.`
          : `${e.applicant ?? e.applicationId} applied on ${fmtDate(
              e.submittedAt,
            )} for ${e.datasetTitle ?? e.datasetId ?? "data"}. My ${
              isRequest ? "answer" : "permit decision"
            } is due ${clock.dueAt}, ${clock.daysLeft} days left.`,
        article: isRequest ? "Art. 69(4)" : "Art. 68(4), Art. 57(1)(j)",
      });
    }
    if (e.permitId && clock.decided) {
      const pid = `permit:${e.permitId}`;
      const pstatus: Severity | "none" =
        ps === "valid"
          ? "ok"
          : ps === "expiring"
            ? "warn"
            : ps === "refused"
              ? "warn"
              : ps === "revoked" || ps === "expired"
                ? "bad"
                : "info";
      add({
        id: pid,
        label: `${ps === "refused" ? "refusal" : "permit"} ${e.permitId}`,
        layer: "chain",
        kind: ps === "refused" ? "Refusal" : "Data permit",
        size: 1.6,
        status: pstatus,
        title: e.permitId,
        sub: `${e.applicant ?? ""}, ${ps}${
          e.validUntil ? `, valid until ${fmtDate(e.validUntil)}` : ""
        }`,
        facts: [
          ["decided", fmtDate(e.decidedAt)],
          ["valid until", fmtDate(e.validUntil)],
          ["state", ps],
          ...(e.revocationReason
            ? [["revoked", e.revocationReason] as [string, string]]
            : []),
        ],
        article:
          "Art. 68 data permit; Art. 61(1): access only under it; Art. 63 revocation",
        links: [{ href: "/permits", text: "Public register" }],
      });
      link(id, pid, {
        kind: "decided",
        color: "#94a3b8",
        distance: 50,
        arrow: true,
      });
      Eif (e.applicantDid) {
        link(pid, `p:${e.applicantDid}`, {
          kind: "issued to",
          status: pstatus,
          distance: 70,
        });
      }
      Eif (e.datasetId) {
        link(pid, `ds:${e.datasetId}`, {
          kind: "covers",
          color: "#a78bfa",
          distance: 80,
        });
      }
      Iif (ps === "expiring") {
        signals.push({
          severity: "warn",
          code: "permit-expiring",
          nodeId: pid,
          text: `${e.applicant}: permit ${e.permitId} lapses on ${fmtDate(
            e.validUntil,
          )}.`,
          article: "Art. 68(6)",
        });
      }
    }
  }
 
  // ── credentials in the wallets ────────────────────────────────────────────
  for (const c of input.credentials) {
    const holder = credHolder.get(c.credentialId);
    const state = credentialState(c, asOf);
    const isLabel = /quality/i.test(c.credentialType);
    const sev: Severity | "none" =
      state === "expired" || state === "revoked"
        ? isLabel
          ? "warn"
          : "bad"
        : state === "expiring"
          ? "warn"
          : "ok";
    const id = `vc:${c.credentialId}`;
    add({
      id,
      label: `${c.credentialType.replace(/Credential$/, "")} ${state}`,
      layer: "chain",
      kind: isLabel ? "Quality label" : "Credential",
      size: 1.2,
      status: sev,
      title: c.credentialType,
      sub: `${holder ? nameOf(holder) : "unknown holder"}, ${state}`,
      facts: [
        ["id", c.credentialId],
        ["issued", fmtDate(c.issuedAt)],
        ["expires", fmtDate(c.expiresAt)],
        ...(c.conformance != null
          ? [["conformance", String(c.conformance)] as [string, string]]
          : []),
        ...(c.purpose ? [["purpose", c.purpose] as [string, string]] : []),
      ],
      article: isLabel
        ? "Art. 78 data quality and utility label"
        : "DCP v1.0 credential; Art. 53 purpose, Art. 61 duties of users",
      links: [{ href: "/credentials", text: "Credential wallet" }],
    });
    link(id, holder ? `p:${holder}` : "me", {
      kind: "held by",
      status: sev,
      distance: 50,
    });
    if (isLabel && (state === "expired" || state === "revoked")) {
      signals.push({
        severity: "warn",
        code: "label-expired",
        nodeId: id,
        text: `${
          holder ? nameOf(holder) : "A holder"
        }: the quality label expired on ${fmtDate(
          c.expiresAt,
        )}; the datasets it covered carry no label.`,
        article: "Art. 78, Art. 57(1)(d)",
      });
    }
    if (isLabel && c.datasetId) {
      link(id, `ds:${c.datasetId}`, {
        kind: "labels",
        color: "#a78bfa",
        distance: 80,
      });
    }
  }
 
  // ── contracts on file ─────────────────────────────────────────────────────
  for (const c of input.contracts) {
    const id = `contract:${c.contractId}`;
    const expired = c.validUntil ? c.validUntil < asOf : false;
    add({
      id,
      label: `contract ${c.contractId}`,
      layer: "chain",
      kind: "Contract",
      size: 1.2,
      status: expired ? "warn" : "ok",
      title: c.contractId,
      sub: `DSP 2025-1 contract agreement${c.status ? `, ${c.status}` : ""}`,
      facts: [["valid until", fmtDate(c.validUntil)]],
      article:
        "DSP contract behind the flow; Art. 60(1) the holder makes data available under a permit",
    });
    Eif (c.consumerDid) {
      link(id, `p:${c.consumerDid}`, {
        kind: "party",
        color: "#94a3b8",
        distance: 60,
      });
    }
    Eif (c.datasetId) {
      link(id, `ds:${c.datasetId}`, {
        kind: "covers",
        color: "#a78bfa",
        distance: 80,
      });
    }
  }
 
  // ── datasets and their use ────────────────────────────────────────────────
  const dsIds = new Set<string>([...input.datasets.map((d) => d.id)]);
  for (const e of input.events) Eif (e.datasetId) dsIds.add(e.datasetId);
  for (const l of links) {
    if (l.target.startsWith("ds:")) dsIds.add(l.target.slice(3));
  }
  const served = input.events.filter((e) => (e.statusCode ?? 200) < 400);
  const perDataset = aggregateMonthly(
    served,
    months,
    (e) => e.datasetId ?? null,
  );
  for (const dsId of [...dsIds].sort()) {
    const id = `ds:${dsId}`;
    const providers = new Set(
      input.events
        .filter((e) => e.datasetId === dsId && e.providerDid)
        .map((e) => e.providerDid!),
    );
    const node = add({
      id,
      label: datasetTitle.get(dsId) ?? dsId,
      layer: "data",
      kind: "Dataset",
      size: 1.8,
      color: "#a78bfa",
      title: datasetTitle.get(dsId) ?? dsId,
      sub:
        providers.size > 0
          ? `held by ${[...providers].map(nameOf).join(", ")}`
          : "in the national catalogue",
      facts: [["id", dsId]],
      article:
        "Art. 77 dataset description in the national catalogue; Art. 51 minimum categories",
      links: [{ href: "/catalog", text: "Catalogue" }],
    });
    const series = perDataset[dsId];
    if (series) {
      raiseStatus(
        node,
        attachSeries(
          node,
          {
            series,
            unit: "accesses",
            measure: "Accesses per month",
            higherIsWorse: false,
          },
          "time",
        ),
      );
    }
    for (const prov of providers) {
      link(`p:${prov}`, id, {
        kind: "provides",
        color: "#38bdf8",
        distance: 70,
      });
    }
  }
 
  const refusedCount = input.events.length - served.length;
  signals.push({
    severity: "info",
    code: "access-log",
    nodeId: "me",
    text: `${
      served.length
    } accesses served and ${refusedCount} refused across ${
      Object.keys(perDataset).length
    } datasets in the last 12 months, all logged (Art. 73(1)(e)).`,
    article: "Art. 73(1)(e) log, Art. 59 reporting",
  });
 
  const known = new Set(nodes.map((n) => n.id));
  return {
    persona: "hdab",
    asOf,
    me: { id: me.did, name: me.name },
    title: "Compliance, in one view",
    question:
      "Which operations are non-compliant right now, and are my own decisions on time?",
    article: "Regulation (EU) 2025/327 Art. 57, 61, 63, 68, 73, 78",
    layers,
    nodes,
    links: links.filter((l) => known.has(l.source) && known.has(l.target)),
    signals: rankSignals(signals),
    legend: [
      { color: "#ef4444", text: "non-compliant now" },
      { color: "#f59e0b", text: "at risk / due soon" },
      { color: "#38bdf8", text: "information" },
      { color: "#22c55e", text: "in order" },
      { color: "#a78bfa", text: "dataset" },
      { color: "#93c5fd", text: "month (unfolded)" },
    ],
    dataNote: "Every organisation shown is fictional.",
  };
}