|
| 1 | +import datetime |
1 | 2 | import json |
2 | 3 | import logging |
3 | 4 |
|
@@ -1078,49 +1079,80 @@ def get_calls(space_id: int, query_params: dict): |
1078 | 1079 | collection_name = "tasks" |
1079 | 1080 | skip, limit = getPagination(query_params, 30) |
1080 | 1081 | query = { |
1081 | | - "space_id": str(space_id), |
1082 | | - "output": {"$ne": {}, "$exists": True}, |
| 1082 | + "space_id": str(space_id) |
| 1083 | + } |
| 1084 | + search_str = query_params.get("search") |
| 1085 | + call_type = query_params.get("call_type") |
| 1086 | + date_from = query_params.get("from_ts") |
| 1087 | + date_to = query_params.get("to_ts") |
| 1088 | + status = query_params.get("status") |
| 1089 | + |
| 1090 | + filters = [] |
| 1091 | + if date_from or date_to: |
| 1092 | + date_filter = {} |
| 1093 | + try: |
| 1094 | + if date_from: |
| 1095 | + date_filter["$gte"] = datetime.fromtimestamp(int(date_from)) |
| 1096 | + if date_to: |
| 1097 | + date_filter["$lte"] = datetime.fromtimestamp(int(date_to)) |
| 1098 | + filters.append({"created": date_filter}) |
| 1099 | + except Exception: |
| 1100 | + pass |
| 1101 | + |
| 1102 | + if call_type: |
| 1103 | + if call_type == "inbound": |
| 1104 | + call_type_values = ["inbound", "inboundPhoneCall"] |
| 1105 | + elif call_type == "outbound": |
| 1106 | + call_type_values = ["outbound", "outboundPhoneCall"] |
| 1107 | + else: |
| 1108 | + call_type_values = [call_type] |
| 1109 | + filters.append( |
| 1110 | + { |
| 1111 | + "$or": [ |
| 1112 | + {"output.call_type": {"$in": call_type_values}}, |
| 1113 | + {"output.type": {"$in": call_type_values}}, |
| 1114 | + ] |
| 1115 | + } |
| 1116 | + ) |
| 1117 | + |
| 1118 | + if search_str: |
| 1119 | + filters.append( |
| 1120 | + { |
| 1121 | + "$or": [ |
| 1122 | + {"input.name": {"$regex": search_str, "$options": "i"}}, |
| 1123 | + {"input.phone": {"$regex": search_str, "$options": "i"}}, |
| 1124 | + {"output.customer": {"$regex": search_str, "$options": "i"}}, |
| 1125 | + { |
| 1126 | + "output.contact_number": { |
| 1127 | + "$regex": search_str, |
| 1128 | + "$options": "i", |
| 1129 | + } |
| 1130 | + }, |
| 1131 | + ] |
| 1132 | + } |
| 1133 | + ) |
| 1134 | + |
| 1135 | + if status: |
| 1136 | + status_values = [s.strip().replace("+", " ") for s in status.split(",")] |
| 1137 | + |
| 1138 | + filters.append( |
| 1139 | + {"output.post_call_data.summary.status": {"$in": status_values}} |
| 1140 | + ) |
| 1141 | + |
| 1142 | + if filters: |
| 1143 | + query = {**query, **{"$and": filters}} |
| 1144 | + |
| 1145 | + projection = { |
| 1146 | + "_id": 1, |
| 1147 | + "task_id": 1, |
| 1148 | + "created": 1, |
| 1149 | + "status": 1, |
| 1150 | + "input.call_type": 1, |
| 1151 | + "input.name": 1, |
| 1152 | + "input.contact_number": 1, |
| 1153 | + "output.call_type": 1, |
| 1154 | + "output.customer": 1, |
1083 | 1155 | } |
1084 | | - exclude_fields = [ |
1085 | | - "retry_attempt", |
1086 | | - "thread_id", |
1087 | | - "user_info", |
1088 | | - "call_analytics", |
1089 | | - "execution_analytics", |
1090 | | - "last_status_change", |
1091 | | - "provider", |
1092 | | - "attachments", |
1093 | | - "ref_id", |
1094 | | - "failure_count", |
1095 | | - "last_failure_reason", |
1096 | | - "scheduled_timestamp", |
1097 | | - "task", |
1098 | | - "collection_ref", |
1099 | | - "run_id", |
1100 | | - "user_org_id", |
1101 | | - "user", |
1102 | | - "space_id", |
1103 | | - "modified", |
1104 | | - "assignee", |
1105 | | - "execution_type", |
1106 | | - "input.token", |
1107 | | - "input.quality", |
1108 | | - "output.call_id", |
1109 | | - "output.contact_number", |
1110 | | - "output.transcript", |
1111 | | - "output.post_call_data", |
1112 | | - "output.metadata", |
1113 | | - "output.call_end_reason", |
1114 | | - "output.recording_url", |
1115 | | - "output.start_time", |
1116 | | - "output.end_time", |
1117 | | - "output.assistant_number", |
1118 | | - "output.call_summary", |
1119 | | - "output.duration", |
1120 | | - "output.cost", |
1121 | | - "output.call_status", |
1122 | | - ] |
1123 | | - projection = {field: 0 for field in exclude_fields} |
1124 | 1156 |
|
1125 | 1157 | try: |
1126 | 1158 | collection = MongoDBQueryManager.get_collection(collection_name) |
|
0 commit comments