From cd1045ee70b8b1eca0bbf4a0c1a486352aa87b19 Mon Sep 17 00:00:00 2001 From: Sonny May Date: Thu, 26 Nov 2026 15:04:00 +0000 Subject: [PATCH] fix: stop ticket edits from clearing the assignee PUT /tickets/{id} dumped the full Ticket model, so any field the client omitted was filled with its default and written by the PATCH. The Tickets page edit form has no assignee input, so every edit from the UI silently set assigned_user_id to NULL. update_ticket now PATCHes only the fields present in the request (model_dump(exclude_unset=True)), and writes status history only when the request includes a status. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_0171zWzmk8ywmS3JeGFiZq3C --- backend/main.py | 9 +++++++-- backend/test_main.py | 45 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 2 deletions(-) diff --git a/backend/main.py b/backend/main.py index a434e29..cf71b3c 100644 --- a/backend/main.py +++ b/backend/main.py @@ -233,10 +233,15 @@ def create_ticket(t: Ticket): @app.put("/tickets/{id}") def update_ticket(id: str, t: Ticket): - data = clean_empty_strings(model_data(t), "customer_id", "device_id", "assigned_user_id") + # Only PATCH the fields the client actually sent. Dumping the full model would + # fill omitted fields with their defaults, so an edit from a form that has no + # assignee input (the Tickets page) would silently clear assigned_user_id. + data = clean_empty_strings( + t.model_dump(exclude_unset=True), "customer_id", "device_id", "assigned_user_id" + ) old = db_get("tickets", f"id=eq.{escape_filter_value(id)}&select=status") result = db_patch("tickets", id, data) - if old and old[0]["status"] != data["status"]: + if old and "status" in data and old[0]["status"] != data["status"]: db_post( "ticket_history", { diff --git a/backend/test_main.py b/backend/test_main.py index ebd5e80..31536a6 100644 --- a/backend/test_main.py +++ b/backend/test_main.py @@ -438,6 +438,51 @@ def fake_get(table, params=""): ) in posts +def test_update_ticket_only_patches_fields_sent_by_client(client, monkeypatch): + test_client, main = client + patches = [] + monkeypatch.setattr(main, "db_get", lambda table, params="": [{"status": "Open"}]) + monkeypatch.setattr(main, "db_post", lambda table, data: data) + monkeypatch.setattr( + main, "db_patch", lambda table, row_id, data: patches.append(data) or [data] + ) + + # Mirrors the Tickets page edit form, which has no assignee field. + res = test_client.put( + "/tickets/t1", + json={ + "title": "Printer down", + "description": "", + "status": "Open", + "priority": "High", + "customer_id": "", + "device_id": "", + }, + ) + + assert res.status_code == 200 + assert "assigned_user_id" not in patches[0] + assert patches[0]["customer_id"] is None + assert patches[0]["device_id"] is None + + +def test_update_ticket_without_status_skips_history(client, monkeypatch): + test_client, main = client + posts = [] + patches = [] + monkeypatch.setattr(main, "db_get", lambda table, params="": [{"status": "Open"}]) + monkeypatch.setattr(main, "db_post", lambda table, data: posts.append((table, data))) + monkeypatch.setattr( + main, "db_patch", lambda table, row_id, data: patches.append(data) or [data] + ) + + res = test_client.put("/tickets/t1", json={"title": "Renamed"}) + + assert res.status_code == 200 + assert patches == [{"title": "Renamed"}] + assert posts == [] + + def test_ticket_routes_convert_blank_relationship_ids_to_null(client, monkeypatch): test_client, main = client calls = []