from fastapi import APIRouter, Body, Request, status, HTTPException from typing import List from datetime import date import sys from ..operations import calls as calls from ..models.calls import UserCaptions, UserCall, UpdateCall from ..endpoints.users import get_collection_users router = APIRouter(prefix="/call", tags=["Calls"]) def get_collection_calls(request: Request): try: return request.app.database["call_records"] # return request.app.database["call_test"] except: raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail=f"Unable to find call records Database.") @router.post("/create-call", response_description="Create a new user call record", status_code=status.HTTP_201_CREATED, response_model=UserCall) async def create_calls(request: Request, user_calls: UserCall = Body(...)): collection = get_collection_calls(request) return calls.create_calls(collection, user_calls) @router.get("/list-call", response_description="List all existing call records", response_model=List[UserCall]) async def list_calls(request: Request, limit: int): collection = get_collection_calls(request) return calls.list_calls(collection, 100) @router.get("/find-call/{call_id}", response_description="Find user's calls based on User ID", response_model=UserCall) async def find_call(request: Request, call_id: str): collection = get_collection_calls(request) return calls.find_call(collection, call_id) @router.get("/find-user/{user_id}", response_description="Find user's calls based on User ID", response_model=List[UserCall]) async def find_user_calls(request: Request, user_id: str): collection = get_collection_calls(request) return calls.find_user_calls(collection, user_id) @router.get("/get-captions/{user_id}", response_description="Find user's calls based on User ID") async def get_caption_text(request: Request, user_id: str): collection = get_collection_calls(request) return calls.get_caption_text(collection, user_id) '''Key terms list can have variable length -> using POST request over GET''' @router.post("/find-term/", response_description="Find calls based on key term list", response_model=List[UserCall]) async def list_transcripts_by_key_terms(request: Request, key_terms: List[str]): collection = get_collection_calls(request) return calls.list_transcripts_by_key_terms(collection, key_terms) @router.get("/find-date/{start_date}/{end_date}", response_description="Find calls based on date ranges", response_model=List[UserCall]) async def list_transcripts_by_dates(request: Request, start_date: str, end_date: str): collection = get_collection_calls(request) return calls.list_transcripts_by_dates(collection, start_date, end_date) @router.get("/find-duration/{min_len}/{max_len}", response_description="Find calls based on call duration in minutes", response_model=List[UserCall]) async def list_transcripts_by_duration(request: Request, min_len: int, max_len: int): collection = get_collection_calls(request) return calls.list_transcripts_by_duration(collection, min_len, max_len) @router.put("/update-call/{call_id}", response_description="Update an existing call", response_model=UpdateCall) async def update_calls(request: Request, call_id: str, user_calls: UpdateCall = Body(...)): collection = get_collection_calls(request) return calls.update_calls(collection, call_id, user_calls) @router.put("/update-captions/{call_id}", response_description="Update an existing call", response_model=UpdateCall) async def update_captions(request: Request, call_id: str, user_calls: UserCaptions = Body(...)): call_collection = get_collection_calls(request) user_collection = get_collection_users(request) return calls.update_captions(call_collection, user_collection, call_id, user_calls) @router.delete("/delete-call/{call_id}", response_description="Delete a call by its id") async def delete_call(request: Request, call_id: str): collection = get_collection_calls(request) return calls.delete_calls(collection, call_id) @router.get("/full-text-search/{query}", response_description="Perform full text search on caption fields", response_model=List[UserCall]) async def full_text_search(request: Request, query: str): collection = get_collection_calls(request) return calls.full_text_search(collection, query) @router.get("/fuzzy-search/{query}", response_description="Perform fuzzy text search on caption fields", response_model=List[UserCall]) async def fuzzy_search(request: Request, query: str): collection = get_collection_calls(request) return calls.fuzzy_search(collection, query) @router.get("/summarise/{call_id}", response_description="Perform gpt-3.5 summarisation on call_id") async def summarise(request: Request, call_id: str): collection = get_collection_calls(request) return calls.summarise(collection, call_id)