From c526284d981f47d200d8e31c65c4105bf1a4d332 Mon Sep 17 00:00:00 2001 From: Edwin Eames Date: Fri, 22 Aug 2025 14:48:42 -0400 Subject: [PATCH] working calender --- app/service/views.py | 45 +++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 44 insertions(+), 1 deletion(-) diff --git a/app/service/views.py b/app/service/views.py index 45b964d..f602404 100644 --- a/app/service/views.py +++ b/app/service/views.py @@ -113,4 +113,47 @@ def update_service_call(id): return jsonify({"ok": True, "service": service_schema.dump(service_record)}), 200 except Exception as e: db.session.rollback() - return jsonify({"error": str(e)}), 500 \ No newline at end of file + return jsonify({"error": str(e)}), 500 + + + +@service.route("/upcoming/count", methods=["GET"]) +def get_upcoming_service_calls_count(): + """ + Efficiently counts the number of all future service calls from today onwards. + """ + # 1. Get the current time + now = datetime.now() + + # 2. Build the query and use the database's optimized count() function + try: + count = ( + db.session.query(Service_Service) + .filter(Service_Service.scheduled_date >= now) + .count() + ) + # 3. Return the count in a simple JSON object + return jsonify({"count": count}), 200 + except Exception as e: + # Return an error if the query fails + return jsonify({"error": str(e)}), 500 + + +@service.route("/for-customer/", methods=["GET"]) +def get_service_calls_for_customer(customer_id): + """ + Fetches all service calls for a specific customer, ordered by most recent first. + """ + # Query the database, filtering by customer_id and ordering by date + service_records = ( + Service_Service.query + .filter_by(customer_id=customer_id) + .order_by(Service_Service.scheduled_date.desc()) # .desc() for newest first + .all() + ) + + # Use the schema to convert the list of objects to JSON + service_schema = Service_Service_schema(many=True) + result = service_schema.dump(service_records) + + return jsonify(result), 200 \ No newline at end of file