fix: correzione ricerca cliente tramite LDAP. fix: vista dettaglio che interrogava l'endpoint sbagliato. PRovocato probabilmente ancora dall'unificazione delle note rapide con le note normali.

This commit is contained in:
Gabriele Cimaschi committed 2026-09-23 17:33:17 +02:00
1 parent 0b6907969d
commit abb72d53c9
3 files changed
+104 -23

No files matched your search

+53 -15
View File
@@ -4201,36 +4201,53 @@ const TicketDetailView = {
customerSearchInput.addEventListener('input', () => { customerSearchInput.addEventListener('input', () => {
clearTimeout(customerDebounce); clearTimeout(customerDebounce);
const q = customerSearchInput.value.trim(); const q = customerSearchInput.value.trim();
const origCustFirst = (this.originalValues['customer_first'] || '').trim();
const origCustLast = (this.originalValues['customer_last'] || '').trim();
const origCustName = origCustFirst ? `${origCustFirst} ${origCustLast}`.trim() : (this.originalValues['customer_user_id'] || '').trim();
if (q !== origCustName) {
if (customerUserIdInput) customerUserIdInput.value = ''; if (customerUserIdInput) customerUserIdInput.value = '';
if (customerIdInput) customerIdInput.value = ''; if (customerIdInput) customerIdInput.value = '';
} else {
if (customerUserIdInput) customerUserIdInput.value = this.originalValues['customer_user_id'] || '';
if (customerIdInput) customerIdInput.value = this.originalValues['customer_id'] || '';
}
checkChanges(); checkChanges();
customerDebounce = setTimeout(async () => { customerDebounce = setTimeout(async () => {
try { try {
const customers = await App.api(`/api/customers/search?q=${encodeURIComponent(q)}`); const users = await App.api(`/api/customer-users/search?q=${encodeURIComponent(q)}`);
if (!customers || customers.length === 0) { if (!users || users.length === 0) {
customerSuggestionsDiv.innerHTML = '<div class="autocomplete-suggestion-item" style="color:var(--text-muted); cursor:default; padding: 6px 12px; font-size:0.78rem;">Nessun cliente trovato</div>'; customerSuggestionsDiv.innerHTML = '<div class="autocomplete-suggestion-item" style="color:var(--text-muted); cursor:default; padding: 6px 12px; font-size:0.78rem;">Nessun cliente trovato</div>';
customerSuggestionsDiv.style.display = 'block'; customerSuggestionsDiv.style.display = 'block';
return; return;
} }
customerSuggestionsDiv.innerHTML = customers.map(c => ` customerSuggestionsDiv.innerHTML = users.map(u => {
<div class="autocomplete-suggestion-item" data-user-id="${App.escapeHtml(c.customer_user_id)}" data-customer-id="${App.escapeHtml(c.customer_id || c.customer_user_id)}" data-name="${App.escapeHtml(c.first_name ? `${c.first_name} ${c.last_name}` : c.customer_user_id)}" style="padding: 6px 12px; font-size: 0.78rem; cursor: pointer;"> const fullName = (u.first_name || u.last_name) ? `${u.first_name || ''} ${u.last_name || ''}`.trim() : (u.login || '');
<strong>${App.escapeHtml(c.first_name ? `${c.first_name} ${c.last_name}` : c.customer_user_id)}</strong> return `
${c.email ? `<span style="color:var(--text-muted); margin-left:6px;">(${App.escapeHtml(c.email)})</span>` : ''} <div class="autocomplete-suggestion-item" data-login="${App.escapeHtml(u.login || '')}" data-customer-id="${App.escapeHtml(u.customer_id || '')}" data-name="${App.escapeHtml(fullName)}" style="padding: 6px 12px; font-size: 0.78rem; cursor: pointer;">
<strong>${App.escapeHtml(fullName)}</strong>
<span style="color:var(--text-muted); margin-left:6px; font-size:0.75rem;">(Login: ${App.escapeHtml(u.login || '')}${u.customer_id ? ` | ${App.escapeHtml(u.customer_id)}` : ''})</span>
${u.email ? `<div style="color:var(--text-secondary); font-size:0.73rem;">${App.escapeHtml(u.email)}</div>` : ''}
</div> </div>
`).join(''); `;
}).join('');
customerSuggestionsDiv.style.display = 'block'; customerSuggestionsDiv.style.display = 'block';
customerSuggestionsDiv.querySelectorAll('.autocomplete-suggestion-item').forEach(item => { const selectCustomerItem = (item) => {
if (item.dataset.userId) {
item.addEventListener('mousedown', (e) => {
e.preventDefault();
customerSearchInput.value = item.dataset.name; customerSearchInput.value = item.dataset.name;
if (customerUserIdInput) customerUserIdInput.value = item.dataset.userId; if (customerUserIdInput) customerUserIdInput.value = item.dataset.login;
if (customerIdInput) customerIdInput.value = item.dataset.customerId; if (customerIdInput) customerIdInput.value = item.dataset.customerId;
customerSuggestionsDiv.style.display = 'none'; customerSuggestionsDiv.style.display = 'none';
checkChanges(); checkChanges();
};
customerSuggestionsDiv.querySelectorAll('.autocomplete-suggestion-item').forEach(item => {
if (item.dataset.login) {
item.addEventListener('mousedown', (e) => {
e.preventDefault();
selectCustomerItem(item);
}); });
} }
}); });
@@ -4240,6 +4257,18 @@ const TicketDetailView = {
}, 150); }, 150);
}); });
if (App.setupAutocompleteKeyboardNavigation) {
App.setupAutocompleteKeyboardNavigation(customerSearchInput, customerSuggestionsDiv, (item) => {
if (item && item.dataset.login) {
customerSearchInput.value = item.dataset.name;
if (customerUserIdInput) customerUserIdInput.value = item.dataset.login;
if (customerIdInput) customerIdInput.value = item.dataset.customerId;
customerSuggestionsDiv.style.display = 'none';
checkChanges();
}
});
}
customerSearchInput.addEventListener('focus', () => { customerSearchInput.addEventListener('focus', () => {
customerSearchInput.dispatchEvent(new Event('input')); customerSearchInput.dispatchEvent(new Event('input'));
}); });
@@ -4270,9 +4299,18 @@ const TicketDetailView = {
} }
const customerSearch = customerSearchInput ? customerSearchInput.value.trim() : ''; const customerSearch = customerSearchInput ? customerSearchInput.value.trim() : '';
if (customerSearch && customerUserIdInput && !customerUserIdInput.value) { const origCustFirst = (this.originalValues['customer_first'] || '').trim();
customerUserIdInput.value = customerSearch; const origCustLast = (this.originalValues['customer_last'] || '').trim();
if (customerIdInput) customerIdInput.value = customerSearch; const origCustName = origCustFirst ? `${origCustFirst} ${origCustLast}`.trim() : (this.originalValues['customer_user_id'] || '').trim();
if (customerSearchInput) {
if (!customerSearch) {
if (customerUserIdInput) customerUserIdInput.value = '';
if (customerIdInput) customerIdInput.value = '';
} else if (customerSearch === origCustName) {
if (customerUserIdInput) customerUserIdInput.value = this.originalValues['customer_user_id'] || '';
if (customerIdInput) customerIdInput.value = this.originalValues['customer_id'] || '';
}
} }
// Check metadata updates // Check metadata updates
+6
View File
@@ -296,6 +296,12 @@ router.get('/customer-companies/search', async (req, res) => {
} }
}); });
// Alias /api/customers/search -> /api/customer-users/search for backwards compatibility
router.get('/customers/search', (req, res, next) => {
req.url = '/customer-users/search' + (req.url.includes('?') ? req.url.slice(req.url.indexOf('?')) : '');
router.handle(req, res, next);
});
// GET /api/customer-users/search — Search customer users (supports LDAP via REST, fallbacks to DB) // GET /api/customer-users/search — Search customer users (supports LDAP via REST, fallbacks to DB)
router.get('/customer-users/search', async (req, res) => { router.get('/customer-users/search', async (req, res) => {
const { q = '', customer_company_id } = req.query; const { q = '', customer_company_id } = req.query;
+45 -8
View File
@@ -387,7 +387,11 @@ router.get('/:id', async (req, res, next) => {
// 1. Try to read from local SQLite cache in internal.db // 1. Try to read from local SQLite cache in internal.db
let cachedUser = null; let cachedUser = null;
try { try {
cachedUser = db.prepare('SELECT email, first_name, last_name, phone FROM customer_user_cache WHERE login = ?').get(ticket.customer_user_id); cachedUser = db.prepare(`
SELECT login, email, first_name, last_name, phone
FROM customer_user_cache
WHERE login = ? OR (first_name || ' ' || last_name) = ? COLLATE NOCASE
`).get(ticket.customer_user_id, ticket.customer_user_id);
} catch (dbErr) { } catch (dbErr) {
console.warn('[LDAP Cache Detail] SQLite cache read failed:', dbErr.message); console.warn('[LDAP Cache Detail] SQLite cache read failed:', dbErr.message);
} }
@@ -398,8 +402,20 @@ router.get('/:id', async (req, res, next) => {
ticket.customer_email = cachedUser.email || ''; ticket.customer_email = cachedUser.email || '';
ticket.customer_phone = cachedUser.phone || ''; ticket.customer_phone = cachedUser.phone || '';
} else { } else {
// 2. Fallback to live API fetch // 2. Fallback to live API fetch (only if login doesn't contain spaces)
const getResult = await otrsRequest('POST', '/CustomerUserGet', { UserLogin: ticket.customer_user_id }); let targetLogin = ticket.customer_user_id;
if (targetLogin.includes(' ')) {
try {
const sRes = await otrsRequest('POST', '/CustomerUserSearch', { Search: `*${targetLogin}*`, Valid: 1 });
const sData = (sRes && sRes.Data) || sRes;
if (sData && sData.CustomerUserID) {
targetLogin = Array.isArray(sData.CustomerUserID) ? sData.CustomerUserID[0] : sData.CustomerUserID;
}
} catch (_) {}
}
if (targetLogin && !targetLogin.includes(' ')) {
const getResult = await otrsRequest('POST', '/CustomerUserGet', { UserLogin: targetLogin });
const getData = (getResult && getResult.Data) || getResult; const getData = (getResult && getResult.Data) || getResult;
if (getData && getData.CustomerUser) { if (getData && getData.CustomerUser) {
const u = getData.CustomerUser; const u = getData.CustomerUser;
@@ -432,6 +448,7 @@ router.get('/:id', async (req, res, next) => {
} }
} }
} }
}
} catch (apiErr) { } catch (apiErr) {
console.warn(`Failed to fetch LDAP customer details for detail view:`, apiErr.message); console.warn(`Failed to fetch LDAP customer details for detail view:`, apiErr.message);
} }
@@ -940,9 +957,19 @@ router.patch('/:id', async (req, res) => {
if (updates.responsible_user_id !== undefined) ticketFields.ResponsibleID = updates.responsible_user_id; if (updates.responsible_user_id !== undefined) ticketFields.ResponsibleID = updates.responsible_user_id;
if (updates.type_id !== undefined) ticketFields.TypeID = updates.type_id; if (updates.type_id !== undefined) ticketFields.TypeID = updates.type_id;
if (updates.title !== undefined) ticketFields.Title = updates.title; if (updates.title !== undefined) ticketFields.Title = updates.title;
if (updates.ticket_lock_id !== undefined) ticketFields.LockID = updates.ticket_lock_id; if (updates.customer_user_id !== undefined && updates.customer_user_id !== null && updates.customer_user_id !== '') {
try {
const match = db.prepare(`SELECT login, customer_id FROM customer_user_cache WHERE login = ? OR (first_name || ' ' || last_name) = ? COLLATE NOCASE`).get(updates.customer_user_id, updates.customer_user_id);
if (match) {
updates.customer_user_id = match.login;
if (!updates.customer_id && match.customer_id) {
updates.customer_id = match.customer_id;
}
}
} catch (e) {}
ticketFields.CustomerUser = updates.customer_user_id;
}
if (updates.customer_id !== undefined && updates.customer_id !== null && updates.customer_id !== '') ticketFields.CustomerID = updates.customer_id; if (updates.customer_id !== undefined && updates.customer_id !== null && updates.customer_id !== '') ticketFields.CustomerID = updates.customer_id;
if (updates.customer_user_id !== undefined && updates.customer_user_id !== null && updates.customer_user_id !== '') ticketFields.CustomerUser = updates.customer_user_id;
// If ticketFields is empty but we have a note or timeUnit to log, supply a dummy field (StateID) so Ticket is not empty // If ticketFields is empty but we have a note or timeUnit to log, supply a dummy field (StateID) so Ticket is not empty
if (Object.keys(ticketFields).length === 0 && (hasNote || (!isNaN(timeUnit) && timeUnit > 0))) { if (Object.keys(ticketFields).length === 0 && (hasNote || (!isNaN(timeUnit) && timeUnit > 0))) {
@@ -1673,9 +1700,19 @@ router.patch('/batch/update', async (req, res) => {
if (updates.queue_id !== undefined) ticketFields.QueueID = updates.queue_id; if (updates.queue_id !== undefined) ticketFields.QueueID = updates.queue_id;
if (updates.type_id !== undefined) ticketFields.TypeID = updates.type_id; if (updates.type_id !== undefined) ticketFields.TypeID = updates.type_id;
if (updates.user_id !== undefined) ticketFields.OwnerID = updates.user_id; if (updates.user_id !== undefined) ticketFields.OwnerID = updates.user_id;
if (updates.responsible_user_id !== undefined) ticketFields.ResponsibleID = updates.responsible_user_id; if (updates.customer_user_id !== undefined && updates.customer_user_id !== null && updates.customer_user_id !== '') {
if (updates.customer_id !== undefined) ticketFields.CustomerID = updates.customer_id; try {
if (updates.customer_user_id !== undefined) ticketFields.CustomerUser = updates.customer_user_id; const match = db.prepare(`SELECT login, customer_id FROM customer_user_cache WHERE login = ? OR (first_name || ' ' || last_name) = ? COLLATE NOCASE`).get(updates.customer_user_id, updates.customer_user_id);
if (match) {
updates.customer_user_id = match.login;
if (!updates.customer_id && match.customer_id) {
updates.customer_id = match.customer_id;
}
}
} catch (e) {}
ticketFields.CustomerUser = updates.customer_user_id;
}
if (updates.customer_id !== undefined && updates.customer_id !== null && updates.customer_id !== '') ticketFields.CustomerID = updates.customer_id;
// Auto sblocco check // Auto sblocco check
if (updates.ticket_state_id) { if (updates.ticket_state_id) {