feat(api,ui): enhance ticket data mapping and add test endpoint; improve error handling and logging

This commit is contained in:
2025-09-27 10:27:25 +08:00
parent 77d5be8fd1
commit 7cb7b68446
4 changed files with 87 additions and 44 deletions

Binary file not shown.

View File

@@ -82,16 +82,37 @@ def ticket_to_dict(ticket, request=None) -> dict:
logger.exception("Failed to build image_url")
image_url = None
# Map backend enum values to dashboard expected values
severity_mapping = {
"N/A": "low",
"Low": "low",
"Medium": "medium",
"High": "high"
}
status_mapping = {
"New": "submitted",
"In Progress": "in_progress",
"Fixed": "fixed"
}
# Map category to expected values
category_mapping = {
"Unknown": "other",
"garbage": "trash"
}
return {
"id": ticket.id,
"category": ticket.category,
"severity": ticket.severity.value if getattr(ticket, "severity", None) else None,
"status": ticket.status.value if getattr(ticket, "status", None) else None,
"description": ticket.description,
"category": category_mapping.get(ticket.category, ticket.category) if ticket.category else "other",
"severity": severity_mapping.get(ticket.severity.value, "low") if getattr(ticket, "severity", None) else "low",
"status": status_mapping.get(ticket.status.value, "submitted") if getattr(ticket, "status", None) else "submitted",
"notes": ticket.description, # Map description to notes
"user_id": ticket.user_id,
"user_name": ticket.user.name if getattr(ticket, "user", None) else None,
"userName": ticket.user.name if getattr(ticket, "user", None) else None,
"user_email": ticket.user.email if getattr(ticket, "user", None) else None,
"created_at": created,
"createdAt": created, # Map created_at to createdAt
"updatedAt": getattr(ticket, "updated_at", None).isoformat() if getattr(ticket, "updated_at", None) else created,
"latitude": ticket.latitude,
"longitude": ticket.longitude,
"address": ticket.address,

View File

@@ -48,17 +48,17 @@ app.mount("/static", StaticFiles(directory="static"), name="static")
# ----------------------
# CORS - allow dashboard & emulator origins
# ----------------------
DEFAULT_ORIGINS = "http://localhost:3000,http://127.0.0.1:3000,http://10.0.2.2:3000,http://192.168.100.59:3000"
DEFAULT_ORIGINS = "http://localhost:3000,http://127.0.0.1:3000,http://[::1]:3000,http://10.0.2.2:3000,http://192.168.100.59:3000"
origins_env = os.environ.get("FIXMATE_CORS_ORIGINS", DEFAULT_ORIGINS)
allowed_origins = [o.strip() for o in origins_env.split(",") if o.strip()]
# Ensure common development origins are always allowed (localhost, emulator, LAN)
for origin in ("http://localhost:3000", "http://127.0.0.1:3000", "http://10.0.2.2:3000", "http://192.168.100.59:3000"):
for origin in ("http://localhost:3000", "http://127.0.0.1:3000", "http://[::1]:3000", "http://10.0.2.2:3000", "http://192.168.100.59:3000"):
if origin not in allowed_origins:
allowed_origins.append(origin)
app.add_middleware(
CORSMiddleware,
allow_origins=allowed_origins,
allow_origins=["*"], # Allow all origins for development
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
@@ -82,6 +82,10 @@ except Exception as e:
def root():
return {"message": "Welcome to FixMate Backend API! Visit /docs for API documentation."}
@app.get("/test")
def test():
return {"status": "Backend is working", "timestamp": "2025-09-27T10:12:41"}
print("✅ FastAPI server setup complete")
# Start the server when running this script directly