# WebStatus.py import PersistentData import CockpitITSM def init_status_interface(ui): """ Registers the bridge between Status.html and Python logic. """ def handle_get_ticket_statuses(_, __): """Syncs with API then returns ticket statuses from DB, sorted by subType / reference.""" success = CockpitITSM.update_ticket_status() if not success: ui.send_message("status_response", { "success": False, "message": "Failed to refresh ticket statuses. Check API configuration." }) return statuses = PersistentData.get_ticket_statuses() ui.send_message("status_data", {"statuses": statuses}) def handle_save_ticket_statuses(_, data): """Persists the selected state for each ticket status.""" try: statuses = data.get("statuses", []) for s in statuses: PersistentData.db.update( "objects", {"selected": s["selected"]}, condition=f"id = {s['id']} AND subType = '{s['subType']}'" ) print(f"DEBUG: Saved selection for {len(statuses)} ticket statuses.") ui.send_message("status_response", { "success": True, "message": f"Selection saved ({len(statuses)} statuses)." }) except Exception as e: print(f"ERROR saving ticket statuses: {str(e)}") ui.send_message("status_response", { "success": False, "message": f"Save error: {str(e)}" }) def handle_resolve_status_reference(_, data): """ Fetches a ticket by ID and updates the status reference in DB for the matching (status_id, sub_type) entry. """ ticket_id = data.get("ticket_id") sub_type = data.get("sub_type", "") if not ticket_id: ui.send_message("status_resolve_response", { "success": False, "message": "No ticket ID provided.", "sub_type": sub_type }) return success, result = CockpitITSM.resolve_status_from_ticket(ticket_id, sub_type) if not success: ui.send_message("status_resolve_response", { "success": False, "message": result, "sub_type": sub_type }) return status_id = result["status_id"] status_ref = result["status_reference"] obj_type = "TICKET_STATUS" cond = f"id = {status_id} AND type = '{obj_type}' AND subType = '{sub_type}'" existing = PersistentData.db.read("objects", condition=cond) if existing: PersistentData.db.update("objects", {"reference": status_ref}, condition=cond) else: PersistentData.db.store("objects", { "id": status_id, "module": "TICKETING", "type": obj_type, "subType": sub_type, "reference": status_ref, "selected": 0, "manual": 1 }) print(f"DEBUG: Resolved status for {sub_type} #{status_id} → '{status_ref}'") ui.send_message("status_resolve_response", { "success": True, "message": f"Resolved: {sub_type} / {status_ref}", "sub_type": sub_type, "status_id": status_id, "reference": status_ref }) # Refresh the list from DB only — no API call to avoid overwriting resolved entries statuses = PersistentData.get_ticket_statuses() ui.send_message("status_data", {"statuses": statuses}) # Registering the handlers ui.on_message("get_ticket_statuses", handle_get_ticket_statuses) ui.on_message("save_ticket_statuses", handle_save_ticket_statuses) ui.on_message("resolve_status_reference", handle_resolve_status_reference)