working calender

This commit is contained in:
2025-08-22 14:48:42 -04:00
parent 51f2e986a9
commit c526284d98

View File

@@ -114,3 +114,46 @@ def update_service_call(id):
except Exception as e: except Exception as e:
db.session.rollback() db.session.rollback()
return jsonify({"error": str(e)}), 500 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/<int:customer_id>", 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