REFACTOR: Alterei a ordem das instruções no glpiModel e descomentei a função que executa o glpiService no app.js.
This commit is contained in:
parent
40d0b2f4c6
commit
9926aee962
28248
servicenow_requests.json
Normal file
28248
servicenow_requests.json
Normal file
File diff suppressed because it is too large
Load Diff
9418
servicenow_tickets.json
Normal file
9418
servicenow_tickets.json
Normal file
File diff suppressed because it is too large
Load Diff
@ -11,7 +11,7 @@ logInfo('Aplicação iniciada', {
|
|||||||
async function main() {
|
async function main() {
|
||||||
try {
|
try {
|
||||||
await snServices();
|
await snServices();
|
||||||
//await glpiServices();
|
await glpiServices();
|
||||||
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
logInfo('Erro na aplicação:', error);
|
logInfo('Erro na aplicação:', error);
|
||||||
|
|||||||
@ -44,43 +44,42 @@ class TicketGlpiModel {
|
|||||||
// src/models/ticketGlpiModel.js
|
// src/models/ticketGlpiModel.js
|
||||||
|
|
||||||
static async createTicket(ticketData) {
|
static async createTicket(ticketData) {
|
||||||
const connection = await glpiPool.getConnection();
|
const connection = await glpiPool.getConnection();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await connection.beginTransaction();
|
await connection.beginTransaction();
|
||||||
|
|
||||||
// 1. Preparar dados para o GLPI (usando valores fixos para teste)
|
// 1. Preparar dados para o GLPI (usando valores fixos para teste)
|
||||||
const glpiTicketData = {
|
const glpiTicketData = {
|
||||||
entities_id: 1371, // Valor fixo para teste
|
entities_id: 1371, // Valor fixo para teste
|
||||||
name: ticketData.short_description || 'Sem título',
|
name: ticketData.short_description || 'Sem título',
|
||||||
date: ticketData.opened_at || new Date(),
|
date: ticketData.opened_at || new Date(),
|
||||||
date_mod: ticketData.updated_at || new Date(),
|
date_mod: ticketData.updated_at || new Date(),
|
||||||
status: TicketGlpiModel.mapStatus(ticketData.state || '1'),
|
status: TicketGlpiModel.mapStatus(ticketData.state || '1'),
|
||||||
users_id_recipient: 917, // Valor fixo para teste
|
users_id_recipient: 917, // Valor fixo para teste
|
||||||
requesttypes_id: 1,
|
requesttypes_id: 1,
|
||||||
content: ticketData.description || '',
|
content: ticketData.description || '',
|
||||||
urgency: 3, // Valor fixo
|
urgency: 3, // Valor fixo
|
||||||
impact: 3, // Valor fixo
|
impact: 3, // Valor fixo
|
||||||
priority: TicketGlpiModel.mapPriority(ticketData.priority || '3'),
|
priority: TicketGlpiModel.mapPriority(ticketData.priority || '3'),
|
||||||
type: 1, // Incidente
|
type: 1, // Incidente
|
||||||
itilcategories_id: TicketGlpiModel.mapCategory(ticketData.short_description),
|
itilcategories_id: TicketGlpiModel.mapCategory(ticketData.short_description),
|
||||||
global_validation: 1,
|
global_validation: 1,
|
||||||
date_creation: ticketData.opened_at || new Date()
|
date_creation: ticketData.opened_at || new Date()
|
||||||
};
|
};
|
||||||
|
|
||||||
await connection.execute(requesterQuery, [ticketId, 917]); // Default user
|
// Construir a query dinamicamente e inserir o ticket principal
|
||||||
// Construir a query manualmente
|
|
||||||
const fields = Object.keys(glpiTicketData).join(', ');
|
const fields = Object.keys(glpiTicketData).join(', ');
|
||||||
const placeholders = Object.keys(glpiTicketData).map(() => '?').join(', ');
|
const placeholders = Object.keys(glpiTicketData).map(() => '?').join(', ');
|
||||||
const values = Object.values(glpiTicketData);
|
const values = Object.values(glpiTicketData);
|
||||||
|
|
||||||
const ticketQuery = `INSERT INTO glpi_tickets (${fields}) VALUES (${placeholders})`;
|
const ticketQuery = `INSERT INTO glpi_tickets (${fields}) VALUES (${placeholders})`;
|
||||||
|
|
||||||
// Inserir ticket principal
|
// Inserir ticket principal e obter insertId
|
||||||
const [ticketResult] = await connection.execute(ticketQuery, values);
|
const [ticketResult] = await connection.execute(ticketQuery, values);
|
||||||
const ticketId = ticketResult.insertId;
|
const ticketId = ticketResult.insertId;
|
||||||
|
|
||||||
// Adicionar requerente
|
// Adicionar requerente (usa ticketId agora que foi criado)
|
||||||
await connection.execute(
|
await connection.execute(
|
||||||
`INSERT INTO glpi_tickets_users
|
`INSERT INTO glpi_tickets_users
|
||||||
(tickets_id, users_id, type, use_notification, alternative_email)
|
(tickets_id, users_id, type, use_notification, alternative_email)
|
||||||
@ -110,26 +109,26 @@ try {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Verificar se ticket já existe no GLPI
|
// Verificar se ticket já existe no GLPI
|
||||||
static async ticketExists(snTicketNumber) {
|
static async ticketExists(snTicketNumber) {
|
||||||
try {
|
try {
|
||||||
const query = `
|
const query = `
|
||||||
SELECT id FROM glpi_tickets
|
SELECT id FROM glpi_tickets
|
||||||
WHERE name LIKE ? OR content LIKE ?
|
WHERE name LIKE ? OR content LIKE ?
|
||||||
LIMIT 1
|
LIMIT 1
|
||||||
`;
|
`;
|
||||||
|
|
||||||
const [rows] = await glpiPool.execute(query, [
|
const [rows] = await glpiPool.execute(query, [
|
||||||
`%${snTicketNumber}%`,
|
`%${snTicketNumber}%`,
|
||||||
`%${snTicketNumber}%`
|
`%${snTicketNumber}%`
|
||||||
]);
|
]);
|
||||||
|
|
||||||
return rows.length > 0 ? rows[0].id : null;
|
return rows.length > 0 ? rows[0].id : null;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
logError(error, 'Erro ao verificar ticket existente');
|
logError(error, 'Erro ao verificar ticket existente');
|
||||||
return null;
|
return null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = TicketGlpiModel;
|
module.exports = TicketGlpiModel;
|
||||||
Loading…
Reference in New Issue
Block a user