add drug parser; add iha estimation rules

This commit is contained in:
ipu 2025-07-31 22:08:02 +03:00
parent bf1d988d36
commit 80916f6c3e
10 changed files with 1271 additions and 15 deletions

View file

@ -0,0 +1,208 @@
import asyncio
import json
from typing import Dict, Any
from services.estimation_service import estimation_service
async def example_estimation_request():
"""Example of how to use the estimation service"""
# Sample request data
request_data = {
"applicants": [
{
"firstName": "John",
"lastName": "Doe",
"dob": "1985-03-15",
"gender": "male",
"weight": 180,
"heightFt": 6,
"heightIn": 1,
"nicotine": False
},
{
"firstName": "Jane",
"lastName": "Doe",
"dob": "1988-07-22",
"gender": "female",
"weight": 140,
"heightFt": 5,
"heightIn": 6,
"nicotine": False
},
{
"firstName": "Emma",
"lastName": "Doe",
"dob": "2015-11-08",
"gender": "female",
"weight": 65,
"heightFt": 4,
"heightIn": 2,
"nicotine": False
}
],
"plans": [
{
"id": "plan_001",
"coverage": "family"
},
{
"id": "plan_002",
"coverage": "family"
}
],
"phq": {
"medications": [
{
"name": "Lisinopril",
"dosage": "10mg",
"frequency": "daily"
},
{
"name": "Metformin",
"dosage": "500mg",
"frequency": "twice daily"
},
{
"name": "Albuterol",
"dosage": "90mcg",
"frequency": "as needed"
}
],
"issues": [
{
"key": "diabetes",
"details": [
{
"description": "Type 2 diabetes diagnosed in 2020"
},
{
"description": "Well controlled with medication"
}
]
},
{
"key": "hypertension",
"details": [
{
"description": "High blood pressure managed with Lisinopril"
}
]
},
{
"key": "asthma",
"details": [
{
"description": "Mild asthma, uses inhaler as needed"
}
]
}
],
"conditions": [
{
"description": "Type 2 diabetes mellitus"
},
{
"description": "Essential hypertension"
},
{
"description": "Mild persistent asthma"
}
]
},
"income": 75000,
"address": {
"address1": "123 Main Street",
"address2": "Apt 4B",
"city": "Springfield",
"state": "IL",
"zipcode": "62701"
}
}
print("=== Insurance Estimation Service Example ===\n")
print("Request Data:")
print(json.dumps(request_data, indent=2))
print("\n" + "="*50 + "\n")
try:
# Call the estimation service
print("Calling estimation service...")
result = await estimation_service.estimate_insurance(request_data)
print("Estimation Result:")
print(json.dumps(result, indent=2))
# Display a summary
if result.get("status") == "accepted":
details = result.get("details", {})
print(f"\n=== SUMMARY ===")
print(f"Status: {result['status']}")
print(f"DTQ: {details.get('dtq', False)}")
print(f"Reason: {details.get('reason', 'N/A')}")
print(f"Tier: {details.get('tier', 'N/A')}")
print(f"Total Price: ${details.get('total_price', 0):,.2f}")
results = result.get("results", [])
if results:
print(f"\nApplicant Results:")
for applicant_result in results:
print(f"- {applicant_result.get('name', 'N/A')} ({applicant_result.get('applicant_type', 'N/A')})")
print(f" Age: {applicant_result.get('age', 'N/A')}")
print(f" BMI: {applicant_result.get('bmi', 'N/A')}")
print(f" Tier: {applicant_result.get('tier', 'N/A')}")
print(f" RX Spend: ${applicant_result.get('rx_spend', 0):,.2f}")
print(f" Message: {applicant_result.get('message', 'N/A')}")
else:
print(f"\nEstimation failed: {result.get('status', 'unknown')}")
if result.get("details"):
print(f"Reason: {result['details'].get('reason', 'N/A')}")
except Exception as e:
print(f"Error calling estimation service: {str(e)}")
async def example_minimal_request():
"""Example with minimal required data"""
minimal_request = {
"applicants": [
{
"firstName": "Alice",
"lastName": "Smith",
"dob": "1990-01-01",
"gender": "female",
"weight": 150,
"heightFt": 5,
"heightIn": 7,
"nicotine": False
}
],
"plans": [
{
"id": "basic_plan",
"coverage": "individual"
}
],
"income": 50000,
"address": {
"city": "Chicago",
"state": "IL"
}
}
print("\n=== Minimal Request Example ===\n")
print("Minimal Request Data:")
print(json.dumps(minimal_request, indent=2))
print("\n" + "="*50 + "\n")
try:
result = await estimation_service.estimate_insurance(minimal_request)
print("Minimal Request Result:")
print(json.dumps(result, indent=2))
except Exception as e:
print(f"Error with minimal request: {str(e)}")
if __name__ == "__main__":
# Run the examples
asyncio.run(example_estimation_request())
asyncio.run(example_minimal_request())

