fix: correzione doppia consuntivazione note. fix: utenti ldap. feat: ricerca per customer useers. feat: possiblità di rimuovere le note

This commit is contained in:
Gabriele Cimaschi
2026-07-08 12:52:30 +02:00
parent 3b06609fcb
commit 9a91c3d9f5
7 changed files with 661 additions and 72 deletions
+168 -23
View File
@@ -192,30 +192,89 @@ router.get('/lock-types', async (req, res) => {
router.get('/customer-companies/search', async (req, res) => {
try {
const { q = '' } = req.query;
let result;
if (!q) {
result = await pool.query(
`SELECT customer_id, name
FROM customer_company
WHERE valid_id = 1
ORDER BY name
LIMIT 20`
);
} else {
const searchTerm = `%${q}%`;
result = await pool.query(
`SELECT customer_id, name
FROM customer_company
WHERE valid_id = 1 AND (
customer_id ILIKE $1 OR
name ILIKE $1
)
ORDER BY name
LIMIT 20`,
[searchTerm]
);
// 1. Fetch from local SQLite LDAP cache
let localRows = [];
try {
if (!q) {
localRows = db.prepare(`
SELECT DISTINCT customer_id AS customer_id, customer_id AS name
FROM customer_user_cache
WHERE customer_id IS NOT NULL AND customer_id != ''
ORDER BY customer_id
LIMIT 500
`).all();
} else {
const searchTerm = `%${q}%`;
localRows = db.prepare(`
SELECT DISTINCT customer_id AS customer_id, customer_id AS name
FROM customer_user_cache
WHERE customer_id IS NOT NULL AND customer_id != '' AND customer_id LIKE ?
ORDER BY customer_id
LIMIT 500
`).all(searchTerm);
}
} catch (e) {
console.warn('Failed to query local customer cache:', e.message);
}
res.json(result.rows);
// 2. Fetch from OTRS Postgres DB
let dbRows = [];
try {
if (!q) {
const result = await pool.query(
`SELECT customer_id, name
FROM customer_company
WHERE valid_id = 1
ORDER BY name
LIMIT 500`
);
dbRows = result.rows;
} else {
const searchTerm = `%${q}%`;
const result = await pool.query(
`SELECT customer_id, name
FROM customer_company
WHERE valid_id = 1 AND (
customer_id ILIKE $1 OR
name ILIKE $1
)
ORDER BY name
LIMIT 500`,
[searchTerm]
);
dbRows = result.rows;
}
} catch (e) {
console.warn('Failed to query OTRS customer_company table:', e.message);
}
// 3. Merge results and remove duplicates by customer_id
const seen = new Set();
const merged = [];
// Prioritize OTRS database rows (which might have better names)
for (const row of dbRows) {
const cid = String(row.customer_id).trim();
if (cid && !seen.has(cid.toLowerCase())) {
seen.add(cid.toLowerCase());
merged.push({ customer_id: cid, name: row.name || cid });
}
}
// Add local LDAP rows
for (const row of localRows) {
const cid = String(row.customer_id).trim();
if (cid && !seen.has(cid.toLowerCase())) {
seen.add(cid.toLowerCase());
merged.push({ customer_id: cid, name: row.name || cid });
}
}
// Sort alphabetically by name
merged.sort((a, b) => a.name.localeCompare(b.name, 'it', { sensitivity: 'base' }));
res.json(merged.slice(0, 500));
} catch (err) {
console.error('Error searching customer companies:', err);
res.status(500).json({ error: err.message });
@@ -226,6 +285,92 @@ router.get('/customer-companies/search', async (req, res) => {
router.get('/customer-users/search', async (req, res) => {
const { q = '', customer_company_id } = req.query;
// If q is empty, we return a merged list for populating filter dropdowns
if (!q) {
let localRows = [];
try {
if (customer_company_id) {
localRows = db.prepare(`
SELECT login, email, first_name, last_name, customer_id
FROM customer_user_cache
WHERE customer_id = ?
ORDER BY last_name, first_name
LIMIT 1000
`).all(customer_company_id);
} else {
localRows = db.prepare(`
SELECT login, email, first_name, last_name, customer_id
FROM customer_user_cache
ORDER BY last_name, first_name
LIMIT 1000
`).all();
}
} catch (e) {
console.warn('Failed to query local customer user cache:', e.message);
}
let dbRows = [];
try {
let queryText = `
SELECT login, email, first_name, last_name, customer_id
FROM customer_user
WHERE valid_id = 1
`;
let queryParams = [];
if (customer_company_id) {
queryText += ` AND customer_id = $1`;
queryParams.push(customer_company_id);
}
queryText += ` ORDER BY last_name, first_name LIMIT 1000`;
const result = await pool.query(queryText, queryParams);
dbRows = result.rows;
} catch (e) {
console.warn('Failed to query OTRS customer_user table:', e.message);
}
// Merge and deduplicate by login
const seen = new Set();
const merged = [];
for (const row of dbRows) {
const login = String(row.login).trim();
if (login && !seen.has(login.toLowerCase())) {
seen.add(login.toLowerCase());
merged.push({
login,
email: row.email || '',
first_name: row.first_name || '',
last_name: row.last_name || '',
customer_id: row.customer_id || ''
});
}
}
for (const row of localRows) {
const login = String(row.login).trim();
if (login && !seen.has(login.toLowerCase())) {
seen.add(login.toLowerCase());
merged.push({
login,
email: row.email || '',
first_name: row.first_name || '',
last_name: row.last_name || '',
customer_id: row.customer_id || ''
});
}
}
// Sort alphabetically by last name, first name
merged.sort((a, b) => {
const nameA = `${a.last_name} ${a.first_name}`.trim();
const nameB = `${b.last_name} ${b.first_name}`.trim();
return nameA.localeCompare(nameB, 'it', { sensitivity: 'base' });
});
return res.json(merged.slice(0, 1000));
}
// 1. Try to search via OTRS GenericInterface REST API if configured
if (process.env.OTRS_API_URL && process.env.OTRS_API_USER) {
try {