fix: corretta modifica massiva scassata da claude. fix: e possibile inserire clienti non esistenti come in otrs.

This commit is contained in:
2026-07-07 08:15:29 +02:00
parent b3570d6757
commit 7b0b29e6c6
5 changed files with 161 additions and 10 deletions
+27 -5
View File
@@ -278,12 +278,34 @@ router.post('/', async (req, res) => {
await client.query('BEGIN');
// Generate ticket number: get next counter value for today (daily reset)
const now = new Date();
const systemId = process.env.OTRS_SYSTEM_ID || '10';
const datePrefix = `${now.getFullYear()}${String(now.getMonth() + 1).padStart(2, '0')}${String(now.getDate()).padStart(2, '0')}${systemId}`;
// 1. Fetch current maximum counter in ticket table for today
const maxTicketResult = await client.query(
`SELECT tn FROM ticket WHERE tn LIKE $1`,
[`${datePrefix}%`]
);
let maxCounterFromTicketTable = 0;
for (const row of maxTicketResult.rows) {
const counterPart = row.tn.substring(datePrefix.length);
const parsed = parseInt(counterPart, 10);
if (!isNaN(parsed) && parsed > maxCounterFromTicketTable) {
maxCounterFromTicketTable = parsed;
}
}
// 2. Fetch current maximum counter in ticket_number_counter table for today
const counterTodayResult = await client.query(
`SELECT COALESCE(MAX(counter), 0) AS max_counter
FROM ticket_number_counter
WHERE create_time >= CURRENT_DATE`
);
const nextCounter = parseInt(counterTodayResult.rows[0].max_counter, 10) + 1;
const maxCounterFromCounterTable = parseInt(counterTodayResult.rows[0].max_counter, 10);
// 3. Compute the next counter (absolute max + 1)
const nextCounter = Math.max(maxCounterFromTicketTable, maxCounterFromCounterTable) + 1;
const counterResult = await client.query(
`INSERT INTO ticket_number_counter (counter, counter_uid, create_time)
@@ -296,10 +318,8 @@ router.post('/', async (req, res) => {
[nextCounter]
);
const counter = counterResult.rows[0].counter;
const now = new Date();
const systemId = process.env.OTRS_SYSTEM_ID || '10';
const counterPadding = parseInt(process.env.OTRS_COUNTER_PADDING, 10) || 6;
const tn = `${now.getFullYear()}${String(now.getMonth() + 1).padStart(2, '0')}${String(now.getDate()).padStart(2, '0')}${systemId}${String(counter).padStart(counterPadding, '0')}`;
const tn = `${datePrefix}${String(counter).padStart(counterPadding, '0')}`;
// Determine lock type (1 = unlock by default)
const lockId = 1;
@@ -885,6 +905,8 @@ router.patch('/batch/update', async (req, res) => {
if (updates.ticket_priority_id !== undefined) ticketFields.PriorityID = updates.ticket_priority_id;
if (updates.queue_id !== undefined) ticketFields.QueueID = updates.queue_id;
if (updates.user_id !== undefined) ticketFields.OwnerID = updates.user_id;
if (updates.customer_id !== undefined) ticketFields.CustomerID = updates.customer_id;
if (updates.customer_user_id !== undefined) ticketFields.CustomerUser = updates.customer_user_id;
// Auto sblocco check
if (updates.ticket_state_id) {
@@ -926,7 +948,7 @@ router.patch('/batch/update', async (req, res) => {
try {
await client.query('BEGIN');
const allowedFields = ['ticket_state_id', 'ticket_priority_id', 'queue_id', 'user_id'];
const allowedFields = ['ticket_state_id', 'ticket_priority_id', 'queue_id', 'user_id', 'customer_id', 'customer_user_id'];
const setClauses = [];
const setParams = [];
let pIdx = 1;