Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions backend/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
{
Expand Down
45 changes: 45 additions & 0 deletions backend/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = []
Expand Down