add drug dosages parsing, add postgres db

This commit is contained in:
ipu 2025-08-07 01:04:44 +03:00
parent c218e0bbf3
commit 4a59ba5f4a
15 changed files with 856 additions and 122 deletions

View file

@ -1,8 +1,9 @@
from datetime import date
from enum import Enum
from typing import Optional
from src.cache.drug_cache import fetch_drug_with_dosage
from src.models import PHQ, Applicant, Plan, EstimationResponse, EstimationDetails, EstimationResult
from src.cache.redis_cache import fetch_conditions, fetch_drug, get_plan_by_id, search_drug
from src.cache.redis_cache import fetch_conditions, get_plan_by_id, search_drug
from src.config import settings
import httpx
@ -159,7 +160,7 @@ class EstimationService:
today = date.today()
return today.year - born.year - ((today.month, today.day) < (born.month, born.day))
def calculate_rx_spend(self, phq: PHQ, applicant_id: int) -> float:
async def calculate_rx_spend(self, phq: PHQ, applicant_id: int) -> float:
rx_spend = 0
for medication in phq.medications:
if medication.applicant != applicant_id:
@ -167,9 +168,29 @@ class EstimationService:
try:
drug_name = medication.name
drug_url = search_drug(drug_name)
drug_price = fetch_drug(drug_url)
rx_spend += drug_price.prices[0].dosages[0].price
except Exception:
drug_dosage = float(medication.dosage)
drug_price = await fetch_drug_with_dosage(drug_url, drug_dosage)
if medication.frequency in ["Once daily", "At bedtime"]:
month_times = 30
elif medication.frequency == "Twice daily":
month_times = 60
elif medication.frequency in ["Three times daily", "After meals", "Before meals"]:
month_times = 90
elif medication.frequency == "Four times daily":
month_times = 120
elif medication.frequency == "Weekly":
month_times = 4
elif medication.frequency == "Monthly":
month_times = 1
elif medication.frequency == "Every other day":
month_times = 15
else:
month_times = 1
rx_spend += drug_price.unit_price * month_times
except Exception as e:
raise e
pass
return rx_spend
@ -241,7 +262,7 @@ class EstimationService:
is_dtq = True
reason = "Declined due to high BMI of one or more applicants"
rx_spend_applicant = self.calculate_rx_spend(phq, applicant_id)
rx_spend_applicant = await self.calculate_rx_spend(phq, applicant_id)
rx_spend += rx_spend_applicant
applicant_new_tier = self.get_tier(plans[0].coverage, rx_spend_applicant)
@ -262,7 +283,7 @@ class EstimationService:
bmi=self.calculate_bmi(applicant.weight, applicant.heightFt, applicant.heightIn),
tier=applicant_tier.value,
rx_spend=rx_spend_applicant,
message=reason if reason else f"Tier {applicant_tier} assigned with Rx spend within allowed limits."
message=reason if reason else f"Tier {applicant_tier.value} assigned with Rx spend within allowed limits."
)
)