From cd9eb27a9888a087735f730d8e51b4e43d270313 Mon Sep 17 00:00:00 2001 From: Atharv1705 Date: Fri, 13 Nov 2026 14:42:42 +0530 Subject: [PATCH] fix: suppress critical complaint alert once restored_at is set --- backend/app/services/alerts.py | 8 ++++++-- backend/tests/test_complaints.py | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 2 deletions(-) diff --git a/backend/app/services/alerts.py b/backend/app/services/alerts.py index b7c65b0..e862e93 100644 --- a/backend/app/services/alerts.py +++ b/backend/app/services/alerts.py @@ -278,11 +278,15 @@ def run_alert_check(db: Session) -> dict[str, object]: created_at=eff_now, ) - # 7. Critical complaints open >= escalation_open_complaint_hours + # 7. Critical complaints open >= escalation_open_complaint_hours (and not yet restored) crit_complaints = ( db.query(Complaint) .options(joinedload(Complaint.asset)) - .filter(Complaint.call_status != "closed", Complaint.priority == "critical") + .filter( + Complaint.call_status != "closed", + Complaint.priority == "critical", + Complaint.restored_at.is_(None), + ) .all() ) threshold_secs = cfg["critical_hours"] * 3600 diff --git a/backend/tests/test_complaints.py b/backend/tests/test_complaints.py index c4b2b99..705b1cc 100644 --- a/backend/tests/test_complaints.py +++ b/backend/tests/test_complaints.py @@ -553,6 +553,38 @@ def test_sla_breach_alert_emitted_and_suppressed_when_restored(client, db): .all() ) assert crit_unrestored == [] + + # When open > 24h, unrestored complaint triggers critical_complaint alert, + # but restored complaint does NOT trigger it even if not closed yet + past_26h = now - timedelta(hours=26) + c_unrestored.registered_at = past_26h + c_restored.registered_at = past_26h + db.commit() + run_alert_check(db) + db.commit() + + crit_unrestored_26h = ( + db.query(Notification) + .filter( + Notification.type == "critical_complaint", + Notification.entity_type == "complaint", + Notification.entity_id == cid_unrestored, + ) + .all() + ) + assert len(crit_unrestored_26h) == 1 + assert "Critical Complaint Open > 24h" in crit_unrestored_26h[0].title + + crit_restored_26h = ( + db.query(Notification) + .filter( + Notification.type == "critical_complaint", + Notification.entity_type == "complaint", + Notification.entity_id == cid_restored, + ) + .all() + ) + assert crit_restored_26h == [] finally: db.query(Notification).filter( Notification.entity_type == "complaint",