All files / src/lib/overview researcher.ts

92.72% Statements 153/165
59.34% Branches 162/273
90.9% Functions 30/33
93.75% Lines 135/144

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                                                                                                                                                                    2x 66x       46x                                 46x 46x 820x   46x         232x       6x 6x 6x                                 6x 6x 6x 6x 6x 91x 91x 91x 91x 91x   6x       73x   48x 24x 25x 12x 12x 12x 6x 78x         6x 78x   6x   18x       18x 6x 6x 36x       6x                                                 6x 6x     12x                   42x 36x 6x 6x   6x 6x 6x   6x           12x 12x     6x                     6x 6x 5x                       6x 18x 18x   18x         18x 18x                                                     18x 18x       5x                                       6x 6x 6x 6x               6x 6x 6x 6x                     6x                                                                                                   6x           6x 6x 6x 6x           6x 6x                                                                                         6x     6x   6x 6x 6x 6x                                   6x 6x           6x 12x       6x     12x   6x 25x   6x 12x             6x 6x             6x 6x 25x 25x       25x 25x                                                                     25x 25x 6x                           25x   6x 6x 1x         1x                         6x 18x 18x 18x 18x                                                   18x 18x                     18x 12x 12x 12x                     6x     12x   12x 12x 12x                   12x               6x               91x 6x                   73x                          
/**
 * The researcher overview (issue #271 M4): "what am I allowed to use today,
 * what is pending or blocked, and what am I missing?"
 *
 * Built from the permit register, the compliance matrix, the credential
 * wallet, the catalogue, the study registry with its enrolment, and the
 * researcher's own access events. The state of every permit, application
 * and credential comes from `derive.ts`; relevance of other studies and
 * datasets is token overlap with the researcher's own data needs (the
 * first cut, Art. 77 variable-level descriptions are still missing).
 *
 * Article numbers are those of Regulation (EU) 2025/327 as adopted.
 */
import {
  aggregateMonthly,
  chainOfTrust,
  credentialState,
  decisionClock,
  monthRange,
  permitState,
  rankSignals,
  type SeriesPoint,
  type Severity,
} from "./derive";
import {
  holderOf,
  matrixPermit,
  registerPermit,
  type ContractShape,
  type CredentialEntry,
  type HdabAccessEvent,
  type MatrixRow,
  type ParticipantShape,
  type RegisterEntry,
} from "./hdab";
import {
  completenessFact,
  descriptionCompleteness,
  type CatalogEntryLike,
} from "./completeness";
import { attachSeries, raiseStatus } from "./series";
import type {
  OverviewLayer,
  OverviewLink,
  OverviewNode,
  OverviewSignal,
  OverviewView,
} from "./types";
 
export interface CatalogDataset extends CatalogEntryLike {
  id: string;
  title: string;
}
 
export interface StudyRecord {
  studyId: string;
  studyName: string;
  institution?: string | null;
  institutionDid?: string | null;
  status?: string | null;
  dataNeeded?: string | null;
  description?: string | null;
  countries?: string[] | null;
  participantCount?: number | null;
  /** Enrolment snapshots, oldest first */
  enrolment?: SeriesPoint[] | null;
}
 
export interface ResearcherViewInput {
  asOf: string;
  me: { did: string; name: string };
  consumers: ParticipantShape[];
  datasets: CatalogDataset[];
  matrix: MatrixRow[];
  register: RegisterEntry[];
  credentials: CredentialEntry[];
  contracts: ContractShape[];
  /** The researcher's own access events */
  events: HdabAccessEvent[];
  studies: StudyRecord[];
}
 
const fmtDate = (iso?: string | null) =>
  iso ? String(iso).slice(0, 10) : "n/a";
 
