From e7f6374200b37e096a53c6fb5cef5f4753ab3122 Mon Sep 17 00:00:00 2001 From: Matthias Guillitte Date: Sun, 23 Aug 2026 17:06:59 +0200 Subject: [PATCH] Skip food safety alert for webex and sms --- cloud/app.py | 164 ++++++++++++++++++++++++++------------------------- 1 file changed, 84 insertions(+), 80 deletions(-) diff --git a/cloud/app.py b/cloud/app.py index b5bceca..98e57ae 100644 --- a/cloud/app.py +++ b/cloud/app.py @@ -323,97 +323,101 @@ def alert(): alert_type = alert_info.get("type", "Unknown") alert_message = alert_info.get("message", "No message provided") - client_name = f"Unknown Client (Orchestrator {orchestrator_id})" if orchestrator_id else "Unknown Client" - client_email = None - client_phone = None - - if orchestrator_id: - client_doc = client_collection.find_one({ - "devices": { - "$elemMatch": { - "device_type": "orchestrator", - "device_id": str(orchestrator_id) - } - } - }) - - if client_doc: - client_name = client_doc.get("client_name", client_name) - contact_info = client_doc.get("contact_info", {}) - client_email = contact_info.get("email") - client_phone = contact_info.get("phone") - webex_room_id = None - room_title = f"Support - {client_name}" room_link = None + room_title = None webex_status = "skipped" sms_sent = False - - three_minutes_ago = datetime.now(timezone.utc) - timedelta(minutes=3) - recent_alert = alert_collection.find_one({ - "orchestrator_id": orchestrator_id, - "webex_room_id": {"$exists": True, "$ne": None}, - "created_at": {"$gte": three_minutes_ago}, - "_id": {"$ne": saved_alert.inserted_id} - }, sort=[("created_at", DESCENDING)]) + if alert_type != "COOKING_SAFETY": - if recent_alert: - webex_room_id = recent_alert["webex_room_id"] - room_title = recent_alert.get("webex_room_title", room_title) - try: - webex_manager.send_alert_info_message( - room_id=webex_room_id, - alert_type=alert_type, - alert_message=alert_message, - alert_id=doc_id, - added_alert_info=True - ) - webex_status = "reused_room" - room_link = recent_alert.get("webex_room_link") or f"https://web.webex.com/spaces/{webex_room_id}" - except Exception as e: - current_app.logger.exception("Failed to send follow-up message to Webex room") - webex_status = f"message_failed: {str(e)}" - else: - try: - room_details = webex_manager.create_support_room( - client_name, - client_email, - alert_type, - alert_message, - doc_id - ) + client_name = f"Unknown Client (Orchestrator {orchestrator_id})" if orchestrator_id else "Unknown Client" + client_email = None + client_phone = None + + if orchestrator_id: + client_doc = client_collection.find_one({ + "devices": { + "$elemMatch": { + "device_type": "orchestrator", + "device_id": str(orchestrator_id) + } + } + }) - if isinstance(room_details, dict): - webex_room_id = room_details.get("id") - room_title = room_details.get("title", room_title) - room_link = room_details.get("meetingLink") or f"https://web.webex.com/spaces/{webex_room_id}" - else: - webex_room_id = room_details - room_link = f"https://web.webex.com/spaces/{webex_room_id}" + if client_doc: + client_name = client_doc.get("client_name", client_name) + contact_info = client_doc.get("contact_info", {}) + client_email = contact_info.get("email") + client_phone = contact_info.get("phone") - webex_status = "created_new_room" - except Exception as e: - current_app.logger.exception("Failed to create Webex support room") - webex_status = f"creation_failed: {str(e)}" + room_title = f"Support - {client_name}" - if webex_room_id and client_phone and not SAVEUP_TWILIO_API_TOKEN: - sms_sent = send_alert_sms( - to_phone=client_phone, - room_title=room_title, - room_link=room_link, - client_email=client_email - ) + three_minutes_ago = datetime.now(timezone.utc) - timedelta(minutes=3) + + recent_alert = alert_collection.find_one({ + "orchestrator_id": orchestrator_id, + "webex_room_id": {"$exists": True, "$ne": None}, + "created_at": {"$gte": three_minutes_ago}, + "_id": {"$ne": saved_alert.inserted_id} + }, sort=[("created_at", DESCENDING)]) - update_fields = {"sms_sent": sms_sent} - if webex_room_id: - update_fields.update({ - "webex_room_id": webex_room_id, - "webex_room_title": room_title, - "webex_room_link": room_link - }) + if recent_alert: + webex_room_id = recent_alert["webex_room_id"] + room_title = recent_alert.get("webex_room_title", room_title) + try: + webex_manager.send_alert_info_message( + room_id=webex_room_id, + alert_type=alert_type, + alert_message=alert_message, + alert_id=doc_id, + added_alert_info=True + ) + webex_status = "reused_room" + room_link = recent_alert.get("webex_room_link") or f"https://web.webex.com/spaces/{webex_room_id}" + except Exception as e: + current_app.logger.exception("Failed to send follow-up message to Webex room") + webex_status = f"message_failed: {str(e)}" + else: + try: + room_details = webex_manager.create_support_room( + client_name, + client_email, + alert_type, + alert_message, + doc_id + ) + + if isinstance(room_details, dict): + webex_room_id = room_details.get("id") + room_title = room_details.get("title", room_title) + room_link = room_details.get("meetingLink") or f"https://web.webex.com/spaces/{webex_room_id}" + else: + webex_room_id = room_details + room_link = f"https://web.webex.com/spaces/{webex_room_id}" - alert_collection.update_one({"_id": saved_alert.inserted_id}, {"$set": update_fields}) + webex_status = "created_new_room" + except Exception as e: + current_app.logger.exception("Failed to create Webex support room") + webex_status = f"creation_failed: {str(e)}" + + if webex_room_id and client_phone and not SAVEUP_TWILIO_API_TOKEN: + sms_sent = send_alert_sms( + to_phone=client_phone, + room_title=room_title, + room_link=room_link, + client_email=client_email + ) + + update_fields = {"sms_sent": sms_sent} + if webex_room_id: + update_fields.update({ + "webex_room_id": webex_room_id, + "webex_room_title": room_title, + "webex_room_link": room_link + }) + + alert_collection.update_one({"_id": saved_alert.inserted_id}, {"$set": update_fields}) # OData v4 Created Response Payload return jsonify({