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
5 changes: 5 additions & 0 deletions backend/main.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from datetime import UTC, datetime

from fastapi import FastAPI, HTTPException, Query, Request, Response
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import JSONResponse
Expand Down Expand Up @@ -234,6 +236,9 @@ 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")
# schema.sql has no trigger for tickets.updated_at, so the column only ever
# held the creation time. Stamp it here on every edit.
data["updated_at"] = datetime.now(UTC).isoformat()
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
27 changes: 27 additions & 0 deletions backend/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -559,3 +559,30 @@ 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_update_ticket_sets_updated_at(client, monkeypatch):
from datetime import datetime

test_client, main = client
patches = []
monkeypatch.setattr(main, "db_get", lambda table, params="": [{"status": "Open"}])
monkeypatch.setattr(
main, "db_patch", lambda table, row_id, data: patches.append(data) or [data]
)

res = test_client.put("/tickets/t1", json={"title": "Printer down", "status": "Open"})

assert res.status_code == 200
stamp = datetime.fromisoformat(patches[0]["updated_at"])
assert stamp.tzinfo is not None


def test_create_ticket_does_not_send_updated_at(client, monkeypatch):
test_client, main = client
calls = []
monkeypatch.setattr(main, "db_post", lambda table, data: calls.append(data) or data)

test_client.post("/tickets", json={"title": "Printer down"})

assert "updated_at" not in calls[0]
Loading