All files / src/app/patient/profile page.tsx

98.64% Statements 73/74
81.44% Branches 79/97
100% Functions 19/19
100% Lines 67/67

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 4513x                                                                                                               3x                               32x 32x 30x 2x             28x   1x       3x             88x 88x   88x 101x                 88x 26x         88x 26x         88x 88x 88x 88x 88x 88x   88x 88x 88x 88x             88x 88x         88x 19x 17x 17x   16x 16x 1x 1x   15x   16x 4x   16x 16x     16x   17x 17x       88x 32x 15x 15x 15x           15x 12x 2x 2x 2x   10x 10x     15x   15x 15x       88x                                                                                                                               2x       88x                                                                                                                                                                                                           26x 26x                                             52x                                                                         26x                                              
"use client";
 
import { fetchApi } from "@/lib/api";
import { useEffect, useState } from "react";
import {
  Heart,
  AlertTriangle,
  CheckCircle2,
  Activity,
  Shield,
  User,
  Wallet,
} from "lucide-react";
import Link from "next/link";
import PageIntro from "@/components/PageIntro";
import { isSandboxUsername } from "@/lib/sandbox-account";
import ConnectAppCard from "@/components/ConnectAppCard";
import HealthInterests from "@/components/HealthInterests";
import { useSession } from "next-auth/react";
import { T, useT } from "@/lib/i18n/translate";
import { useOwnRecord } from "@/lib/patient/use-own-record";
import { IS_STATIC } from "@/lib/static-export";
 
interface RiskScore {
  score: number;
  level: "low" | "moderate" | "high";
  factors: string[];
}
 
interface Profile {
  patient: {
    id: string;
    name: string;
    gender: string;
    birthDate: string;
    age?: number | null;
  };
  conditions: { code: string; display: string; onsetDate: string }[];
  medications: { code: string; display: string }[];
  riskScores: { cardiovascular: RiskScore; diabetes: RiskScore };
  interests: string[];
  interestsChosen?: boolean;
  suggestedInterests?: { id: string; label: string }[];
  gdprRights: Record<string, string>;
  totalConditionCount?: number;
}
 
interface PatientItem {
  id: string;
  name: string;
  gender: string;
  birthDate: string;
  age?: number | null;
  conditionCount: number;
}
 
const RISK_COLORS = {
  low: "text-green-800 dark:text-green-300 bg-green-50 dark:bg-green-950 border-green-200 dark:border-green-800",
  moderate:
    "text-yellow-800 dark:text-yellow-300 bg-yellow-50 dark:bg-yellow-900/30 border-yellow-300 dark:border-yellow-700",
  high: "text-red-800 dark:text-red-300 bg-red-50 dark:bg-red-950 border-red-200 dark:border-red-800",
};
 
/** Why the profile did not load: refused (401, 403) or failed (anything else). */
type LoadError =
  | { kind: "refused" }
  | { kind: "failed"; status: number | null };
 
/** The answer's body, or why there is none. A network error is a failure. */
async function readJson<T>(
  request: Promise<Response>,
): Promise<{ data: T } | { error: LoadError }> {
  try {
    const r = await request;
    if (!r.ok) {
      return {
        error:
          r.status === 401 || r.status === 403
            ? { kind: "refused" }
            : { kind: "failed", status: r.status },
      };
    }
    return { data: (await r.json()) as T };
  } catch {
    return { error: { kind: "failed", status: null } };
  }
}
 
const RISK_ICONS = {
  low: CheckCircle2,
  moderate: AlertTriangle,
  high: AlertTriangle,
};
 
