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`;
+53 -1
View File
@@ -38,7 +38,8 @@ router.get('/', async (req, res) => {
const {
queue_id, state_id, priority_id, user_id, type_id,
search, sort_by = 'create_time', sort_dir = 'DESC',
page = 1, per_page = 50
page = 1, per_page = 50,
date_from, date_to
} = req.query;
const conditions = [];
@@ -65,6 +66,14 @@ router.get('/', async (req, res) => {
conditions.push(`t.type_id = $${paramIdx++}`);
params.push(parseInt(type_id));
}
if (date_from) {
conditions.push(`t.create_time >= $${paramIdx++}`);
params.push(new Date(date_from));
}
if (date_to) {
conditions.push(`t.create_time <= $${paramIdx++}`);
params.push(new Date(date_to));
}
if (search) {
conditions.push(`(t.title ILIKE $${paramIdx} OR t.tn ILIKE $${paramIdx})`);
params.push(`%${search}%`);
@@ -865,4 +874,47 @@ router.patch('/batch/update', async (req, res) => {
}
});
// POST /api/tickets/:id/retrodata-ticket — Retrodate ticket creation time
router.post('/:id/retrodata-ticket', async (req, res) => {
try {
const { id } = req.params;
const { create_time } = req.body;
if (!create_time) {
return res.status(400).json({ error: 'Specificare la data di creazione.' });
}
// Fetch TN for the ticket
const tnResult = await pool.query('SELECT tn FROM ticket WHERE id = $1', [id]);
if (tnResult.rows.length === 0) {
return res.status(404).json({ error: 'Ticket non trovato.' });
}
const tn = tnResult.rows[0].tn;
// Execute stored procedure
await pool.query('CALL prretrodataticket($1, $2)', [tn, new Date(create_time)]);
res.json({ message: 'Ticket retrodatato correttamente.' });
} catch (err) {
console.error('Errore in prretrodataticket:', err);
res.status(500).json({ error: err.message });
}
});
// POST /api/tickets/articles/:articleId/retrodata-article — Retrodate article creation time
router.post('/articles/:articleId/retrodata-article', async (req, res) => {
try {
const { articleId } = req.params;
const { create_time } = req.body;
if (!create_time) {
return res.status(400).json({ error: 'Specificare la data di creazione.' });
}
// Execute stored procedure
await pool.query('CALL prretrodataarticolo($1, $2)', [parseInt(articleId), new Date(create_time)]);
res.json({ message: 'Articolo retrodatato correttamente.' });
} catch (err) {
console.error('Errore in prretrodataarticolo:', err);
res.status(500).json({ error: err.message });
}
});
module.exports = router;