benjolo commited on
Commit
21b18a2
·
verified ·
1 Parent(s): d816fe6

Update backend/mongodb/operations/calls.py

Browse files
Files changed (1) hide show
  1. backend/mongodb/operations/calls.py +17 -25
backend/mongodb/operations/calls.py CHANGED
@@ -4,37 +4,27 @@ import sys
4
  # sys.path.append('/Users/benolojo/DCU/CA4/ca400_FinalYearProject/2024-ca400-olojob2-majdap2/src/backend/src/')
5
  from ..models.calls import UpdateCall, UserCall, UserCaptions
6
 
7
- def get_collection_calls(request: Request):
8
- try:
9
- # return request.app.database["call_records"]
10
- return request.app.database["call_test"]
11
- except:
12
- raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail=f"Unable to find call records Database.")
13
 
14
  # Used within calls to create call record in main.py
15
  def create_calls(collection, user: UserCall = Body(...)):
16
  calls = jsonable_encoder(user)
17
- # new_calls = get_collection_calls(request).insert_one(calls)
18
  new_calls = collection.insert_one(calls)
19
- # created_calls = get_collection_calls(request).find_one({"_id": new_calls.inserted_id})
20
  created_calls = collection.find_one({"_id": new_calls.inserted_id})
21
 
22
  return created_calls
23
 
24
 
25
- def list_calls(request: Request, limit: int):
26
  try:
27
- calls = list(get_collection_calls(request).find(limit = limit))
28
- # dateTest = calls[2]['date']
29
  return calls
30
  except:
31
  raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail=f"No existing call records yet.")
32
 
33
 
34
  '''Finding calls based on call id'''
35
- def find_calls(request: Request, call_id: str):
36
- # if user_calls := get_collection_calls(request).find_one({"call_id": call_id}) is not None:
37
- user_calls = get_collection_calls(request).find_one({"call_id": call_id})
38
  if user_calls is not None:
39
  return user_calls
40
  else:
@@ -42,8 +32,8 @@ def find_calls(request: Request, call_id: str):
42
 
43
 
44
  '''Finding calls based on user id'''
45
- def find_user_calls(request: Request, user_id: str):
46
- user_calls = list(get_collection_calls(request).find({"$or": [{"caller_id": user_id}, {"callee_id": user_id}]})) # match on caller or callee ID
47
  if len(user_calls):
48
  return user_calls
49
  else:
@@ -51,10 +41,10 @@ def find_user_calls(request: Request, user_id: str):
51
 
52
 
53
  '''Finding calls based on key terms list'''
54
- def list_transcripts_by_key_terms(request: Request, key_terms_list: list[str] = Body(...)):
55
  key_terms_list = jsonable_encoder(key_terms_list)
56
 
57
- call_records = list(get_collection_calls(request).find({"key_terms": {"$in": key_terms_list}}, {'_id': 0})) # exclude returning ObjectID in find()
58
 
59
  # Check if any call records were returned
60
  if len(call_records):
@@ -64,13 +54,14 @@ def list_transcripts_by_key_terms(request: Request, key_terms_list: list[str] =
64
 
65
 
66
  '''Finding calls based on date ranges'''
67
- def list_transcripts_by_dates(request: Request, start_date: str, end_date: str):
68
- print(start_date, end_date)
 
69
  # Convert strings to date string in YYYY-MM-ddT00:00:00 format
70
  start_date = f'{start_date}T00:00:00'
71
  end_date = f'{end_date}T00:00:00'
72
 
73
- call_records = list(get_collection_calls(request).find({"date":{"$gte": start_date, "$lte": end_date}}))
74
 
75
  if len(call_records):
76
  return call_records
@@ -79,9 +70,9 @@ def list_transcripts_by_dates(request: Request, start_date: str, end_date: str):
79
 
80
 
81
  '''Finding calls based on call lengths'''
82
- def list_transcripts_by_duration(request: Request, min_len: int, max_len: int):
83
 
84
- call_records = list(get_collection_calls(request).find({"duration":{"$gte": min_len, "$lte": max_len}}))
85
 
86
  if len(call_records):
87
  return call_records
@@ -103,6 +94,7 @@ def update_calls(collection, call_id: str, calls: UpdateCall = Body(...)):
103
 
104
  raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail=f"Call not found!")
