diff --git a/routes/reslevis.py b/routes/reslevis.py index 8c5bb95..89f5a3b 100644 --- a/routes/reslevis.py +++ b/routes/reslevis.py @@ -3,6 +3,7 @@ import json import os import tempfile from urllib.parse import urlencode +from uuid import UUID from fastapi import APIRouter, Depends, File, Form, HTTPException, Query, Request, UploadFile import httpx @@ -176,6 +177,48 @@ def _uid_from_claims(claims: dict) -> str: return str(uid) +def _uuid_or_none(value): + if value in ("", None, 0, "0"): + return None + try: + return str(UUID(str(value))) + except (TypeError, ValueError, AttributeError): + return None + + +def _operator_uuid_by_name(value): + name = str(value).strip() + if not name: + return None + + try: + rows = operator_repo.list() + except Exception: + return None + + target = name.lower() + for row in rows: + if not isinstance(row, dict): + continue + if str(row.get("name") or "").strip().lower() == target: + return _uuid_or_none(row.get("id")) + return None + + +def _normalize_alarm_core(row: dict) -> dict: + row = dict(row) + operator_raw = row.get("operator") + operator_uuid = _uuid_or_none(operator_raw) + + if operator_uuid is None and operator_raw not in ("", None, 0, "0"): + operator_uuid = _operator_uuid_by_name(operator_raw) + if operator_uuid is None and not row.get("operatorName"): + row["operatorName"] = str(operator_raw) + + row["operator"] = operator_uuid + return row + + def _floor_maps_index_path() -> str: return os.path.join(config_env.RESLEVIS_MAPS_DIR, "maps.json") @@ -626,7 +669,7 @@ async def getAlarms(): if not isinstance(payload, list): raise HTTPException(status_code=502, detail="Unexpected CORE response type") - return payload + return [_normalize_alarm_core(row) for row in payload if isinstance(row, dict)] @router.put( diff --git a/schemas/reslevis.py b/schemas/reslevis.py index 635bc6e..7417ea0 100644 --- a/schemas/reslevis.py +++ b/schemas/reslevis.py @@ -176,6 +176,8 @@ class AlarmCoreItem(BaseModel): status: str timestamp: str operator: Optional[UUID] = None + operatorName: Optional[str] = None + assignment_timestamp: Optional[str] = None resolution_timestamp: Optional[str] = None