diff --git a/app/contacts/backend/router.py b/app/contacts/backend/router.py index faab68a..14909a9 100644 --- a/app/contacts/backend/router.py +++ b/app/contacts/backend/router.py @@ -88,8 +88,26 @@ async def get_contacts( params = [] if search: - where_clauses.append("(c.first_name ILIKE %s OR c.last_name ILIKE %s OR c.email ILIKE %s)") - params.extend([f"%{search}%", f"%{search}%", f"%{search}%"]) + where_clauses.append( + """ + ( + c.first_name ILIKE %s + OR c.last_name ILIKE %s + OR c.email ILIKE %s + OR c.phone ILIKE %s + OR c.mobile ILIKE %s + OR EXISTS ( + SELECT 1 + FROM contact_companies cc2 + JOIN customers cu2 ON cu2.id = cc2.customer_id + WHERE cc2.contact_id = c.id + AND cu2.name ILIKE %s + ) + ) + """ + ) + like = f"%{search}%" + params.extend([like, like, like, like, like, like]) if is_active is not None: where_clauses.append("c.is_active = %s") diff --git a/app/contacts/backend/router_simple.py b/app/contacts/backend/router_simple.py index 674ab22..5bcbb90 100644 --- a/app/contacts/backend/router_simple.py +++ b/app/contacts/backend/router_simple.py @@ -113,8 +113,27 @@ async def get_contacts( params = [] if search: - where_clauses.append("(c.first_name ILIKE %s OR c.last_name ILIKE %s OR c.email ILIKE %s)") - params.extend([f"%{search}%", f"%{search}%", f"%{search}%"]) + where_clauses.append( + """ + ( + c.first_name ILIKE %s + OR c.last_name ILIKE %s + OR c.email ILIKE %s + OR c.phone ILIKE %s + OR c.mobile ILIKE %s + OR c.user_company ILIKE %s + OR EXISTS ( + SELECT 1 + FROM contact_companies cc2 + JOIN customers cu2 ON cu2.id = cc2.customer_id + WHERE cc2.contact_id = c.id + AND cu2.name ILIKE %s + ) + ) + """ + ) + like = f"%{search}%" + params.extend([like, like, like, like, like, like, like]) if is_active is not None: where_clauses.append("c.is_active = %s") diff --git a/app/contacts/frontend/contacts.html b/app/contacts/frontend/contacts.html index b3464cf..5ef9f39 100644 --- a/app/contacts/frontend/contacts.html +++ b/app/contacts/frontend/contacts.html @@ -953,7 +953,7 @@ async function loadContacts() { totalContacts = data.total; currentContactsData = Array.isArray(data.contacts) ? data.contacts : []; displayContacts(currentContactsData); - updatePagination(data.total); + updatePagination(data.total, currentContactsData.length); } catch (error) { if (error.name === 'AbortError') { @@ -1182,11 +1182,12 @@ function persistTablePreferences() { } } -function updatePagination(total) { - const start = currentPage * pageSize + 1; - const end = Math.min((currentPage + 1) * pageSize, total); +function updatePagination(total, rowsOnPage = 0) { + const safeRowsOnPage = Number.isFinite(Number(rowsOnPage)) ? Math.max(0, Number(rowsOnPage)) : 0; + const start = total > 0 ? (currentPage * pageSize + 1) : 0; + const end = total > 0 ? Math.min(currentPage * pageSize + safeRowsOnPage, total) : 0; - document.getElementById('showingStart').textContent = total > 0 ? start : 0; + document.getElementById('showingStart').textContent = start; document.getElementById('showingEnd').textContent = end; document.getElementById('totalCount').textContent = total; diff --git a/app/services/email_workflow_service.py b/app/services/email_workflow_service.py index 3270694..1a0fcfb 100644 --- a/app/services/email_workflow_service.py +++ b/app/services/email_workflow_service.py @@ -1156,11 +1156,13 @@ class EmailWorkflowService: if sag_id_from_tag: if sag_id and sag_id != sag_id_from_tag: logger.warning( - "⚠️ Email %s contains conflicting case hints (thread: SAG-%s, tag: SAG-%s). Using thread match.", + "⚠️ Email %s contains conflicting case hints (thread: SAG-%s, tag: SAG-%s). Using SAG tag.", email_id, sag_id, sag_id_from_tag ) + sag_id = sag_id_from_tag + routing_source = 'sag_tag' elif not sag_id: sag_id = sag_id_from_tag routing_source = 'sag_tag'