From cfe702eeebef5d4063616575b8c8a18ab83031a7 Mon Sep 17 00:00:00 2001 From: root Date: Mon, 24 Aug 2026 12:16:46 +0000 Subject: [PATCH] fix(meters): centralize alert investigation workflow --- app_new/core/services/meter_analytics.py | 12 ++++++++++++ app_new/planning/meter_readings.py | 9 +++++---- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/app_new/core/services/meter_analytics.py b/app_new/core/services/meter_analytics.py index 3fe74a2..ef75ba5 100644 --- a/app_new/core/services/meter_analytics.py +++ b/app_new/core/services/meter_analytics.py @@ -258,6 +258,18 @@ def close_meter_alert(*, alert, conclusion, comment=None, user_id=None, commit=T return alert +def investigate_meter_alert(*, alert, comment=None, user_id=None, commit=True): + """Passe une alerte ouverte en investigation via le service métier.""" + if alert.status not in {"OPEN", "INVESTIGATING"}: + raise ValueError("Une alerte clôturée ne peut plus passer en investigation.") + alert.status = "INVESTIGATING" + if comment: + alert.comment = comment + if commit: + db.session.commit() + return alert + + def visible_open_meter_alerts(user): """Retourne les alertes visibles dans Ma journée selon le RBAC métier.""" query = MeterAlert.query.filter(MeterAlert.status.in_(("OPEN", "INVESTIGATING"))) diff --git a/app_new/planning/meter_readings.py b/app_new/planning/meter_readings.py index 1b6b839..6ce3d56 100644 --- a/app_new/planning/meter_readings.py +++ b/app_new/planning/meter_readings.py @@ -20,7 +20,10 @@ from ..core.services.meter_reading_planning import ( create_round, ensure_occurrences_for_operational_date, generate_occurrences, record_occurrence_reading, record_occurrence_without_reading, ) -from ..core.services.meter_analytics import consumption_intervals, visible_open_meter_alerts, close_meter_alert +from ..core.services.meter_analytics import ( + consumption_intervals, visible_open_meter_alerts, close_meter_alert, + investigate_meter_alert, +) from ..core.models.planning import MeterAlert, MeterAlertRule from .schedules import planning_bp @@ -224,9 +227,7 @@ def close_meter_alert_route(alert_id): alert = MeterAlert.query.get_or_404(alert_id) action = request.form.get('status', 'CLOSED').upper() if action == 'INVESTIGATING': - alert.status = 'INVESTIGATING' - alert.comment = request.form.get('comment') or alert.comment - db.session.commit() + investigate_meter_alert(alert=alert, comment=request.form.get('comment'), user_id=current_user.id) flash('Alerte passée en investigation.', 'success') else: try: