From 3dfc5086c0c6a2e80975cc303ccd079a7f4f9d58 Mon Sep 17 00:00:00 2001 From: Christian Date: Sat, 6 Dec 2025 13:13:05 +0100 Subject: [PATCH] feat: Add global search functionality and redirect root to dashboard --- app/dashboard/backend/router.py | 60 +++++++++ app/dashboard/frontend/index.html | 20 ++- app/shared/frontend/base.html | 208 ++++++++++-------------------- main.py | 5 + 4 files changed, 152 insertions(+), 141 deletions(-) diff --git a/app/dashboard/backend/router.py b/app/dashboard/backend/router.py index 950e0b9..37c47a5 100644 --- a/app/dashboard/backend/router.py +++ b/app/dashboard/backend/router.py @@ -62,3 +62,63 @@ async def get_dashboard_stats(): except Exception as e: logger.error(f"❌ Error fetching dashboard stats: {e}", exc_info=True) raise HTTPException(status_code=500, detail=str(e)) + + +@router.get("/search", response_model=Dict[str, List[Any]]) +async def global_search(q: str): + """ + Global search across customers, contacts, and vendors + """ + if not q or len(q) < 2: + return {"customers": [], "contacts": [], "vendors": []} + + search_term = f"%{q}%" + + try: + # Search Customers + customers = execute_query(""" + SELECT id, name, email, 'Kunde' as type + FROM customers + WHERE deleted_at IS NULL AND ( + name ILIKE %s OR + email ILIKE %s OR + cvr_number ILIKE %s OR + phone ILIKE %s OR + mobile_phone ILIKE %s + ) + LIMIT 5 + """, (search_term, search_term, search_term, search_term, search_term)) + + # Search Contacts + contacts = execute_query(""" + SELECT id, first_name || ' ' || last_name as name, email, 'Kontakt' as type + FROM contacts + WHERE first_name ILIKE %s OR + last_name ILIKE %s OR + email ILIKE %s OR + phone ILIKE %s OR + mobile ILIKE %s + LIMIT 5 + """, (search_term, search_term, search_term, search_term, search_term)) + + # Search Vendors + vendors = execute_query(""" + SELECT id, name, email, 'Leverandør' as type + FROM vendors + WHERE is_active = true AND ( + name ILIKE %s OR + email ILIKE %s OR + cvr_number ILIKE %s OR + phone ILIKE %s + ) + LIMIT 5 + """, (search_term, search_term, search_term, search_term)) + + return { + "customers": customers or [], + "contacts": contacts or [], + "vendors": vendors or [] + } + except Exception as e: + logger.error(f"❌ Error performing global search: {e}", exc_info=True) + return {"customers": [], "contacts": [], "vendors": []} diff --git a/app/dashboard/frontend/index.html b/app/dashboard/frontend/index.html index 1e80c08..11edbdb 100644 --- a/app/dashboard/frontend/index.html +++ b/app/dashboard/frontend/index.html @@ -11,7 +11,7 @@
- +