update dtq conditions
This commit is contained in:
parent
80916f6c3e
commit
0a41d9ba82
11 changed files with 915 additions and 303 deletions
|
|
@ -1,11 +1,9 @@
|
|||
from fastapi import APIRouter, HTTPException
|
||||
from typing import Dict, Any
|
||||
import httpx
|
||||
|
||||
from src.services.estimation_service_v2 import EstimationService
|
||||
|
||||
from . import models
|
||||
from ...services.chat_service import chat_service
|
||||
from ...services.estimation_service import estimation_service
|
||||
from ...config import settings
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
|
|
@ -26,148 +24,25 @@ async def insurance_chat(request: models.InsuranceChatRequest):
|
|||
)
|
||||
|
||||
except Exception as e:
|
||||
raise e
|
||||
raise HTTPException(status_code=500, detail=f"Error processing chat request: {str(e)}")
|
||||
|
||||
@router.post("/estimation", response_model=models.EstimationResponse)
|
||||
async def estimate(request: models.EstimationRequest):
|
||||
"""Handle insurance estimation requests"""
|
||||
try:
|
||||
# Validate required fields
|
||||
if not request.applicants or not request.plans:
|
||||
raise HTTPException(
|
||||
status_code=400,
|
||||
detail="Missing required applicants or plans"
|
||||
)
|
||||
|
||||
# Convert request to the format expected by the estimation service
|
||||
request_data = {
|
||||
"applicants": [
|
||||
{
|
||||
"applicant": applicant.applicant,
|
||||
"firstName": applicant.firstName,
|
||||
"lastName": applicant.lastName,
|
||||
"midName": applicant.midName,
|
||||
"phone": applicant.phone,
|
||||
"gender": applicant.gender,
|
||||
"dob": applicant.dob.strftime("%d/%m/%Y"),
|
||||
"nicotine": applicant.nicotine,
|
||||
"weight": applicant.weight,
|
||||
"heightFt": applicant.heightFt,
|
||||
"heightIn": applicant.heightIn
|
||||
} for applicant in request.applicants
|
||||
],
|
||||
"plans": [
|
||||
{
|
||||
"id": plan.id,
|
||||
"coverage": plan.coverage,
|
||||
"tier": plan.tier
|
||||
} for plan in request.plans
|
||||
],
|
||||
"phq": {
|
||||
"treatment": request.phq.treatment,
|
||||
"invalid": request.phq.invalid,
|
||||
"pregnancy": request.phq.pregnancy,
|
||||
"effectiveDate": request.phq.effectiveDate.strftime("%d/%m/%Y"),
|
||||
"disclaimer": request.phq.disclaimer,
|
||||
"signature": request.phq.signature,
|
||||
"medications": [
|
||||
{
|
||||
"applicant": med.applicant,
|
||||
"name": med.name,
|
||||
"rxcui": med.rxcui,
|
||||
"dosage": med.dosage,
|
||||
"frequency": med.frequency,
|
||||
"description": med.description
|
||||
} for med in request.phq.medications
|
||||
],
|
||||
"issues": [
|
||||
{
|
||||
"key": issue.key,
|
||||
"details": [
|
||||
{
|
||||
"key": detail.key,
|
||||
"description": detail.description
|
||||
} for detail in issue.details
|
||||
]
|
||||
} for issue in request.phq.issues
|
||||
],
|
||||
"conditions": [
|
||||
{
|
||||
"key": condition.key,
|
||||
"description": condition.description
|
||||
} for condition in request.phq.conditions
|
||||
]
|
||||
},
|
||||
"income": request.income,
|
||||
"address": {
|
||||
"address1": request.address.address1,
|
||||
"address2": request.address.address2,
|
||||
"city": request.address.city,
|
||||
"state": request.address.state,
|
||||
"zipcode": request.address.zipcode
|
||||
}
|
||||
}
|
||||
estimation_service = EstimationService()
|
||||
estimation_response = await estimation_service.estimate_insurance(request.applicants, request.phq, request.plans)
|
||||
|
||||
return estimation_response
|
||||
|
||||
# Call the TALESTORM API for estimation
|
||||
estimation_result = await estimation_service.estimate_insurance(request_data)
|
||||
|
||||
# Handle the response from TALESTORM API
|
||||
if estimation_result.get("status") == "error":
|
||||
raise HTTPException(
|
||||
status_code=500,
|
||||
detail=estimation_result.get("details", {}).get("reason", "Estimation service error")
|
||||
)
|
||||
|
||||
# Extract details and results from the TALESTORM response
|
||||
details = estimation_result.get("details", {})
|
||||
results = estimation_result.get("results", [])
|
||||
|
||||
# Convert results to the expected format
|
||||
estimation_results = []
|
||||
for result in results:
|
||||
estimation_results.append(models.EstimationResult(
|
||||
name=result.get("name", "Unknown"),
|
||||
applicant_type=result.get("applicant_type", "Unknown"),
|
||||
age=result.get("age", 0),
|
||||
bmi=result.get("bmi", 0.0),
|
||||
tier=result.get("tier", 4),
|
||||
rx_spend=result.get("rx_spend", 0.0),
|
||||
message=result.get("message", "")
|
||||
))
|
||||
|
||||
# Include validation information in the reason field if there are issues
|
||||
reason = details.get("reason", "")
|
||||
if "validation" in estimation_result:
|
||||
validation_data = estimation_result["validation"]
|
||||
if validation_data.get("issues"):
|
||||
validation_issues = "; ".join(validation_data["issues"])
|
||||
if reason:
|
||||
reason = f"{reason}; Validation issues: {validation_issues}"
|
||||
else:
|
||||
reason = f"Validation issues: {validation_issues}"
|
||||
|
||||
# Add warnings to reason if any
|
||||
if validation_data.get("warnings"):
|
||||
validation_warnings = "; ".join(validation_data["warnings"])
|
||||
if reason:
|
||||
reason = f"{reason}; Warnings: {validation_warnings}"
|
||||
else:
|
||||
reason = f"Warnings: {validation_warnings}"
|
||||
|
||||
return models.EstimationResponse(
|
||||
status=estimation_result.get("status", "accepted"),
|
||||
details=models.EstimationDetails(
|
||||
dtq=details.get("dtq", False),
|
||||
reason=reason,
|
||||
tier=details.get("tier", 4),
|
||||
total_price=details.get("total_price", 0.0)
|
||||
),
|
||||
results=estimation_results
|
||||
)
|
||||
|
||||
except HTTPException:
|
||||
raise
|
||||
except HTTPException as e:
|
||||
raise e
|
||||
except Exception as e:
|
||||
raise HTTPException(
|
||||
status_code=500,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue