File size: 752 Bytes
877add7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
"""Duplicate therapy checks."""

from __future__ import annotations

from app.common.types import Medication
from app.knowledge.drug_catalog import DRUG_CLASSES


def has_duplicate_therapy(
    meds: list[Medication],
    target_drug: str | None,
    replacement_drug: str | None,
) -> bool:
    classes = [DRUG_CLASSES.get(m.drug) for m in meds if DRUG_CLASSES.get(m.drug)]
    if replacement_drug and DRUG_CLASSES.get(replacement_drug):
        classes.append(DRUG_CLASSES[replacement_drug])
    if target_drug and replacement_drug:
        # replacement within class is expected; duplicate only when 3+ active in same class.
        return any(classes.count(c) >= 3 for c in set(classes))
    return any(classes.count(c) >= 3 for c in set(classes))