- Updated the case list endpoint to handle filtering and error logging more effectively. - Changed the template directory structure for better organization. - Enhanced the case detail view with improved error handling and customer information retrieval. - Redesigned the index.html template to include a more modern layout and responsive design using Bootstrap. - Implemented dark mode toggle functionality and improved search/filter capabilities in the frontend. - Removed unused code and optimized existing JavaScript for better performance.
112 lines
4.2 KiB
Python
112 lines
4.2 KiB
Python
import logging
|
|
from fastapi import APIRouter, HTTPException, Query
|
|
from fastapi.responses import HTMLResponse
|
|
from fastapi.templating import Jinja2Templates
|
|
from pathlib import Path
|
|
from app.core.database import execute_query
|
|
|
|
logger = logging.getLogger(__name__)
|
|
router = APIRouter()
|
|
|
|
# Setup template directory
|
|
template_dir = Path(__file__).parent.parent / "templates"
|
|
templates = Jinja2Templates(directory=str(template_dir))
|
|
|
|
@router.get("/sag", response_class=HTMLResponse)
|
|
async def sager_liste(
|
|
request,
|
|
status: str = Query(None),
|
|
tag: str = Query(None),
|
|
customer_id: int = Query(None),
|
|
):
|
|
"""Display list of all cases."""
|
|
try:
|
|
query = "SELECT * FROM sag_sager WHERE deleted_at IS NULL"
|
|
params = []
|
|
|
|
if status:
|
|
query += " AND status = %s"
|
|
params.append(status)
|
|
if customer_id:
|
|
query += " AND customer_id = %s"
|
|
params.append(customer_id)
|
|
|
|
query += " ORDER BY created_at DESC"
|
|
sager = execute_query(query, tuple(params))
|
|
|
|
# Filter by tag if provided
|
|
if tag and sager:
|
|
sag_ids = [s['id'] for s in sager]
|
|
tag_query = "SELECT sag_id FROM sag_tags WHERE tag_navn = %s AND deleted_at IS NULL"
|
|
tagged = execute_query(tag_query, (tag,))
|
|
tagged_ids = set(t['sag_id'] for t in tagged)
|
|
sager = [s for s in sager if s['id'] in tagged_ids]
|
|
|
|
# Fetch all distinct statuses and tags for filters
|
|
statuses = execute_query("SELECT DISTINCT status FROM sag_sager WHERE deleted_at IS NULL ORDER BY status", ())
|
|
all_tags = execute_query("SELECT DISTINCT tag_navn FROM sag_tags WHERE deleted_at IS NULL ORDER BY tag_navn", ())
|
|
|
|
return templates.TemplateResponse("index.html", {
|
|
"request": request,
|
|
"sager": sager,
|
|
"statuses": [s['status'] for s in statuses],
|
|
"all_tags": [t['tag_navn'] for t in all_tags],
|
|
"current_status": status,
|
|
"current_tag": tag,
|
|
})
|
|
except Exception as e:
|
|
logger.error("❌ Error displaying case list: %s", e)
|
|
raise HTTPException(status_code=500, detail="Failed to load case list")
|
|
|
|
@router.get("/sag/{sag_id}", response_class=HTMLResponse)
|
|
async def sag_detaljer(request, sag_id: int):
|
|
"""Display case details."""
|
|
try:
|
|
# Fetch main case
|
|
sag_query = "SELECT * FROM sag_sager WHERE id = %s AND deleted_at IS NULL"
|
|
sag_result = execute_query(sag_query, (sag_id,))
|
|
|
|
if not sag_result:
|
|
raise HTTPException(status_code=404, detail="Case not found")
|
|
|
|
sag = sag_result[0]
|
|
|
|
# Fetch tags
|
|
tags_query = "SELECT * FROM sag_tags WHERE sag_id = %s AND deleted_at IS NULL ORDER BY created_at DESC"
|
|
tags = execute_query(tags_query, (sag_id,))
|
|
|
|
# Fetch relations
|
|
relationer_query = """
|
|
SELECT sr.*,
|
|
ss_kilde.titel as kilde_titel,
|
|
ss_mål.titel as mål_titel
|
|
FROM sag_relationer sr
|
|
JOIN sag_sager ss_kilde ON sr.kilde_sag_id = ss_kilde.id
|
|
JOIN sag_sager ss_mål ON sr.målsag_id = ss_mål.id
|
|
WHERE (sr.kilde_sag_id = %s OR sr.målsag_id = %s)
|
|
AND sr.deleted_at IS NULL
|
|
ORDER BY sr.created_at DESC
|
|
"""
|
|
relationer = execute_query(relationer_query, (sag_id, sag_id))
|
|
|
|
# Fetch customer info if customer_id exists
|
|
customer = None
|
|
if sag.get('customer_id'):
|
|
customer_query = "SELECT * FROM customers WHERE id = %s"
|
|
customer_result = execute_query(customer_query, (sag['customer_id'],))
|
|
if customer_result:
|
|
customer = customer_result[0]
|
|
|
|
return templates.TemplateResponse("detail.html", {
|
|
"request": request,
|
|
"sag": sag,
|
|
"customer": customer,
|
|
"tags": tags,
|
|
"relationer": relationer,
|
|
})
|
|
except HTTPException:
|
|
raise
|
|
except Exception as e:
|
|
logger.error("❌ Error displaying case details: %s", e)
|
|
raise HTTPException(status_code=500, detail="Failed to load case details")
|