105
 
 
106
  def update_captions(collection, call_id: str, calls: UserCaptions = Body(...)):
107
  # calls = {k: v for k, v in calls.model_dump().items() if v is not None}
108
  calls = {k: v for k, v in calls.items() if v is not None}
@@ -119,8 +111,8 @@ def update_captions(collection, call_id: str, calls: UserCaptions = Body(...)):
119
  raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail=f"Captions not found!")
120
 
121
 
122
- def delete_calls(request: Request, call_id: str):
123
- deleted_calls = get_collection_calls(request).delete_one({"call_id": call_id})
124
 
125
  if deleted_calls.deleted_count == 1:
126
  return f"Call deleted sucessfully!"
 
4
  # sys.path.append('/Users/benolojo/DCU/CA4/ca400_FinalYearProject/2024-ca400-olojob2-majdap2/src/backend/src/')
5
  from ..models.calls import UpdateCall, UserCall, UserCaptions
6
 
 
 
 
 
 
 
7
 
8
  # Used within calls to create call record in main.py
9
  def create_calls(collection, user: UserCall = Body(...)):
10
  calls = jsonable_encoder(user)
 
11
  new_calls = collection.insert_one(calls)
 
12
  created_calls = collection.find_one({"_id": new_calls.inserted_id})
13
 
14
  return created_calls
15
 
16
 
17
+ def list_calls(collection, limit: int):
18
  try:
19
+ calls = collection.find(limit = limit))
 
20
  return calls
21
  except:
22
  raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail=f"No existing call records yet.")
23
 
24
 
25
  '''Finding calls based on call id'''
26
+ def find_calls(collection, call_id: str):
27
+ user_calls = collection.find_one({"call_id": call_id})
 
28
  if user_calls is not None:
29
  return user_calls
30
  else:
 
32
 
33
 
34
  '''Finding calls based on user id'''
35
+ def find_user_calls(collection, user_id: str):
36
+ user_calls = list(collection.find({"$or": [{"caller_id": user_id}, {"callee_id": user_id}]})) # match on caller or callee ID
37
  if len(user_calls):
38
  return user_calls
39
  else:
 
41
 
42
 
43
  '''Finding calls based on key terms list'''
44
+ def list_transcripts_by_key_terms(collection, key_terms_list: list[str] = Body(...)):
45
  key_terms_list = jsonable_encoder(key_terms_list)
46
 
47
+ call_records = list(collection.find({"key_terms": {"$in": key_terms_list}}, {'_id': 0})) # exclude returning ObjectID in find()
48
 
49
  # Check if any call records were returned
50
  if len(call_records):
 
54
 
55
 
56
  '''Finding calls based on date ranges'''
57
+ def list_transcripts_by_dates(collection, start_date: str, end_date: str):
58
+ # print(start_date, end_date)
59
+
60
  # Convert strings to date string in YYYY-MM-ddT00:00:00 format
61
  start_date = f'{start_date}T00:00:00'
62
  end_date = f'{end_date}T00:00:00'
63
 
64
+ call_records = list(collection.find({"date":{"$gte": start_date, "$lte": end_date}}))
65
 
66
  if len(call_records):
67
  return call_records
 
70
 
71
 
72
  '''Finding calls based on call lengths'''
73
+ def list_transcripts_by_duration(collection, min_len: int, max_len: int):
74
 
75
+ call_records = list(collection.find({"duration":{"$gte": min_len, "$lte": max_len}}))
76
 
77
  if len(call_records):
78
  return call_records
 
94
 
95
  raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail=f"Call not found!")
96
 
97
+
98
  def update_captions(collection, call_id: str, calls: UserCaptions = Body(...)):
99
  # calls = {k: v for k, v in calls.model_dump().items() if v is not None}
100
  calls = {k: v for k, v in calls.items() if v is not None}
 
111
  raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail=f"Captions not found!")
112
 
113
 
114
+ def delete_calls(collection, call_id: str):
115
+ deleted_calls = collection.delete_one({"call_id": call_id})
116
 
117
  if deleted_calls.deleted_count == 1:
118
  return f"Call deleted sucessfully!"