estimation service refactor
This commit is contained in:
parent
aaba8753ef
commit
18c95083c9
4 changed files with 367 additions and 62 deletions
|
|
@ -39,41 +39,191 @@ async def estimate(request: models.EstimationRequest):
|
|||
detail="Missing required applicants or plans"
|
||||
)
|
||||
|
||||
# Step 1: Run estimation
|
||||
underwriting_result = run_underwriting(request.applicants, request.phq, request.plans)
|
||||
# Convert request to the format expected by run_underwriting
|
||||
applicants_dict = []
|
||||
for applicant in request.applicants:
|
||||
applicants_dict.append({
|
||||
"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
|
||||
})
|
||||
|
||||
# Step 2: If DTQ → reject application
|
||||
phq_dict = {
|
||||
"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
|
||||
]
|
||||
}
|
||||
|
||||
plans_dict = [
|
||||
{
|
||||
"id": plan.id,
|
||||
"coverage": plan.coverage,
|
||||
"tier": plan.tier
|
||||
} for plan in request.plans
|
||||
]
|
||||
|
||||
# Step 1: Run estimation
|
||||
underwriting_result = run_underwriting(applicants_dict, phq_dict, plans_dict)
|
||||
|
||||
# Step 2: Check if DTQ → reject application
|
||||
if underwriting_result["combined"].get("dtq"):
|
||||
# For DTQ cases, call external reject API and return rejected status
|
||||
reject_response = await reject_application(request.uid)
|
||||
return models.EstimationResponse(
|
||||
uid=request.uid,
|
||||
status="rejected",
|
||||
data=underwriting_result,
|
||||
external=reject_response
|
||||
details=models.EstimationDetails(
|
||||
dtq=True,
|
||||
reason="Declined due to high-risk conditions (DTQ triggered).",
|
||||
tier=int(underwriting_result["combined"]["tier"]),
|
||||
total_price=underwriting_result["combined"]["total_price"]
|
||||
),
|
||||
results=[
|
||||
models.EstimationResult(
|
||||
name=result["name"],
|
||||
applicant_type=result["applicant_type"],
|
||||
age=result["age"] or 0,
|
||||
bmi=result["bmi"] or 0.0,
|
||||
tier=int(result["tier"]),
|
||||
rx_spend=result["rx_spend"],
|
||||
message=result["message"]
|
||||
) for result in underwriting_result["results"]
|
||||
]
|
||||
)
|
||||
|
||||
# Step 3: Else → assign tier and submit
|
||||
# Step 3: Else → assign tier and submit to external API
|
||||
final_tier = underwriting_result["combined"]["tier"]
|
||||
plans = request.plans.copy()
|
||||
if plans:
|
||||
plans[0]["tier"] = f"tier_{str(final_tier).replace('.', '_')}"
|
||||
plans[0].tier = f"tier_{str(final_tier).replace('.', '_')}"
|
||||
|
||||
# Assemble external payload
|
||||
submission_payload = {
|
||||
"applicants": request.applicants,
|
||||
"plans": plans,
|
||||
"phq": request.phq,
|
||||
"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 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": request.address
|
||||
"address": {
|
||||
"address1": request.address.address1,
|
||||
"address2": request.address.address2,
|
||||
"city": request.address.city,
|
||||
"state": request.address.state,
|
||||
"zipcode": request.address.zipcode
|
||||
}
|
||||
}
|
||||
|
||||
submit_response = await submit_application(submission_payload)
|
||||
|
||||
return models.EstimationResponse(
|
||||
uid=request.uid,
|
||||
status="submitted",
|
||||
data=underwriting_result,
|
||||
external=submit_response
|
||||
status="accepted",
|
||||
details=models.EstimationDetails(
|
||||
dtq=False,
|
||||
reason=underwriting_result["combined"]["message"],
|
||||
tier=int(underwriting_result["combined"]["tier"]),
|
||||
total_price=underwriting_result["combined"]["total_price"]
|
||||
),
|
||||
results=[
|
||||
models.EstimationResult(
|
||||
name=result["name"],
|
||||
applicant_type=result["applicant_type"],
|
||||
age=result["age"] or 0,
|
||||
bmi=result["bmi"] or 0.0,
|
||||
tier=int(result["tier"]),
|
||||
rx_spend=result["rx_spend"],
|
||||
message=result["message"]
|
||||
) for result in underwriting_result["results"]
|
||||
]
|
||||
)
|
||||
|
||||
except HTTPException:
|
||||
|
|
@ -101,4 +251,7 @@ async def submit_application(application_payload: Dict[str, Any]) -> Dict[str, A
|
|||
f"{settings.INSURANCE_API_BASE_URL}/applications/submit",
|
||||
json=application_payload
|
||||
)
|
||||
return response.json() if response.status_code == 200 else {"error": "Failed to submit application"}
|
||||
return response.json() if response.status_code == 200 else {"error": "Failed to submit application"}
|
||||
|
||||
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue