initial commit
This commit is contained in:
commit
aaba8753ef
36 changed files with 3682 additions and 0 deletions
104
src/api/v1/router.py
Normal file
104
src/api/v1/router.py
Normal file
|
|
@ -0,0 +1,104 @@
|
|||
from fastapi import APIRouter, HTTPException
|
||||
from typing import Dict, Any
|
||||
import httpx
|
||||
|
||||
from . import models
|
||||
from ...services.chat_service import chat_service
|
||||
from ...services.estimation_service import run_underwriting
|
||||
from ...config import settings
|
||||
|
||||
router = APIRouter(prefix="/v1", tags=["v1"])
|
||||
|
||||
@router.post("/insurance_chat", response_model=models.InsuranceChatResponse)
|
||||
async def insurance_chat(request: models.InsuranceChatRequest):
|
||||
"""Handle insurance chat requests"""
|
||||
try:
|
||||
result = await chat_service.process_insurance_chat(
|
||||
message=request.message,
|
||||
session_id=request.session_id
|
||||
)
|
||||
|
||||
return models.InsuranceChatResponse(
|
||||
session_id=result["session_id"],
|
||||
answer=result["answer"],
|
||||
sources=result["sources"],
|
||||
history=result["history"]
|
||||
)
|
||||
|
||||
except Exception as 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"
|
||||
)
|
||||
|
||||
# Step 1: Run estimation
|
||||
underwriting_result = run_underwriting(request.applicants, request.phq, request.plans)
|
||||
|
||||
# Step 2: If DTQ → reject application
|
||||
if underwriting_result["combined"].get("dtq"):
|
||||
reject_response = await reject_application(request.uid)
|
||||
return models.EstimationResponse(
|
||||
uid=request.uid,
|
||||
status="rejected",
|
||||
data=underwriting_result,
|
||||
external=reject_response
|
||||
)
|
||||
|
||||
# Step 3: Else → assign tier and submit
|
||||
final_tier = underwriting_result["combined"]["tier"]
|
||||
plans = request.plans.copy()
|
||||
if plans:
|
||||
plans[0]["tier"] = f"tier_{str(final_tier).replace('.', '_')}"
|
||||
|
||||
# Assemble external payload
|
||||
submission_payload = {
|
||||
"applicants": request.applicants,
|
||||
"plans": plans,
|
||||
"phq": request.phq,
|
||||
"income": request.income,
|
||||
"address": request.address
|
||||
}
|
||||
|
||||
submit_response = await submit_application(submission_payload)
|
||||
|
||||
return models.EstimationResponse(
|
||||
uid=request.uid,
|
||||
status="submitted",
|
||||
data=underwriting_result,
|
||||
external=submit_response
|
||||
)
|
||||
|
||||
except HTTPException:
|
||||
raise
|
||||
except Exception as e:
|
||||
raise HTTPException(
|
||||
status_code=500,
|
||||
detail=f"Server error: {str(e)}"
|
||||
)
|
||||
|
||||
|
||||
async def reject_application(uid: str) -> Dict[str, Any]:
|
||||
"""Reject application via external API"""
|
||||
async with httpx.AsyncClient() as client:
|
||||
response = await client.post(
|
||||
f"{settings.INSURANCE_API_BASE_URL}/applications/reject",
|
||||
json={"applicationId": uid}
|
||||
)
|
||||
return response.json() if response.status_code == 200 else {"error": "Failed to reject application"}
|
||||
|
||||
async def submit_application(application_payload: Dict[str, Any]) -> Dict[str, Any]:
|
||||
"""Submit application via external API"""
|
||||
async with httpx.AsyncClient() as client:
|
||||
response = await client.post(
|
||||
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"}
|
||||
Loading…
Add table
Add a link
Reference in a new issue