File size: 3,168 Bytes
7d72bcf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
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__)

@router.post("/process-call")
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))

@router.post("/webhook")
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))

@router.get("/calls/{call_id}")
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))

@router.get("/calls/search")
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))