/** Five-letter stems of the words that carry meaning in a data need. */
export function stems(text: string | null | undefined): Set<string> {
  const stop = new Set([
    "fhir",
    "omop",
    "data",
    "values",
    "record",
    "records",
    "cohort",
    "study",
    "patient",
    "patients",
    "health",
    "using",
    "across",
    "with",
    "from",
  ]);
  const out = new Set<string>();
  for (const w of (text ?? "").toLowerCase().split(/[^a-z0-9]+/)) {
    if (w.length >= 5 && !stop.has(w)) out.add(w.slice(0, 5));
  }
  return out;
}
 
/** How many stems two texts share. */
export function overlap(a: Set<string>, b: Set<string>): string[] {
  return [...a].filter((s) => b.has(s));
}
 
export function buildResearcherView(input: ResearcherViewInput): OverviewView {
  const { me, asOf } = input;
  const months = monthRange(asOf, 12);
  const layers: OverviewLayer[] = [
    {
      id: "mine",
      name: "My permits, applications, requests",
      z: 2,
      color: "#fbbf24",
    },
    { id: "allowed", name: "What I may use today", z: 1, color: "#22c55e" },
    { id: "me", name: "My studies and my wallet", z: 0, color: "#38bdf8" },
    {
      id: "other",
      name: "Other studies and datasets near mine",
      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(input.consumers.map((p) => [p.id, p]));
  const nameOf = (did: string) => participants.get(did)?.name ?? did;
  const datasetById = new Map(input.datasets.map((d) => [d.id, d]));
  const events = input.events.filter((e) => e.consumerDid === me.did);
  const served = events.filter((e) => (e.statusCode ?? 200) < 400);
  const refused = events.filter((e) => (e.statusCode ?? 200) >= 400);
  const credHolder = new Map(
    input.credentials.map((c) => [
      c.credentialId,
      holderOf(c, [...participants.values()]),
    ]),
  );
  const myCreds = input.credentials.filter(
    (c) => credHolder.get(c.credentialId) === me.did,
  );
  const myStudies = input.studies.filter(
    (s) =>
      s.institutionDid === me.did ||
      (!s.institutionDid && s.institution === me.name) ||
      s.institution === me.name,
  );
  const otherStudies = input.studies.filter((s) => !myStudies.includes(s));
  const myNeeds = new Set<string>();
  for (const s of myStudies) {
    for (const t of stems(s.dataNeeded)) myNeeds.add(t);
  }
 
  // ── me ────────────────────────────────────────────────────────────────────
  const meNode = add({
    id: "me",
    label: me.name,
    layer: "me",
    kind: "Health data user",
    size: 4,
    color: "#fbbf24",
    pin: [0, 0],
    title: me.name,
    sub: `health data user, ${myStudies.length} studies, ${myCreds.length} credentials in the wallet`,
    facts: [
      ["did", me.did],
      [
        "accesses",
        `${served.length} served, ${refused.length} refused in 12 months`,
      ],
    ],
    article:
      "Art. 53 permitted purposes, Art. 54 prohibited ones, Art. 61 duties of health data users, Art. 66 minimisation",
    links: [
      { href: "/data/discover", text: "Discover datasets" },
      { href: "/tasks", text: "My applications" },
      { href: "/query", text: "Secure processing environment" },
    ],
  });
  Eif (served.length > 0) {
    attachSeries(
      meNode,
      {
        series: aggregateMonthly(served, months, () => "x")["x"],
        unit: "accesses",
        measure: "My accesses per month",
        higherIsWorse: false,
      },
      "time",
    );
  }
 
  // ── my own chain of trust ─────────────────────────────────────────────────
  const myRegister = input.register.filter((e) => e.applicantDid === me.did);
  const row = input.matrix.find((r) => r.consumerId === me.did);
  const permits = myRegister
    .filter((e) => e.kind === "application")
    .map(registerPermit);
  const fromMatrix = matrixPermit(row);
  Eif (fromMatrix && !permits.some((x) => x.permitId === fromMatrix.permitId)) {
    permits.push(fromMatrix);
  }
  const contracts = [
    ...input.contracts.filter(
      (c) => !c.consumerDid || c.consumerDid === me.did,
    ),
    ...(row?.hasContract
      ? served
          .filter((e) => e.contractId)
          .map((e) => ({ contractId: e.contractId! }))
      : []),
  ];
  const chain = chainOfTrust(
    {
      consumerDid: me.did,
      consumerName: "I",
      credentials: myCreds,
      permits,
      contracts,
      events,
    },
    asOf,
  );
  raiseStatus(meNode, chain.severity === "ok" ? "ok" : chain.severity);
  for (const f of chain.findings) {
    signals.push({
      severity: f.severity,
      code: f.code,
      nodeId: "me",
      text: f.text
        .replace(/^I: /, "My chain of trust: ")
        .replace(/^I presents/, "I present"),
      article: f.article,
    });
  }
 
  // ── my wallet ─────────────────────────────────────────────────────────────
  for (const c of myCreds) {
    const state = credentialState(c, asOf);
    const isLabel = /quality/i.test(c.credentialType);
    const sev: Severity | "none" =
      state === "expired" || state === "revoked"
        ? "bad"
        : state === "expiring"
          ? "warn"
          : "ok";
    const id = `vc:${c.credentialId}`;
    add({
      id,
      label: `${c.credentialType.replace(/Credential$/, "")} ${state}`,
      layer: "me",
      kind: isLabel ? "Quality label" : "Credential",
      size: 1.3,
      status: sev,
      pulse: sev === "bad",
      title: c.credentialType,
      sub: `${state}${
        c.expiresAt
          ? `, ${state === "expired" ? "since" : "until"} ${fmtDate(
              c.expiresAt,
            )}`
          : ""
      }`,
      facts: [
        ["id", c.credentialId],
        ["issued", fmtDate(c.issuedAt)],
        ["expires", fmtDate(c.expiresAt)],
        ...(c.purpose ? [["purpose", c.purpose] as [string, string]] : []),
      ],
      article: /purpose/i.test(c.credentialType)
        ? "Art. 53(1): the purpose I may process data for; presented before every access (DCP v1.0)"
        : "DCP v1.0 credential: who I am in the dataspace",
      links: [{ href: "/credentials", text: "My wallet" }],
    });
    link("me", id, { kind: "holds", status: sev, distance: 45 });
    Iif (
      sev !== "ok" &&
      !chain.findings.some(
        (f) =>
          f.code.startsWith("credential") || f.code.startsWith("access-after"),
      )
    ) {
      signals.push({
        severity: sev,
        code:
          state === "expiring" ? "credential-expiring" : "credential-expired",
        nodeId: id,
        text: `My ${c.credentialType.replace(
          /Credential$/,
          "",
        )} credential is ${state}${
          c.expiresAt ? ` (${fmtDate(c.expiresAt)})` : ""
        }.`,
        article: "Art. 53(1), Art. 61(1)",
      });
    }
  }
 
  // ── my permits, applications, requests ────────────────────────────────────
  const allowed = new Set<string>();
  for (const e of myRegister) {
    const isRequest = e.kind === "request";
    const clock = decisionClock(
      {
        submittedAt: e.submittedAt ?? asOf,
        decidedAt: e.decidedAt,
        decisionDue: e.decisionDue,
      },
      asOf,
    );
    const state = registerPermit(e);
    const ps = permitState(state, asOf);
    const id = `${isRequest ? "req" : "app"}:${e.applicationId}`;
    const status: Severity = !clock.decided
      ? clock.overdue
        ? "warn"
        : "info"
      : ps === "valid"
        ? "ok"
        : ps === "expiring"
          ? "warn"
          : ps === "refused" || ps === "revoked" || ps === "expired"
            ? "bad"
            : "info";
    add({
      id,
      label: `${
        isRequest
          ? "request"
          : ps === "valid" || ps === "expiring"
            ? "permit"
            : "application"
      } ${e.datasetTitle ?? e.datasetId ?? e.applicationId}`,
      layer: "mine",
      kind: isRequest
        ? "Health data request"
        : clock.decided
          ? ps === "refused"
            ? "Refusal"
            : "Data permit"
          : "Access application",
      size: 1.8,
      status,
      pulse: status === "bad",
      title: e.applicationId,
      sub: `${e.accessBody ?? "access body"}, ${e.outcome ?? "pending"}${
        e.validUntil ? `, valid until ${fmtDate(e.validUntil)}` : ""
      }`,
      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`
          })`,
        ],
        ["purpose", e.purpose ?? "n/a"],
        ["dataset", e.datasetTitle ?? e.datasetId ?? "n/a"],
        ...(e.justification
          ? [["justification", e.justification] as [string, string]]
          : []),
      ],
      article: isRequest
        ? "Art. 69 health data request: an anonymised statistical answer within three months"
        : "Art. 67 application, Art. 68 permit: the access body decides within three months of a complete application",
      links: [
        { href: "/tasks", text: "My applications" },
        { href: "/permits", text: "Public register" },
      ],
    });
    link("me", id, {
      kind: "applied",
      color: "#94a3b8",
      distance: 90,
      arrow: true,
    });
    Eif (e.datasetId) {
      const dsId = `ds:${e.datasetId}`;
      Iif (ps === "valid" || ps === "expiring") allowed.add(e.datasetId);
      link(id, dsId, {
        kind: ps === "valid" || ps === "expiring" ? "covers" : "requested",
        status,
        distance: 80,
      });
    }
    if (!clock.decided) {
      signals.push({
        severity: clock.overdue ? "warn" : "info",
        code: clock.overdue ? "decision-overdue" : "decision-due",
        nodeId: id,
        text: clock.overdue
          ? `${e.accessBody ?? "The access body"} has not decided on my ${
              isRequest ? "request" : "application"
            } for ${e.datasetTitle ?? e.datasetId} (submitted ${fmtDate(
              e.submittedAt,
            )}). Its ${isRequest ? "answer" : "permit decision"} was due ${
              clock.dueAt
            }, three months later, and is ${-clock.daysLeft} days late. I may not access the data until it decides.`
          : `My ${isRequest ? "request" : "application"} for ${
              e.datasetTitle ?? e.datasetId
            } (submitted ${fmtDate(e.submittedAt)}) awaits ${
              e.accessBody ?? "the access body"
            }'s ${isRequest ? "answer" : "permit decision"}, due ${
              clock.dueAt
            }, three months after the application; ${
              clock.daysLeft
            } days left.`,
        article: isRequest ? "Art. 69(4)" : "Art. 68(4)",
      });
    } else Eif (ps === "expiring") {
      signals.push({
        severity: "warn",
        code: "permit-expiring",
        nodeId: id,
        text: `My permit for ${
          e.datasetTitle ?? e.datasetId
        } lapses on ${fmtDate(e.validUntil)}; apply for renewal.`,
        article: "Art. 68(6)",
      });
    } else if (ps === "refused") {
      signals.push({
        severity: "warn",
        code: "application-refused",
        nodeId: id,
        text: `My application for ${e.datasetTitle ?? e.datasetId} was refused${
          e.justification ? `: ${e.justification}` : ""
        }.`,
        article: "Art. 68(3), Art. 57(1)(j)(iii)",
      });
    }
  }
  Eif (
    fromMatrix &&
    row?.datasetId &&
    !myRegister.some((e) => e.permitId === fromMatrix.permitId)
  ) {
    const ps = permitState(fromMatrix, asOf);
    Eif (ps === "valid" || ps === "expiring") allowed.add(row.datasetId);
    const id = `permit:${fromMatrix.permitId}`;
    add({
      id,
      label: `permit ${datasetById.get(row.datasetId)?.title ?? row.datasetId}`,
      layer: "mine",
      kind: "Data permit",
      size: 1.8,
      status: ps === "valid" ? "ok" : ps === "expiring" ? "warn" : "bad",
      title: fromMatrix.permitId ?? id,
      sub: `${ps}${
        row.validUntil ? `, valid until ${fmtDate(row.validUntil)}` : ""
      }`,
      facts: [
        ["state", ps],
        ["dataset", datasetById.get(row.datasetId)?.title ?? row.datasetId],
      ],
      article: "Art. 68 data permit",
      links: [{ href: "/permits", text: "Public register" }],
    });
    link("me", id, { kind: "holds", color: "#94a3b8", distance: 90 });
    link(id, `ds:${row.datasetId}`, {
      kind: "covers",
      status: "ok",
      distance: 80,
    });
  }
  for (const e of served) {
    Eif (e.datasetId && e.permitId) allowed.add(e.datasetId);
  }
 
  // ── datasets: allowed today, and the others near my needs ─────────────────
  const perDataset = aggregateMonthly(
    served,
    months,
    (e) => e.datasetId ?? null,
  );
  const allDatasets = new Map<string, CatalogDataset>(
    input.datasets.map((d) => [d.id, d]),
  );
  for (const e of events) {
    Iif (e.datasetId && !allDatasets.has(e.datasetId)) {
      allDatasets.set(e.datasetId, {
        id: e.datasetId,
        title: e.assetTitle ?? e.datasetId,
      });
    }
  }
  for (const e of myRegister) {
    Iif (e.datasetId && !allDatasets.has(e.datasetId)) {
      allDatasets.set(e.datasetId, {
        id: e.datasetId,
        title: e.datasetTitle ?? e.datasetId,
      });
    }
  }
  const ranked: { id: string; shared: string[] }[] = [];
  for (const d of allDatasets.values()) {
    const isAllowed = allowed.has(d.id);
    const shared = overlap(
      myNeeds,
      stems(`${d.title} ${d.description ?? ""} ${d.theme ?? ""}`),
    );
    const id = `ds:${d.id}`;
    const node = add({
      id,
      label: d.title,
      layer: isAllowed ? "allowed" : "other",
      kind: "Dataset",
      size: isAllowed ? 2 : 1.4,
      color: isAllowed ? "#22c55e" : "#a78bfa",
      status: isAllowed ? "ok" : "none",
      title: d.title,
      sub: isAllowed
        ? `permitted to me today${
            d.publisher ? `, held by ${d.publisher}` : ""
          }`
        : `${d.publisher ? `held by ${d.publisher}, ` : ""}not permitted to me${
            shared.length ? `, matches ${shared.length} of my data needs` : ""
          }`,
      description: d.description ?? undefined,
      facts: [
        ["id", d.id],
        completenessFact(descriptionCompleteness(d)),
        ...(d.recordCount != null
          ? [["records", String(d.recordCount)] as [string, string]]
          : []),
        ...(shared.length
          ? [["shares", shared.join(", ")] as [string, string]]
          : []),
      ],
      article: isAllowed
        ? "Art. 61(1): I access it in the secure processing environment under my permit (Art. 73)"
        : "Art. 67: an application names the datasets; Art. 77 describes them in the national catalogue",
      links: [
        { href: "/catalog", text: "Catalogue entry" },
        { href: "/data/discover", text: "Discover" },
      ],
    });
    const series = perDataset[d.id];
    if (series) {
      raiseStatus(
        node,
        attachSeries(
          node,
          {
            series,
            unit: "accesses",
            measure: "My accesses per month",
            higherIsWorse: false,
          },
          "time",
        ),
      );
    }
    if (!isAllowed && shared.length > 0) ranked.push({ id: d.id, shared });
  }
  ranked.sort((a, b) => b.shared.length - a.shared.length);
  for (const r of ranked.slice(0, 3)) {
    link(`ds:${r.id}`, "me", {
      kind: "matches",
      color: "#a78bfa",
      distance: 140,
    });
    signals.push({
      severity: "info",
      code: "dataset-match",
      nodeId: `ds:${r.id}`,
      text: `${allDatasets.get(r.id)
        ?.title} is not permitted to me and matches my data needs (${r.shared.join(
        ", ",
      )}).`,
      article: "Art. 67 application; Art. 77 description",
    });
  }
 
  // ── my studies and the others near them ───────────────────────────────────
  for (const s of input.studies) {
    const mine = myStudies.includes(s);
    const id = `study:${s.studyId}`;
    const shared = mine ? [] : overlap(myNeeds, stems(s.dataNeeded));
    const node = add({
      id,
      label: s.studyName,
      layer: mine ? "me" : "other",
      kind: "Study",
      size: mine ? 2.4 : 1.6,
      status: mine ? "ok" : "none",
      color: mine ? undefined : "#c4b5fd",
      title: s.studyName,
      sub: `${s.institution ?? ""}${s.status ? `, ${s.status}` : ""}${
        s.participantCount != null
          ? `, ${s.participantCount.toLocaleString("en")} participants`
          : ""
      }${s.countries?.length ? `, ${s.countries.join(", ")}` : ""}`,
      description: s.description ?? undefined,
      facts: [
        ["needs", s.dataNeeded ?? "n/a"],
        ...(shared.length
          ? [["shares with mine", shared.join(", ")] as [string, string]]
          : []),
      ],
      article: mine
        ? "My study; Art. 61(4): I publish the results within 18 months"
        : "Another user's study in the space; Art. 57(1)(j)(v) results are published",
      links: [{ href: "/patient/research", text: "Research programmes" }],
    });
    Eif (s.enrolment && s.enrolment.length > 1) {
      attachSeries(
        node,
        {
          series: s.enrolment,
          unit: "participants",
          measure: "Participants enrolled",
          higherIsWorse: false,
        },
        "time",
      );
    }
    if (mine) link("me", id, { kind: "conducts", status: "ok", distance: 70 });
    else Eif (shared.length > 0) {
      link(id, "me", { kind: "near", color: "#a78bfa", distance: 130 });
      signals.push({
        severity: "info",
        code: "study-match",
        nodeId: id,
        text: `${s.studyName} (${s.institution}) needs ${shared.join(
          ", ",
        )} like mine; a candidate for a joint application or a shared cohort.`,
        article: "Art. 53(1)(e), Art. 67",
      });
    }
  }
  for (const s of otherStudies) {
    // studies of another institution link to that institution if it is in the space
    const inst =
      s.institutionDid ??
      [...participants.values()].find((p) => p.name === s.institution)?.id;
    Eif (inst && inst !== me.did) {
      const oid = `org:${inst}`;
      add({
        id: oid,
        label: nameOf(inst),
        layer: "other",
        kind: "Organisation",
        size: 1.2,
        color: "#f97316",
        title: nameOf(inst),
        sub: "another participant",
      });
      link(`study:${s.studyId}`, oid, {
        kind: "conducted by",
        color: "#f97316",
        distance: 45,
      });
    }
  }
 
  signals.push({
    severity: "info",
    code: "allowed-today",
    nodeId: allowed.size > 0 ? `ds:${[...allowed][0]}` : "me",
    text: `${allowed.size} datasets permitted to me today; ${served.length} accesses served in 12 months, ${refused.length} refused.`,
    article: "Art. 61(1), Art. 73(1)(e)",
  });
 
  const known = new Set(nodes.map((n) => n.id));
  return {
    persona: "researcher",
    asOf,
    me: { id: me.did, name: me.name },
    title: "My research, in one view",
    question:
      "What am I allowed to use today, what is pending or blocked, and what am I missing?",
    article: "Regulation (EU) 2025/327 Art. 53, 61, 67, 68, 69, 73",
    layers,
    nodes,
    links: links.filter((l) => known.has(l.source) && known.has(l.target)),
    signals: rankSignals(signals),
    legend: [
      { color: "#ef4444", text: "blocked / expired" },
      { color: "#f59e0b", text: "pending / due" },
      { color: "#38bdf8", text: "information" },
      { color: "#22c55e", text: "permitted today" },
      { color: "#a78bfa", text: "near my needs" },
      { color: "#93c5fd", text: "month (unfolded)" },
    ],
    dataNote: "Every organisation and study shown is fictional.",
  };
}