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
19 changes: 13 additions & 6 deletions backend/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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) == "":
Expand Down Expand Up @@ -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}")
Expand All @@ -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 ---
Expand Down Expand Up @@ -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"]:
Expand Down
21 changes: 21 additions & 0 deletions backend/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Loading