|
from fastapi import Body, Request, HTTPException, status |
|
from fastapi.encoders import jsonable_encoder |
|
import sys |
|
|
|
from ..models.calls import UpdateCall, UserCall, UserCaptions |
|
|
|
def get_collection_calls(request: Request): |
|
try: |
|
|
|
return request.app.database["call_test"] |
|
except: |
|
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail=f"Unable to find call records Database.") |
|
|
|
|
|
def create_calls(collection, user: UserCall = Body(...)): |
|
calls = jsonable_encoder(user) |
|
|
|
new_calls = collection.insert_one(calls) |
|
|
|
created_calls = collection.find_one({"_id": new_calls.inserted_id}) |
|
|
|
return created_calls |
|
|
|
|
|
def list_calls(request: Request, limit: int): |
|
try: |
|
calls = list(get_collection_calls(request).find(limit = limit)) |
|
|
|
return calls |
|
except: |
|
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail=f"No existing call records yet.") |
|
|
|
|
|
'''Finding calls based on call id''' |
|
def find_calls(request: Request, call_id: str): |
|
|
|
user_calls = get_collection_calls(request).find_one({"call_id": call_id}) |
|
if user_calls is not None: |
|
return user_calls |
|
else: |
|
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail=f"Call with ID: '{call_id}' not found.") |
|
|
|
|
|
'''Finding calls based on user id''' |
|
def find_user_calls(request: Request, user_id: str): |
|
user_calls = list(get_collection_calls(request).find({"$or": [{"caller_id": user_id}, {"callee_id": user_id}]})) |
|
if len(user_calls): |
|
return user_calls |
|
else: |
|
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail=f"User with ID: '{user_id}' has no calls yet.") |
|
|
|
|
|
'''Finding calls based on key terms list''' |
|
def list_transcripts_by_key_terms(request: Request, key_terms_list: list[str] = Body(...)): |
|
key_terms_list = jsonable_encoder(key_terms_list) |
|
|
|
call_records = list(get_collection_calls(request).find({"key_terms": {"$in": key_terms_list}}, {'_id': 0})) |
|
|
|
|
|
if len(call_records): |
|
return call_records |
|
else: |
|
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail=f"Call with key terms: '{key_terms_list}' not found!") |
|
|
|
|
|
'''Finding calls based on date ranges''' |
|
def list_transcripts_by_dates(request: Request, start_date: str, end_date: str): |
|
print(start_date, end_date) |
|
|
|
start_date = f'{start_date}T00:00:00' |
|
end_date = f'{end_date}T00:00:00' |
|
|
|
call_records = list(get_collection_calls(request).find({"date":{"$gte": start_date, "$lte": end_date}})) |
|
|
|
if len(call_records): |
|
return call_records |
|
else: |
|
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail=f"Call with creation date between: '{start_date} - {end_date}' not found!") |
|
|
|
|
|
'''Finding calls based on call lengths''' |
|
def list_transcripts_by_duration(request: Request, min_len: int, max_len: int): |
|
|
|
call_records = list(get_collection_calls(request).find({"duration":{"$gte": min_len, "$lte": max_len}})) |
|
|
|
if len(call_records): |
|
return call_records |
|
else: |
|
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail=f"Call with duration between: '{min_len} - {max_len}' milliseconds not found!") |
|
|
|
|
|
def update_calls(collection, call_id: str, calls: UpdateCall = Body(...)): |
|
|
|
calls = {k: v for k, v in calls.items() if v is not None} |
|
if len(calls) >= 1: |
|
update_result = collection.update_one({"call_id": call_id}, {"$set": calls}) |
|
|
|
if update_result.modified_count == 0: |
|
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail=f"Call not updated!") |
|
|
|
if (existing_item := collection.find_one({"call_id": call_id})) is not None: |
|
return existing_item |
|
|
|
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail=f"Call not found!") |
|
|
|
def update_captions(collection, call_id: str, calls: UserCaptions = Body(...)): |
|
|
|
calls = {k: v for k, v in calls.items() if v is not None} |
|
if len(calls) >= 1: |
|
update_result = collection.update_one({"call_id": call_id}, |
|
{"$push": {"captions": calls}}) |
|
|
|
if update_result.modified_count == 0: |
|
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail=f"Captions not updated!") |
|
|
|
if (existing_item := collection.find_one({"call_id": call_id})) is not None: |
|
return existing_item |
|
|
|
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail=f"Captions not found!") |
|
|
|
|
|
def delete_calls(request: Request, call_id: str): |
|
deleted_calls = get_collection_calls(request).delete_one({"call_id": call_id}) |
|
|
|
if deleted_calls.deleted_count == 1: |
|
return f"Call deleted sucessfully!" |
|
|
|
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail=f"Call not found!") |
|
|