Skip food safety alert for webex and sms
This commit is contained in:
+84
-80
@@ -323,97 +323,101 @@ def alert():
|
|||||||
alert_type = alert_info.get("type", "Unknown")
|
alert_type = alert_info.get("type", "Unknown")
|
||||||
alert_message = alert_info.get("message", "No message provided")
|
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
|
webex_room_id = None
|
||||||
room_title = f"Support - {client_name}"
|
|
||||||
room_link = None
|
room_link = None
|
||||||
|
room_title = None
|
||||||
webex_status = "skipped"
|
webex_status = "skipped"
|
||||||
sms_sent = False
|
sms_sent = False
|
||||||
|
|
||||||
three_minutes_ago = datetime.now(timezone.utc) - timedelta(minutes=3)
|
|
||||||
|
|
||||||
recent_alert = alert_collection.find_one({
|
if alert_type != "COOKING_SAFETY":
|
||||||
"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 recent_alert:
|
client_name = f"Unknown Client (Orchestrator {orchestrator_id})" if orchestrator_id else "Unknown Client"
|
||||||
webex_room_id = recent_alert["webex_room_id"]
|
client_email = None
|
||||||
room_title = recent_alert.get("webex_room_title", room_title)
|
client_phone = None
|
||||||
try:
|
|
||||||
webex_manager.send_alert_info_message(
|
if orchestrator_id:
|
||||||
room_id=webex_room_id,
|
client_doc = client_collection.find_one({
|
||||||
alert_type=alert_type,
|
"devices": {
|
||||||
alert_message=alert_message,
|
"$elemMatch": {
|
||||||
alert_id=doc_id,
|
"device_type": "orchestrator",
|
||||||
added_alert_info=True
|
"device_id": str(orchestrator_id)
|
||||||
)
|
}
|
||||||
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):
|
if client_doc:
|
||||||
webex_room_id = room_details.get("id")
|
client_name = client_doc.get("client_name", client_name)
|
||||||
room_title = room_details.get("title", room_title)
|
contact_info = client_doc.get("contact_info", {})
|
||||||
room_link = room_details.get("meetingLink") or f"https://web.webex.com/spaces/{webex_room_id}"
|
client_email = contact_info.get("email")
|
||||||
else:
|
client_phone = contact_info.get("phone")
|
||||||
webex_room_id = room_details
|
|
||||||
room_link = f"https://web.webex.com/spaces/{webex_room_id}"
|
|
||||||
|
|
||||||
webex_status = "created_new_room"
|
room_title = f"Support - {client_name}"
|
||||||
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:
|
three_minutes_ago = datetime.now(timezone.utc) - timedelta(minutes=3)
|
||||||
sms_sent = send_alert_sms(
|
|
||||||
to_phone=client_phone,
|
recent_alert = alert_collection.find_one({
|
||||||
room_title=room_title,
|
"orchestrator_id": orchestrator_id,
|
||||||
room_link=room_link,
|
"webex_room_id": {"$exists": True, "$ne": None},
|
||||||
client_email=client_email
|
"created_at": {"$gte": three_minutes_ago},
|
||||||
)
|
"_id": {"$ne": saved_alert.inserted_id}
|
||||||
|
}, sort=[("created_at", DESCENDING)])
|
||||||
|
|
||||||
update_fields = {"sms_sent": sms_sent}
|
if recent_alert:
|
||||||
if webex_room_id:
|
webex_room_id = recent_alert["webex_room_id"]
|
||||||
update_fields.update({
|
room_title = recent_alert.get("webex_room_title", room_title)
|
||||||
"webex_room_id": webex_room_id,
|
try:
|
||||||
"webex_room_title": room_title,
|
webex_manager.send_alert_info_message(
|
||||||
"webex_room_link": room_link
|
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
|
# OData v4 Created Response Payload
|
||||||
return jsonify({
|
return jsonify({
|
||||||
|
|||||||
Reference in New Issue
Block a user