Aggiunte stored procedure più utili le altre pfff

This commit is contained in:
2026-07-05 18:48:29 +02:00
parent 457c3eacf6
commit 4f41b50d37
10 changed files with 1578 additions and 156 deletions
+32 -21
View File
@@ -153,10 +153,7 @@ router.get('/customer-companies/search', async (req, res) => {
// GET /api/customer-users/search — Search customer users
router.get('/customer-users/search', async (req, res) => {
try {
const { q, customer_company_id } = req.query;
if (!q) {
return res.json([]);
}
const { q = '', customer_company_id } = req.query;
// Try OTRS API first if configured
const OTRS_API_URL = process.env.OTRS_API_URL;
@@ -164,7 +161,7 @@ router.get('/customer-users/search', async (req, res) => {
if (OTRS_API_URL && OTRS_API_USER) {
try {
const searchParams = {
Search: `*${q}*`,
Search: q ? `*${q}*` : '*',
Valid: 1
};
if (customer_company_id) {
@@ -217,22 +214,36 @@ router.get('/customer-users/search', async (req, res) => {
}
// Fallback: local DB query
const searchTerm = `%${q}%`;
let queryText = `
SELECT login, email, first_name, last_name, customer_id
FROM customer_user
WHERE valid_id = 1 AND (
login ILIKE $1 OR
email ILIKE $1 OR
first_name ILIKE $1 OR
last_name ILIKE $1
)
`;
const queryParams = [searchTerm];
if (customer_company_id) {
queryText += ` AND customer_id = $2`;
queryParams.push(customer_company_id);
let queryText;
let queryParams;
if (q) {
const searchTerm = `%${q}%`;
queryText = `
SELECT login, email, first_name, last_name, customer_id
FROM customer_user
WHERE valid_id = 1 AND (
login ILIKE $1 OR
email ILIKE $1 OR
first_name ILIKE $1 OR
last_name ILIKE $1
)
`;
queryParams = [searchTerm];
if (customer_company_id) {
queryText += ` AND customer_id = $2`;
queryParams.push(customer_company_id);
}
} else {
queryText = `
SELECT login, email, first_name, last_name, customer_id
FROM customer_user
WHERE valid_id = 1
`;
queryParams = [];
if (customer_company_id) {
queryText += ` AND customer_id = $1`;
queryParams.push(customer_company_id);
}
}
queryText += ` ORDER BY last_name, first_name LIMIT 20`;