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
+136 -55

No files matched your search

+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)
router.get('/customer-users/search', async (req, res) => {
const { q = '', customer_company_id } = req.query;
+73 -36
View File
@@ -387,7 +387,11 @@ router.get('/:id', async (req, res, next) => {
// 1. Try to read from local SQLite cache in internal.db
let cachedUser = null;
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) {
console.warn('[LDAP Cache Detail] SQLite cache read failed:', dbErr.message);
}
@@ -398,37 +402,50 @@ router.get('/:id', async (req, res, next) => {
ticket.customer_email = cachedUser.email || '';
ticket.customer_phone = cachedUser.phone || '';
} else {
// 2. Fallback to live API fetch
const getResult = await otrsRequest('POST', '/CustomerUserGet', { UserLogin: ticket.customer_user_id });
const getData = (getResult && getResult.Data) || getResult;
if (getData && getData.CustomerUser) {
const u = getData.CustomerUser;
ticket.customer_first = u.UserFirstname || '';
ticket.customer_last = u.UserLastname || '';
ticket.customer_email = u.UserEmail || '';
ticket.customer_phone = u.UserPhone || '';
// 3. Store in SQLite cache for subsequent detail queries
// 2. Fallback to live API fetch (only if login doesn't contain spaces)
let targetLogin = ticket.customer_user_id;
if (targetLogin.includes(' ')) {
try {
db.prepare(`
INSERT INTO customer_user_cache (login, email, first_name, last_name, customer_id, phone)
VALUES (?, ?, ?, ?, ?, ?)
ON CONFLICT(login) DO UPDATE SET
email=excluded.email,
first_name=excluded.first_name,
last_name=excluded.last_name,
customer_id=excluded.customer_id,
phone=excluded.phone
`).run(
u.UserLogin,
u.UserEmail || '',
u.UserFirstname || '',
u.UserLastname || '',
u.UserCustomerID || '',
u.UserPhone || ''
);
} catch (cacheErr) {
console.warn('[LDAP Cache Detail] Failed to write fetched user to SQLite cache:', cacheErr.message);
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;
if (getData && getData.CustomerUser) {
const u = getData.CustomerUser;
ticket.customer_first = u.UserFirstname || '';
ticket.customer_last = u.UserLastname || '';
ticket.customer_email = u.UserEmail || '';
ticket.customer_phone = u.UserPhone || '';
// 3. Store in SQLite cache for subsequent detail queries
try {
db.prepare(`
INSERT INTO customer_user_cache (login, email, first_name, last_name, customer_id, phone)
VALUES (?, ?, ?, ?, ?, ?)
ON CONFLICT(login) DO UPDATE SET
email=excluded.email,
first_name=excluded.first_name,
last_name=excluded.last_name,
customer_id=excluded.customer_id,
phone=excluded.phone
`).run(
u.UserLogin,
u.UserEmail || '',
u.UserFirstname || '',
u.UserLastname || '',
u.UserCustomerID || '',
u.UserPhone || ''
);
} catch (cacheErr) {
console.warn('[LDAP Cache Detail] Failed to write fetched user to SQLite cache:', cacheErr.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.type_id !== undefined) ticketFields.TypeID = updates.type_id;
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_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 (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.type_id !== undefined) ticketFields.TypeID = updates.type_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_id !== undefined) ticketFields.CustomerID = updates.customer_id;
if (updates.customer_user_id !== undefined) ticketFields.CustomerUser = updates.customer_user_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;
// Auto sblocco check
if (updates.ticket_state_id) {