View file

@ -0,0 +1,304 @@
#!/usr/bin/env python3
"""
Test script for IHA underwriting validation functionality
"""
import asyncio
import sys
import os
# Add the src directory to the path
sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..'))
from services.estimation_service import estimation_service
async def test_validation():
"""Test various validation scenarios"""
print("Testing IHA Underwriting Validation\n")
# Test Case 1: Valid application
print("=== Test Case 1: Valid Application ===")
valid_request = {
"applicants": [
{
"firstName": "John",
"lastName": "Doe",
"dob": "15/03/1985",
"gender": "Male",
"weight": 180,
"heightFt": 6,
"heightIn": 0,
"nicotine": False
}
],
"plans": [{"id": "1", "coverage": 1, "tier": "standard"}],
"phq": {
"medications": [
{
"name": "Lisinopril",
"dosage": "10mg",
"frequency": "daily"
}
],
"conditions": [],
"issues": []
},
"income": 50000,
"address": {
"address1": "123 Main St",
"city": "Anytown",
"state": "CA",
"zipcode": "12345"
}
}
result1 = estimation_service._validate_application(valid_request)
print(f"Eligible: {result1['is_eligible']}")
print(f"Issues: {result1['issues']}")
print(f"Warnings: {result1['warnings']}")
print()
# Test Case 2: Uninsurable medication
print("=== Test Case 2: Uninsurable Medication ===")
uninsurable_med_request = {
"applicants": [
{
"firstName": "Jane",
"lastName": "Smith",
"dob": "20/07/1970",
"gender": "Female",
"weight": 150,
"heightFt": 5,
"heightIn": 6,
"nicotine": False
}
],
"plans": [{"id": "1", "coverage": 1, "tier": "standard"}],
"phq": {
"medications": [
{
"name": "Warfarin",
"dosage": "5mg",
"frequency": "daily"
}
],
"conditions": [],
"issues": []
},
"income": 60000,
"address": {
"address1": "456 Oak Ave",
"city": "Somewhere",
"state": "NY",
"zipcode": "67890"
}
}
result2 = estimation_service._validate_application(uninsurable_med_request)
print(f"Eligible: {result2['is_eligible']}")
print(f"Issues: {result2['issues']}")
print(f"Warnings: {result2['warnings']}")
print()
# Test Case 3: Uninsurable condition
print("=== Test Case 3: Uninsurable Condition ===")
uninsurable_condition_request = {
"applicants": [
{
"firstName": "Bob",
"lastName": "Johnson",
"dob": "10/12/1965",
"gender": "Male",
"weight": 200,
"heightFt": 5,
"heightIn": 10,
"nicotine": True
}
],
"plans": [{"id": "1", "coverage": 1, "tier": "standard"}],
"phq": {
"medications": [],
"conditions": [
{
"key": "cancer",
"description": "Lung cancer diagnosed 2 years ago"
}
],
"issues": []
},
"income": 40000,
"address": {
"address1": "789 Pine St",
"city": "Elsewhere",
"state": "TX",
"zipcode": "54321"
}
}
result3 = estimation_service._validate_application(uninsurable_condition_request)
print(f"Eligible: {result3['is_eligible']}")
print(f"Issues: {result3['issues']}")
print(f"Warnings: {result3['warnings']}")
print()
# Test Case 4: Height/weight issues
print("=== Test Case 4: Height/Weight Issues ===")
weight_issue_request = {
"applicants": [
{
"firstName": "Alice",
"lastName": "Brown",
"dob": "05/09/1980",
"gender": "Female",
"weight": 300, # Very high weight
"heightFt": 5,
"heightIn": 4,
"nicotine": False
}
],
"plans": [{"id": "1", "coverage": 1, "tier": "standard"}],
"phq": {
"medications": [],
"conditions": [],
"issues": []
},
"income": 70000,
"address": {
"address1": "321 Elm St",
"city": "Nowhere",
"state": "FL",
"zipcode": "98765"
}
}
result4 = estimation_service._validate_application(weight_issue_request)
print(f"Eligible: {result4['is_eligible']}")
print(f"Issues: {result4['issues']}")
print(f"Warnings: {result4['warnings']}")
print()
# Test Case 5: Diabetes risk factors
print("=== Test Case 5: Diabetes Risk Factors ===")
diabetes_risk_request = {
"applicants": [
{
"firstName": "Charlie",
"lastName": "Wilson",
"dob": "15/01/1975",
"gender": "Male",
"weight": 220,
"heightFt": 6,
"heightIn": 2,
"nicotine": False
}
],
"plans": [{"id": "1", "coverage": 1, "tier": "standard"}],
"phq": {
"medications": [
{
"name": "Insulin",
"dosage": "60 units",
"frequency": "daily"
},
{
"name": "Metformin",
"dosage": "1000mg",
"frequency": "twice daily"
},
{
"name": "Glipizide",
"dosage": "10mg",
"frequency": "daily"
},
{
"name": "Lisinopril",
"dosage": "20mg",
"frequency": "daily"
},
{
"name": "Amlodipine",
"dosage": "10mg",
"frequency": "daily"
},
{
"name": "Hydrochlorothiazide",
"dosage": "25mg",
"frequency": "daily"
}
],
"conditions": [],
"issues": []
},
"income": 55000,
"address": {
"address1": "654 Maple Dr",
"city": "Someplace",
"state": "OH",
"zipcode": "11111"
}
}
result5 = estimation_service._validate_application(diabetes_risk_request)
print(f"Eligible: {result5['is_eligible']}")
print(f"Issues: {result5['issues']}")
print(f"Warnings: {result5['warnings']}")
print()
# Test Case 6: Multiple applicants with mixed eligibility
print("=== Test Case 6: Multiple Applicants ===")
multiple_applicants_request = {
"applicants": [
{
"firstName": "David",
"lastName": "Miller",
"dob": "12/06/1988",
"gender": "Male",
"weight": 175,
"heightFt": 5,
"heightIn": 11,
"nicotine": False
},
{
"firstName": "Sarah",
"lastName": "Miller",
"dob": "08/11/1990",
"gender": "Female",
"weight": 140,
"heightFt": 5,
"heightIn": 6,
"nicotine": False
}
],
"plans": [{"id": "1", "coverage": 1, "tier": "standard"}],
"phq": {
"medications": [
{
"name": "Donepezil",
"dosage": "10mg",
"frequency": "daily"
}
],
"conditions": [],
"issues": []
},
"income": 80000,
"address": {
"address1": "987 Cedar Ln",
"city": "Anywhere",
"state": "WA",
"zipcode": "22222"
}
}
result6 = estimation_service._validate_application(multiple_applicants_request)
print(f"Eligible: {result6['is_eligible']}")
print(f"Issues: {result6['issues']}")
print(f"Warnings: {result6['warnings']}")
print("Applicant validations:")
for app_val in result6['applicant_validations']:
print(f" {app_val['name']}: BMI={app_val['bmi']:.1f}, Category={app_val['premium_category']}, Eligible={app_val['is_eligible']}")
print()
if __name__ == "__main__":
asyncio.run(test_validation())