All files / src/lib/patient recommendations.ts

100% Statements 14/14
100% Branches 5/5
100% Functions 7/7
100% Lines 13/13

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                                                      2x                                                 2x                       2x                                                     2x                 17x 17x 17x 102x 49x               10x 30x   4x   10x    
/**
 * Recommendations on /patient/insights, chosen by the patient's own conditions.
 *
 * Each recommendation names the conditions it is for: code prefixes (ICD-10,
 * SNOMED CT) matched against the start of a Condition's code, and words
 * matched inside its display text. A patient gets the recommendations one of
 * whose conditions they have; with none, one general recommendation.
 */
 
export interface PatientCondition {
  code: string;
  display: string;
}
 
interface Recommendation {
  category: string;
  action: string;
  priority: "high" | "medium" | "low";
  basedOn: string;
  ehdsArticle: string;
}
 
interface CatalogueEntry extends Recommendation {
  codes: readonly string[];
  words: readonly string[];
}
 
const CARDIOVASCULAR = {
  codes: [
    "i10",
    "i11",
    "i20",
    "i21",
    "i25",
    "i48",
    "i50",
    "38341003", // hypertension
    "53741008", // coronary arteriosclerosis
    "22298006", // myocardial infarction
    "49436004", // atrial fibrillation
    "84114007", // heart failure
  ],
  words: [
    "hypertension",
    "coronary",
    "myocardial",
    "heart failure",
    "atrial fibrillation",
    "cardiac",
  ],
} as const;
 
const DIABETES = {
  codes: [
    "e10",
    "e11",
    "r73",
    "73211009", // diabetes mellitus
    "44054006", // type 2 diabetes mellitus
    "15777000", // prediabetes
  ],
  words: ["diabetes", "prediabetes", "hyperglycemia"],
} as const;
 
const RECOMMENDATION_CATALOGUE: readonly CatalogueEntry[] = [
  {
    category: "cardiovascular",
    action: "Annual cardiovascular risk assessment (ECG + lipid panel)",
    priority: "high",
    basedOn: "Study T2D Cohort 2020-2025",
    ehdsArticle: "EHDS Art. 3, informed health decisions",
    ...CARDIOVASCULAR,
  },
  {
    category: "diabetes",
    action: "Bi-annual HbA1c monitoring (LOINC 4548-4)",
    priority: "high",
    basedOn: "Study NL Cardio 2025",
    ehdsArticle: "EHDS Art. 3",
    ...DIABETES,
  },
  {
    category: "longevity",
    action: "Participate in the T2D Longevity research program",
    priority: "medium",
    basedOn: "Study T2D Cohort 2020-2025",
    ehdsArticle: "GDPR Art. 9(2)(a), consent to join a study",
    ...DIABETES,
  },
];
 
const GENERAL_RECOMMENDATION: Recommendation = {
  category: "general",
  action: "Discuss a yearly preventive check-up with your physician",
  priority: "medium",
  basedOn: "No condition in your record matches a study finding",
  ehdsArticle: "EHDS Art. 3, informed health decisions",
};
 
function matches(entry: CatalogueEntry, condition: PatientCondition): boolean {
  const code = condition.code.trim().toLowerCase();
  const display = condition.display.toLowerCase();
  return (
    (code !== "" && entry.codes.some((prefix) => code.startsWith(prefix))) ||
    entry.words.some((word) => display.includes(word))
  );
}
 
/** The recommendations for a patient with these conditions, never empty. */
export function recommendationsFor(
  conditions: readonly PatientCondition[],
): Recommendation[] {
  const chosen = RECOMMENDATION_CATALOGUE.filter((entry) =>
    conditions.some((condition) => matches(entry, condition)),
  ).map(
    ({ codes: _codes, words: _words, ...recommendation }) => recommendation,
  );
  return chosen.length > 0 ? chosen : [GENERAL_RECOMMENDATION];
}