diff --git a/src/models/ticketSnModel.js b/src/models/ticketSnModel.js index 022dd94..99da7cc 100644 --- a/src/models/ticketSnModel.js +++ b/src/models/ticketSnModel.js @@ -4,33 +4,38 @@ const { logInfo, logError, logWarning } = require('../utils/logger'); class TicketSnModel { static async saveTicket(ticketData, typeTicket) { - const query = ` + const query = ` INSERT INTO tickets_sn - (ticket_number, short_description, description, caller_email, - location_name, ramal, telefone, opened_at, tipo) - VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9) + (ticket_number, short_description, description, caller_id, caller_email, + location_id, location_name, ramal, telefone, opened_at, tipo) + VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11) ON CONFLICT (ticket_number) DO UPDATE SET short_description = EXCLUDED.short_description, description = EXCLUDED.description, + location_id = EXCLUDED.location_id, location_name = EXCLUDED.location_name, + caller_id = EXCLUDED.caller_id, caller_email = EXCLUDED.caller_email, ramal = EXCLUDED.ramal, telefone = EXCLUDED.telefone, updated_at = NOW() - RETURNING id, xmax = 0 as inserted -- xmax = 0 indica INSERT, >0 indica UPDATE`; + RETURNING id, xmax = 0 as inserted`; const values = [ ticketData.number?.value || null, ticketData.short_description?.value || null, ticketData.description?.value || null, - ticketData['opened_by.email']?.value || null, - ticketData.location?.display_value || null, + ticketData.opened_by?.display_value || null, + ticketData['opened_by.email']?.value || null, + ticketData.location?.value || null, // location_id (value) + ticketData.location?.display_value || null, // location_name (display_value) ticketData['variables.numero_ramal']?.value || null, ticketData['variables.telephone_favorecido_rh']?.value || null, ticketData.opened_at?.value ? new Date(ticketData.opened_at.value) : null, typeTicket ]; + try { const result = await pool.query(query, values); diff --git a/src/scripts/database/01_create_tables.sql b/src/scripts/database/01_create_tables.sql index 5c79e31..df40620 100644 --- a/src/scripts/database/01_create_tables.sql +++ b/src/scripts/database/01_create_tables.sql @@ -1,11 +1,10 @@ CREATE TABLE tickets_sn ( id SERIAL PRIMARY KEY, -- ID automático (1, 2, 3...) ticket_number VARCHAR(32) NOT NULL UNIQUE, -- Número do ticket (INC0140378) - ÚNICO - sn_ticket_id VARCHAR(64), -- ID interno do ServiceNow short_description TEXT, -- Resumo do problema tipo VARCHAR(10), -- Requisição ou incidente description TEXT, -- Descrição completa - caller_id VARCHAR(255), -- ID de quem abriu o ticket + caller_id VARCHAR(255), -- Nome de quem abriu o ticket caller_email VARCHAR(255), -- Email do solicitante location_id VARCHAR(64), -- ID da localização location_name VARCHAR(255), -- Nome da localização @@ -16,3 +15,6 @@ CREATE TABLE tickets_sn ( updated_at TIMESTAMP DEFAULT NOW() -- Quando atualizamos pela última vez ); + + +SELECT * FROM tickets_sn; diff --git a/src/services/servicenowService.js b/src/services/servicenowService.js index d9ff417..2526531 100644 --- a/src/services/servicenowService.js +++ b/src/services/servicenowService.js @@ -13,7 +13,7 @@ const fetchTicketsFromServiceNow = async () => { }); // comentado para evitar muitos arquivos - //fs.writeFileSync('servicenow_tickets.json', JSON.stringify(ticketsResponse.data, null, 2)); + fs.writeFileSync('servicenow_tickets.json', JSON.stringify(ticketsResponse.data, null, 2)); //logInfo('💾 Backup JSON de incidentes salvo', { count: ticketsResponse.data.result.length }); // Salvar os tickets no banco de dados @@ -38,7 +38,7 @@ const fetchRequestsFromServiceNow = async () => { }); // Comentado para evitar muitos arquivos - //fs.writeFileSync('servicenow_requests.json', JSON.stringify(requestsResponse.data, null, 2)); + fs.writeFileSync('servicenow_requests.json', JSON.stringify(requestsResponse.data, null, 2)); //logInfo('💾 Backup JSON de requisições salvo', { count: requestsResponse.data.result.length }); // Salvar as requisições no banco de dados