From 5d54ea3f17fba9a8f7bde2324fcab3821710967c Mon Sep 17 00:00:00 2001 From: Sonny May Date: Sun, 15 Nov 2026 15:02:45 +0000 Subject: [PATCH] fix: store blank optional customer, device and ticket fields as NULL The UI submits unfilled optional inputs as empty strings. Only the id columns were normalised, so blank email/phone/company, device model/product_type and ticket description were persisted as '' instead of NULL. Co-Authored-By: Claude Sonnet 5.5 Claude-Session: https://claude.ai/code/session_0171zWzmL77J4orNAqemw1KG --- backend/main.py | 19 +++++++++++++------ backend/test_main.py | 21 +++++++++++++++++++++ 2 files changed, 34 insertions(+), 6 deletions(-) diff --git a/backend/main.py b/backend/main.py index a434e29..40f5f48 100644 --- a/backend/main.py +++ b/backend/main.py @@ -84,6 +84,13 @@ class RMA(BaseModel): resolution_status: str | None = "Pending" +# Optional fields the UI submits as "" when left blank; stored as NULL so +# blank values aren't persisted as empty strings. +CUSTOMER_OPTIONAL = ("email", "phone", "company") +DEVICE_OPTIONAL = ("model", "product_type", "customer_id") +TICKET_OPTIONAL = ("description", "customer_id", "device_id", "assigned_user_id") + + def clean_empty_strings(data: dict, *keys: str) -> dict: for key in keys: if data.get(key) == "": @@ -137,12 +144,12 @@ def get_customers(): @app.post("/customers") def create_customer(c: Customer): - return db_post("customers", model_data(c)) + return db_post("customers", clean_empty_strings(model_data(c), *CUSTOMER_OPTIONAL)) @app.put("/customers/{id}") def update_customer(id: str, c: Customer): - return db_patch("customers", id, model_data(c)) + return db_patch("customers", id, clean_empty_strings(model_data(c), *CUSTOMER_OPTIONAL)) @app.delete("/customers/{id}") @@ -158,12 +165,12 @@ def get_devices(): @app.post("/devices") def create_device(d: Device): - return db_post("devices", clean_empty_strings(model_data(d), "customer_id")) + return db_post("devices", clean_empty_strings(model_data(d), *DEVICE_OPTIONAL)) @app.put("/devices/{id}") def update_device(id: str, d: Device): - return db_patch("devices", id, clean_empty_strings(model_data(d), "customer_id")) + return db_patch("devices", id, clean_empty_strings(model_data(d), *DEVICE_OPTIONAL)) # --- Tickets --- @@ -227,13 +234,13 @@ def get_ticket(id: str): def create_ticket(t: Ticket): return db_post( "tickets", - clean_empty_strings(model_data(t), "customer_id", "device_id", "assigned_user_id"), + clean_empty_strings(model_data(t), *TICKET_OPTIONAL), ) @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") + data = clean_empty_strings(model_data(t), *TICKET_OPTIONAL) 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"]: diff --git a/backend/test_main.py b/backend/test_main.py index ebd5e80..6a393f9 100644 --- a/backend/test_main.py +++ b/backend/test_main.py @@ -559,3 +559,24 @@ def test_ai_suggest_maps_errors_to_http_responses(client, error, expected_status res = test_client.post("/ai/suggest", json={"ticket_id": "t1"}) assert res.status_code == expected_status + + +def test_blank_optional_fields_are_stored_as_null(client, monkeypatch): + test_client, main = client + calls = [] + monkeypatch.setattr(main, "db_post", lambda table, data: calls.append((table, data)) or data) + monkeypatch.setattr( + main, "db_patch", lambda table, id, data: calls.append((table, data)) or data + ) + monkeypatch.setattr(main, "db_get", lambda *a, **k: []) + + test_client.post("/customers", json={"name": "Acme", "email": "", "phone": "", "company": ""}) + test_client.put("/customers/c1", json={"name": "Acme", "email": "", "phone": "", "company": ""}) + test_client.post("/devices", json={"serial_number": "S1", "model": "", "product_type": ""}) + test_client.post("/tickets", json={"title": "T", "description": ""}) + + customer_post, customer_put, device, ticket = (c[1] for c in calls[:4]) + for row in (customer_post, customer_put): + assert (row["email"], row["phone"], row["company"]) == (None, None, None) + assert device["model"] is None and device["product_type"] is None + assert ticket["description"] is None