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 = []