export default function PatientProfilePage() {
  const t = useT("patient");
  const tn = useT("nav");
  // FHIR's administrative gender codes, in the picked language.
  const gender = (g: string | null | undefined) =>
    g === "female"
      ? t("female")
      : g === "male"
        ? t("male")
        : g === "other"
          ? t("other")
          : g === "unknown"
            ? t("unknown")
            : g;
  const riskTitle = (key: string) =>
    key === "cardiovascular"
      ? t("Cardiovascular Risk")
      : key === "diabetes"
        ? t("Diabetes Risk")
        : `${key.replace(/([A-Z])/g, " $1")} Risk`;
  const riskLevel = (level: RiskScore["level"]) =>
    level === "high"
      ? t("high risk")
      : level === "moderate"
        ? t("moderate risk")
        : t("low risk");
  const [patients, setPatients] = useState<PatientItem[]>([]);
  const [selectedId, setSelectedId] = useState<string>("");
  const [profile, setProfile] = useState<Profile | null>(null);
  const [loading, setLoading] = useState(true);
  const [profileLoading, setProfileLoading] = useState(false);
  const [loadError, setLoadError] = useState<LoadError | null>(null);
  // Only the patient names their own interests; the route refuses others.
  const { data: session } = useSession();
  const roles = (session as { roles?: string[] } | null)?.roles ?? [];
  const canEditInterests = roles.includes("PATIENT");
  const username =
    (session?.user as { preferredUsername?: string } | undefined)
      ?.preferredUsername ?? "";
 
  // A patient opens their own record. The route lists only that one; the
  // static build's list fixture holds both demo patients, so there the page
  // narrows it the same way (patient2 opens P2, not the first entry).
  const subject = useOwnRecord();
  const ownId = subject.kind === "own" ? subject.patientId : null;
 
  // A refusal or an error is no profile, and the page says which. Before, the
  // list's failure left an empty page and the profile's error body was stored
  // as the profile, which the page then crashed on.
  useEffect(() => {
    if (subject.kind === "pending") return;
    let cancelled = false;
    readJson<{ patients?: PatientItem[] }>(fetchApi("/api/patient/profile"))
      .then((res) => {
        Iif (cancelled) return;
        if ("error" in res) {
          setLoadError(res.error);
          return;
        }
        const all = res.data.patients ?? [];
        const list =
          IS_STATIC && subject.kind === "own"
            ? all.filter((p) => p.id === ownId)
            : all;
        setPatients(list);
        if (list.length > 0) setSelectedId(list[0].id);
      })
      .finally(() => {
        Eif (!cancelled) setLoading(false);
      });
    return () => {
      cancelled = true;
    };
  }, [subject.kind, ownId]);
 
  useEffect(() => {
    if (!selectedId) return;
    let cancelled = false;
    setProfileLoading(true);
    readJson<Profile>(
      fetchApi(
        `/api/patient/profile?patientId=${encodeURIComponent(selectedId)}`,
      ),
    )
      .then((res) => {
        if (cancelled) return;
        if ("error" in res) {
          setProfile(null);
          setLoadError(res.error);
          return;
        }
        setProfile(res.data);
        setLoadError(null);
      })
      .finally(() => {
        if (!cancelled) setProfileLoading(false);
      });
    return () => {
      cancelled = true;
    };
  }, [selectedId]);
 
  return (
    <div className="min-h-screen bg-(--bg)">
      <div className="max-w-3xl mx-auto px-6 py-10">
        <PageIntro
          title={t("Health Profile & Risk Assessment")}
          icon={Heart}
          description={t(
            "GDPR Art. 15 / EHDS Art. 3: your personal health profile, computed risk scores, and health interests. All data derived from your own FHIR electronic health record.",
          )}
          prevStep={{ href: "/patient", label: t("Health Records") }}
          nextStep={{
            href: "/patient/research",
            label: tn("Research Programs"),
          }}
          infoText={t(
            "Risk scores are computed from your clinical conditions and are for informational purposes only. Always consult your physician before making medical decisions.",
          )}
          docLink={{
            href: "/docs/user-guide",
            label: t("Patient Rights Guide"),
          }}
        />
 
        {/* The Klarbefund app reads this record once connected (#473). */}
        <ConnectAppCard />
 
        {/* A sandbox account can sign in with the EUDI wallet (ADR-067). */}
        {isSandboxUsername(username) && (
          <Link
            href="/patient/wallet"
            data-testid="connect-wallet-link"
            className="mb-6 flex items-center gap-2 rounded-lg border border-(--border) bg-(--surface) p-4 text-sm hover:bg-(--surface-2)"
          >
            <Wallet size={18} className="text-(--accent)" aria-hidden />
            <span>
              <strong>{t("Connect your EUDI wallet")}</strong>
              <span className="block text-xs text-(--text-secondary)">
                {t(
                  "Sign in to this account with your PID, at assurance level high.",
                )}
              </span>
            </span>
          </Link>
        )}
 
        {/* GDPR rights banner */}
        <div className="mb-6 rounded-lg border border-(--border) bg-(--surface-2) p-3 flex items-start gap-2">
          <Shield size={16} className="mt-0.5 shrink-0 text-(--accent)" />
          <div className="text-xs text-(--text-secondary)">
            <T area="patient" tags={{ b: <strong /> }}>
              {
                "<b>Your data rights:</b> You have the right to access (GDPR Art. 15), portability (Art. 20), rectification (Art. 16), and erasure (Art. 17) of your health data. Under EHDS Art. 3, you can access your EHR through any EU national contact point."
              }
            </T>
          </div>
        </div>
 
        {/* Patient selector */}
        {!loading && patients.length > 0 && (
          <div className="mb-6">
            <label className="text-sm text-(--text-secondary)">
              {t("Select patient record")}
              <select
                value={selectedId}
                onChange={(e) => setSelectedId(e.target.value)}
                className="mt-1 w-full px-3 py-2 bg-(--surface-2) border border-(--border-ui) rounded-sm text-sm outline-hidden focus:border-teal-500 block"
              >
                {patients.map((p) => (
                  <option key={p.id} value={p.id}>
                    {[
                      p.name,
                      p.gender && p.gender !== "unknown"
                        ? gender(p.gender)
                        : null,
                      p.age != null
                        ? t("age {age}", { age: p.age })
                        : p.birthDate
                          ? t("born {date}", { date: p.birthDate })
                          : null,
                    ]
                      .filter(Boolean)
                      .join(", ")}{" "}
                    {t("({n} conditions)", { n: p.conditionCount })}
                  </option>
                ))}
              </select>
            </label>
          </div>
        )}
 
        {profileLoading && (
          <div className="text-(--text-secondary) text-sm">
            {t("Loading health profile…")}
          </div>
        )}
 
        {loadError && !profileLoading && (
          <div
            role="alert"
            data-testid="profile-load-error"
            className="mb-6 rounded-lg border border-amber-300 dark:border-amber-700 bg-amber-50 dark:bg-amber-900/20 p-3 text-sm text-amber-900 dark:text-amber-200"
          >
            {loadError.kind === "refused"
              ? t(
                  "You may not see this health profile: a patient sees their own record only. If your session has ended, sign in again.",
                )
              : loadError.status
                ? t(
                    "The health profile could not be loaded (HTTP {status}). Please try again later.",
                    { status: loadError.status },
                  )
                : t(
                    "The health profile could not be loaded. Please try again later.",
                  )}
          </div>
        )}
 
        {profile && !profileLoading && (
          <div className="space-y-6">
            {/* Patient demographics */}
            <div className="rounded-xl border border-(--border) bg-(--surface-2)/50 p-4">
              <div className="flex items-center gap-2 mb-3">
                <User size={16} className="text-teal-800 dark:text-teal-400" />
                <h2 className="font-semibold">
                  {profile.patient.name || t("Anonymous Patient")}
                </h2>
              </div>
              <div className="grid grid-cols-2 gap-2 text-xs text-(--text-secondary)">
                <div>
                  <span className="text-(--text-secondary)">
                    {t("Gender:")}
                  </span>{" "}
                  {gender(profile.patient.gender)}
                </div>
                <div>
                  <span className="text-(--text-secondary)">
                    {profile.patient.age != null
                      ? t("Age:")
                      : t("Date of birth:")}
                  </span>{" "}
                  {profile.patient.age != null
                    ? profile.patient.age
                    : profile.patient.birthDate || "—"}
                </div>
                <div>
                  <span className="text-(--text-secondary)">
                    {t("Active Conditions:")}
                  </span>{" "}
                  {profile.totalConditionCount ?? profile.conditions.length}
                </div>
                <div>
                  <span className="text-(--text-secondary)">
                    {t("Medications:")}
                  </span>{" "}
                  {profile.medications.length}
                </div>
              </div>
            </div>
 
            {/* Risk scores */}
            <div>
              <h2 className="text-lg font-semibold mb-3 flex items-center gap-2">
                <Activity
                  size={18}
                  className="text-teal-800 dark:text-teal-400"
                />
                {t("Health Risk Assessment")}
              </h2>
              <div className="grid grid-cols-1 sm:grid-cols-2 gap-4">
                {Object.entries(profile.riskScores).map(([key, risk]) => {
                  const Icon = RISK_ICONS[risk.level];
                  return (
                    <div
                      key={key}
                      className={`rounded-xl border p-4 ${
                        RISK_COLORS[risk.level]
                      }`}
                      data-testid={`risk-card-${key}`}
                    >
                      <div className="flex items-center justify-between mb-2">
                        <span className="font-semibold first-letter:uppercase">
                          {riskTitle(key)}
                        </span>
                        <Icon size={16} />
                      </div>
                      <div className="text-2xl font-bold mb-1">
                        {Math.round(risk.score * 100)}%
                      </div>
                      <div className="text-xs first-letter:uppercase font-medium mb-2">
                        {riskLevel(risk.level)}
                      </div>
                      {risk.factors.length > 0 && (
                        <ul className="text-xs space-y-0.5 opacity-80">
                          {risk.factors.map((f) => (
                            <li key={f}>• {f}</li>
                          ))}
                        </ul>
                      )}
                    </div>
                  );
                })}
              </div>
            </div>
 
            {/* Interests: suggested from the record, editable by the patient */}
            <HealthInterests
              key={profile.patient.id}
              patientId={profile.patient.id}
              interests={profile.interests}
              chosen={profile.interestsChosen ?? false}
              suggested={profile.suggestedInterests ?? []}
              canEdit={canEditInterests}
            />
 
            {/* Conditions */}
            {profile.conditions.length > 0 && (
              <div>
                <h2 className="text-lg font-semibold mb-2">
                  {t("Active Conditions")}
                </h2>
                <div className="rounded-xl border border-(--border) overflow-hidden">
                  <table className="text-xs w-full border-collapse">
                    <thead>
                      <tr className="border-b border-(--border) text-(--text-secondary)">
                        <th className="text-left p-2">{t("Condition")}</th>
                        <th className="text-left p-2">ICD-10 / SNOMED</th>
                        <th className="text-left p-2">{t("Onset")}</th>
                      </tr>
                    </thead>
                    <tbody>
                      {profile.conditions.slice(0, 8).map((c, i) => (
                        <tr key={i} className="border-b border-(--border)">
                          <td className="p-2 text-(--text-primary)">
                            {c.display}
                          </td>
                          <td className="p-2 font-mono text-(--text-secondary)">
                            {c.code}
                          </td>
                          <td className="p-2 text-(--text-secondary)">
                            {c.onsetDate?.slice(0, 10) || "—"}
                          </td>
                        </tr>
                      ))}
                    </tbody>
                  </table>
                </div>
              </div>
            )}
          </div>
        )}
      </div>
    </div>
  );
}