Spaces:
Sleeping
Sleeping
| from fastapi import APIRouter, Depends, HTTPException, UploadFile, File, Header | |
| from typing import Optional | |
| from datetime import datetime | |
| from ..models import Customer | |
| from ..call_processor import CallProcessor | |
| from ..auth import get_current_customer | |
| import logging | |
| router = APIRouter() | |
| logger = logging.getLogger(__name__) | |
| async def process_call( | |
| file: UploadFile = File(...), | |
| caller_number: str = Header(...), | |
| called_number: str = Header(...), | |
| customer: Customer = Depends(get_current_customer) | |
| ): | |
| """Process a call and send results to customer's webhook""" | |
| try: | |
| # Create call processor for the customer | |
| processor = CallProcessor(customer) | |
| # Process and send the call | |
| result = processor.process_and_send_call(file, caller_number, called_number) | |
| if not result: | |
| raise HTTPException( | |
| status_code=500, | |
| detail="Failed to send call results to webhook" | |
| ) | |
| return result | |
| except Exception as e: | |
| logger.error(f"Failed to process call: {str(e)}") | |
| raise HTTPException(status_code=500, detail=str(e)) | |
| async def update_webhook( | |
| webhook_url: str, | |
| customer: Customer = Depends(get_current_customer) | |
| ): | |
| """Update customer's webhook URL""" | |
| try: | |
| # Update webhook URL | |
| customer.webhook_url = webhook_url | |
| customer.save() | |
| return {"message": "Webhook URL updated successfully"} | |
| except Exception as e: | |
| logger.error(f"Failed to update webhook URL: {str(e)}") | |
| raise HTTPException(status_code=500, detail=str(e)) | |
| async def get_call( | |
| call_id: str, | |
| customer: Customer = Depends(get_current_customer) | |
| ): | |
| """Get call details from customer's database""" | |
| try: | |
| processor = CallProcessor(customer) | |
| call_data = processor.get_call_details(call_id) | |
| if not call_data: | |
| raise HTTPException(status_code=404, detail="Call not found") | |
| return call_data | |
| except Exception as e: | |
| logger.error(f"Failed to get call details: {str(e)}") | |
| raise HTTPException(status_code=500, detail=str(e)) | |
| async def search_calls( | |
| start_date: Optional[datetime] = None, | |
| end_date: Optional[datetime] = None, | |
| caller_number: Optional[str] = None, | |
| called_number: Optional[str] = None, | |
| customer: Customer = Depends(get_current_customer) | |
| ): | |
| """Search calls in customer's database""" | |
| try: | |
| processor = CallProcessor(customer) | |
| filters = { | |
| 'start_date': start_date, | |
| 'end_date': end_date, | |
| 'caller_number': caller_number, | |
| 'called_number': called_number | |
| } | |
| results = processor.search_calls(filters) | |
| return { | |
| 'calls': results, | |
| 'total': len(results) | |
| } | |
| except Exception as e: | |
| logger.error(f"Failed to search calls: {str(e)}") | |
| raise HTTPException(status_code=500, detail=str(e)) |