fix: show sanitized phone details in sag contact search results

This commit is contained in:
Christian 2026-05-05 07:14:51 +02:00
parent 1fe0611453
commit f6b78f93eb
4 changed files with 66 additions and 16 deletions

View File

@ -1 +1 @@
2.2.92
2.2.93

View File

@ -4766,11 +4766,35 @@
resultsDiv.innerHTML = '<div class="p-3 text-muted">Ingen kontakter fundet</div>';
} else {
resultsDiv.innerHTML = contacts.map(c => `
${(() => {
const clean = (v) => {
if (v === null || v === undefined) return '';
const s = String(v).trim();
if (!s || s.toLowerCase() === 'null' || s.toLowerCase() === 'none') return '';
return s;
};
const firstName = clean(c.first_name);
const lastName = clean(c.last_name);
const fullName = [firstName, lastName].filter(Boolean).join(' ') || `Kontakt #${c.id}`;
const email = clean(c.email);
const company = clean(c.user_company);
const phones = Array.from(new Set([
clean(c.mobile) ? `Mobil: ${clean(c.mobile)}` : '',
clean(c.phone) ? `Telefon: ${clean(c.phone)}` : '',
clean(c.user_company_phone) ? `Firma: ${clean(c.user_company_phone)}` : ''
].filter(Boolean)));
return `
<div class="list-group-item list-group-item-action" style="cursor: pointer;"
onclick="addContact(${caseId}, ${c.id}, '${(c.first_name + ' ' + c.last_name).replace(/'/g, "\\'")}')">
<strong>${c.first_name} ${c.last_name}</strong>
<div class="small text-muted">${c.email || ''} ${c.user_company ? '(' + c.user_company + ')' : ''}</div>
onclick="addContact(${caseId}, ${c.id}, '${fullName.replace(/'/g, "\\'")}')">
<strong>${fullName}</strong>
<div class="small text-muted">${[email, company ? '(' + company + ')' : ''].filter(Boolean).join(' ')}</div>
<div class="small mt-1">
${phones.join(' · ') || '<span class="text-muted">Intet telefonnummer</span>'}
</div>
</div>
`;
})()}
`).join('');
}
} catch (err) {

View File

@ -5263,17 +5263,35 @@
resultsDiv.innerHTML = '<div class="p-3 text-muted">Ingen kontakter fundet</div>';
} else {
resultsDiv.innerHTML = contacts.map(c => `
${(() => {
const clean = (v) => {
if (v === null || v === undefined) return '';
const s = String(v).trim();
if (!s || s.toLowerCase() === 'null' || s.toLowerCase() === 'none') return '';
return s;
};
const firstName = clean(c.first_name);
const lastName = clean(c.last_name);
const fullName = [firstName, lastName].filter(Boolean).join(' ') || `Kontakt #${c.id}`;
const email = clean(c.email);
const company = clean(c.user_company);
const phones = Array.from(new Set([
clean(c.mobile) ? `Mobil: ${clean(c.mobile)}` : '',
clean(c.phone) ? `Telefon: ${clean(c.phone)}` : '',
clean(c.user_company_phone) ? `Firma: ${clean(c.user_company_phone)}` : ''
].filter(Boolean)));
return `
<div class="list-group-item list-group-item-action" style="cursor: pointer;"
onclick="addContact(${caseId}, ${c.id}, '${(c.first_name + ' ' + c.last_name).replace(/'/g, "\\'")}')">
<strong>${c.first_name} ${c.last_name}</strong>
<div class="small text-muted">${c.email || ''} ${c.user_company ? '(' + c.user_company + ')' : ''}</div>
onclick="addContact(${caseId}, ${c.id}, '${fullName.replace(/'/g, "\\'")}')">
<strong>${fullName}</strong>
<div class="small text-muted">${[email, company ? '(' + company + ')' : ''].filter(Boolean).join(' ')}</div>
<div class="small mt-1">
${Array.from(new Set([
c.mobile ? `Mobil: ${c.mobile}` : '',
c.phone ? `Telefon: ${c.phone}` : ''
].filter(Boolean))).join(' · ') || '<span class="text-muted">Intet telefonnummer</span>'}
${phones.join(' · ') || '<span class="text-muted">Intet telefonnummer</span>'}
</div>
</div>
`;
})()}
`).join('');
}
} catch (err) {

View File

@ -26,7 +26,7 @@ async def search_customers(q: str = Query(..., min_length=2)):
async def search_contacts(q: str = Query(..., min_length=2)):
"""
Autocomplete search for contacts.
Returns list of {id, first_name, last_name, email, phone, mobile, user_company}
Returns list of {id, first_name, last_name, email, phone, mobile, user_company, user_company_phone}
Supports: first name, last name, email, combined "Fornavn Efternavn", phone, mobile.
"""
sql = """
@ -34,9 +34,9 @@ async def search_contacts(q: str = Query(..., min_length=2)):
c.id,
c.first_name,
c.last_name,
c.email,
c.phone,
c.mobile,
NULLIF(NULLIF(TRIM(c.email), ''), 'null') AS email,
NULLIF(NULLIF(TRIM(c.phone), ''), 'null') AS phone,
NULLIF(NULLIF(TRIM(c.mobile), ''), 'null') AS mobile,
(
SELECT cu.name
FROM contact_companies cc
@ -44,7 +44,15 @@ async def search_contacts(q: str = Query(..., min_length=2)):
WHERE cc.contact_id = c.id
ORDER BY cc.is_primary DESC NULLS LAST, cc.id ASC
LIMIT 1
) AS user_company
) AS user_company,
(
SELECT NULLIF(NULLIF(TRIM(COALESCE(cu.mobile_phone, cu.phone)), ''), 'null')
FROM contact_companies cc
JOIN customers cu ON cu.id = cc.customer_id
WHERE cc.contact_id = c.id
ORDER BY cc.is_primary DESC NULLS LAST, cc.id ASC
LIMIT 1
) AS user_company_phone
FROM contacts c
WHERE
c.first_name ILIKE %s