retrieve agents by provided customer (#73)
taylor_socfortress committed
Jul 26, 2023 at 11:13 UTC
2bf3a83f34cc1c3b46b279ce8ff1a812c83a9b8a
2 files changed
+67
-8
backend/app/routes/customers.py
+19
-8
@@ -5,6 +5,8 @@ from flask import jsonify
5
from flask import request
6
from loguru import logger
7
8
+from app.models.agents import AgentMetadata
9
+from app.models.agents import agent_metadatas_schema
10
from app.services.Customers.universal import UniversalCustomers
11
from app.services.Customers.universal import UniversalCustomersMeta
12
@@ -277,8 +279,8 @@ def read_customer_and_meta_details(id: int):
279
return jsonify({"message": "Customer or Customer Meta not found", "success": False}), 404
280
281
280
-@bp.route("/customers/healthcheck/agents/<int:id>", methods=["GET"])
281
-def read_customer_healthcheck(id: int):
282
+@bp.route("/customers/agents/<int:id>", methods=["GET"])
283
+def read_customer_agents(id: int):
284
"""
285
Endpoint to fetch the `customerCode` from the `customers_meta` table for the given customer id.
286
Then, it uses the customer code to fetch all agents from the `agent_metadata` table where the `customerCode`
@@ -289,12 +291,21 @@ def read_customer_healthcheck(id: int):
291
containing the customer code and the list of agents, and the second element is
292
the HTTP status code.
293
"""
292
- logger.info(f"Received request to get customer healthcheck with id {id}")
294
+ logger.info(f"Received request to get customer agents with id {id}")
295
customer_meta = UniversalCustomersMeta.read_by_id(id)
296
if customer_meta:
297
customer_code = customer_meta["customerCode"]
296
- logger.info(f"Customer code for customer id {id} is {customer_code}")
297
- exit(0)
298
- agents = UniversalCustomersMeta.read_agents_by_customer_code(customer_code)
299
- return jsonify({"customerCode": customer_code, "agents": agents, "message": "Customer healthcheck fetched.", "success": True}), 200
300
- return jsonify({"message": "Customer Meta not found", "success": False}), 404
298
+ agents = AgentMetadata.query.filter(AgentMetadata.label.like(f"%{customer_code}%")).all()
299
+ if agents:
300
+ return (
301
+ jsonify(
302
+ {
303
+ "customerCode": customer_code,
304
+ "agents": agent_metadatas_schema.dump(agents),
305
+ "message": "Customer agents fetched.",
306
+ "success": True,
307
+ },
308
+ ),
309
+ 200,
310
+ )
311
+ return jsonify({"message": "No agents found for this customer.", "success": False}), 404
backend/app/static/swagger.json
+48
@@ -832,6 +832,54 @@
832
}
833
}
834
},
835
+ "/customers/agents/{id}": {
836
+ "get": {
837
+ "tags": ["Customers"],
838
+ "summary": "Get a customer",
839
+ "description": "Endpoint to retrieve a customer details from the `customers` and `customers_meta` tables.",
840
+ "parameters": [
841
+ {
842
+ "name": "id",
843
+ "in": "path",
844
+ "description": "ID of the customer to retrieve",
845
+ "required": true,
846
+ "type": "integer"
847
+ }
848
+ ],
849
+ "responses": {
850
+ "200": {
851
+ "description": "Successful operation",
852
+ "content": {
853
+ "application/json": {
854
+ "schema": {
855
+ "type": "object",
856
+ "properties": {
857
+ "message": { "type": "string", "example": "Customer retrieved successfully." },
858
+ "success": { "type": "boolean", "example": true },
859
+ "customer": { "$ref": "#/definitions/Customer" },
860
+ "customerMeta": { "$ref": "#/definitions/CustomerMeta" }
861
+ }
862
+ }
863
+ }
864
+ }
865
+ },
866
+ "404": {
867
+ "description": "Customer not found",
868
+ "content": {
869
+ "application/json": {
870
+ "schema": {
871
+ "type": "object",
872
+ "properties": {
873
+ "message": { "type": "string", "example": "Customer not found." },
874
+ "success": { "type": "boolean", "example": false }
875
+ }
876
+ }
877
+ }
878
+ }
879
+ }
880
+ }
881
+ }
882
+ },
883
"/agents": {
884
"get": {
885
"tags": ["Agents"],