2025-09-10 20:52:12 -03:00
|
|
|
const glpiPool = require('../data/glpiDataBase');
|
|
|
|
|
const { logInfo, logError } = require('../utils/logger');
|
2025-09-17 20:56:32 -03:00
|
|
|
const LocationMappingModel = require('./locationMappingModel');
|
2025-09-10 20:52:12 -03:00
|
|
|
|
|
|
|
|
class TicketGlpiModel {
|
2025-09-11 20:57:43 -03:00
|
|
|
|
2025-09-10 20:52:12 -03:00
|
|
|
static mapStatus(snState) {
|
|
|
|
|
const statusMap = {
|
2025-09-15 20:55:19 -03:00
|
|
|
'Aguardando Atendimento': 1,
|
|
|
|
|
'Em Atendimento': 2,
|
|
|
|
|
'Em Espera': 4,
|
2025-10-23 17:30:03 -03:00
|
|
|
'Resolvido': 5,
|
2025-10-24 13:13:08 -03:00
|
|
|
'Encerrado - Omitido': 6,
|
2025-10-29 14:20:21 -03:00
|
|
|
'Encerrado': 6
|
2025-09-10 20:52:12 -03:00
|
|
|
};
|
|
|
|
|
return statusMap[snState] || 1;
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-23 17:30:03 -03:00
|
|
|
static mapGlpiStatusToString(glpiStatus) {
|
|
|
|
|
const statusMap = {
|
|
|
|
|
1: 'Aguardando Atendimento', // Novo
|
|
|
|
|
2: 'Em Atendimento', // Em atendimento (atribuído)
|
|
|
|
|
4: 'Em Espera', // Pendente
|
|
|
|
|
5: 'Resolvido', // Solucionado
|
|
|
|
|
6: 'Encerrado - Omitido' // Fechado
|
|
|
|
|
};
|
|
|
|
|
return statusMap[glpiStatus] || 'Desconhecido';
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-10 20:52:12 -03:00
|
|
|
static mapPriority(snPriority) {
|
|
|
|
|
const priorityMap = {
|
2025-09-15 20:55:19 -03:00
|
|
|
'1': 5,
|
|
|
|
|
'2': 4,
|
|
|
|
|
'3': 3,
|
|
|
|
|
'4': 2,
|
|
|
|
|
'5': 1
|
2025-09-10 20:52:12 -03:00
|
|
|
};
|
|
|
|
|
return priorityMap[snPriority] || 3;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static mapCategory(shortDescription) {
|
|
|
|
|
const categoryMap = {
|
2025-09-15 20:55:19 -03:00
|
|
|
'Problemas com Telefonia Fixa': 5717,
|
|
|
|
|
'Problemas com Internet': 5707,
|
|
|
|
|
'Problemas com Wi-Fi/Rede Corporativa': 5707,
|
|
|
|
|
'Wi-fi Clientes': 5707
|
2025-09-10 20:52:12 -03:00
|
|
|
};
|
|
|
|
|
return categoryMap[shortDescription] || 5717;
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-15 20:55:19 -03:00
|
|
|
static mapSlasIdTtr(ticketData) {
|
|
|
|
|
const isRequisicao = ticketData.type === 'requisicao';
|
2025-09-17 20:56:32 -03:00
|
|
|
const shortDesc = ticketData.short_description;
|
|
|
|
|
const isTelefonia = shortDesc === 'Problemas com Telefonia Fixa' || shortDesc === 'Solicitações relacionadas a telefonia fixa';
|
|
|
|
|
const isLinkDedicado = shortDesc === 'Problemas com Internet';
|
|
|
|
|
const isWifi = shortDesc === 'Problemas com Wi-Fi/Rede Corporativa' || shortDesc === 'Wi-fi Clientes';
|
2025-09-10 20:52:12 -03:00
|
|
|
|
2025-09-15 20:55:19 -03:00
|
|
|
if (isRequisicao && isTelefonia) {
|
|
|
|
|
return 37;
|
|
|
|
|
}
|
|
|
|
|
if (isRequisicao && (isLinkDedicado || isWifi)) {
|
|
|
|
|
return 37;
|
|
|
|
|
}
|
|
|
|
|
if (!isRequisicao && (isWifi || isLinkDedicado)) {
|
|
|
|
|
return 34;
|
|
|
|
|
}
|
|
|
|
|
if (!isRequisicao && isTelefonia) {
|
|
|
|
|
return 34;
|
|
|
|
|
}
|
2025-10-29 14:20:21 -03:00
|
|
|
return 0;
|
2025-09-15 20:55:19 -03:00
|
|
|
}
|
|
|
|
|
|
2025-09-18 20:52:39 -03:00
|
|
|
static async formatTitle(ticketData) {
|
|
|
|
|
try {
|
|
|
|
|
let entityData = null;
|
|
|
|
|
|
|
|
|
|
if (ticketData.location_id) {
|
|
|
|
|
entityData = await LocationMappingModel.getGlpiEntity(ticketData.location_id);
|
|
|
|
|
} else if (ticketData.location_name) {
|
|
|
|
|
entityData = await LocationMappingModel.getGlpiEntityByName(ticketData.location_name);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const entityName = entityData?.name || 'Sem Entidade';
|
|
|
|
|
const tipo = ticketData.type === 'requisicao' ? 'REQUISIÇÃO' : 'INCIDENTE';
|
|
|
|
|
const tituloSn = ticketData.short_description || 'Sem título';
|
|
|
|
|
|
|
|
|
|
return {
|
2025-10-29 14:20:21 -03:00
|
|
|
title: `${tipo} - ${entityName} - ${tituloSn}`, // Título formatado
|
|
|
|
|
entityId: entityData?.id || 127
|
2025-09-18 20:52:39 -03:00
|
|
|
};
|
|
|
|
|
} catch (error) {
|
|
|
|
|
logError(error, 'Erro ao formatar título do ticket');
|
|
|
|
|
const tipo = ticketData.type === 'requisicao' ? 'REQUISIÇÃO' : 'INCIDENTE';
|
|
|
|
|
return {
|
|
|
|
|
title: `${tipo} - ${ticketData.short_description || 'Sem título'}`,
|
|
|
|
|
entityId: 127
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2025-09-15 20:55:19 -03:00
|
|
|
static formatDescription(ticketData) {
|
|
|
|
|
const extractInfo = (regex, description) => {
|
|
|
|
|
if (!description) return null;
|
|
|
|
|
const match = description.match(regex);
|
|
|
|
|
return match ? match[1] : null;
|
|
|
|
|
};
|
|
|
|
|
const extractEmailFromDescription = (description) => {
|
|
|
|
|
return extractInfo(/([\w-\.]+@([\w-]+\.)+[\w-]{2,4})/i, description);
|
|
|
|
|
};
|
|
|
|
|
const extractPhoneFromDescription = (description) => {
|
|
|
|
|
return extractInfo(/(\+?(\d{2})?\s*\(?\d{2}\)?[\s-]*\d{4,5}[\s-]?\d{4})/i, description);
|
|
|
|
|
};
|
|
|
|
|
const extractRamalFromDescription = (description) => {
|
|
|
|
|
return extractInfo(/(\d{3,4})/i, description);
|
2025-09-12 12:09:41 -03:00
|
|
|
};
|
|
|
|
|
|
2025-09-15 20:55:19 -03:00
|
|
|
const email = ticketData.caller_email || extractEmailFromDescription(ticketData.description) || 'N/A';
|
|
|
|
|
const phone = ticketData.caller_phone || extractPhoneFromDescription(ticketData.description) || 'N/A';
|
|
|
|
|
const ramal = ticketData.caller_ramal || extractRamalFromDescription(ticketData.description) || 'N/A';
|
|
|
|
|
|
|
|
|
|
let htmlDescription = `
|
|
|
|
|
<table style="width:100%; border-collapse: collapse;">
|
|
|
|
|
<tr style="background-color:#f2f2f2;">
|
|
|
|
|
<th style="padding: 8px; border: 1px solid #ddd; text-align: left;">Campo</th>
|
|
|
|
|
<th style="padding: 8px; border: 1px solid #ddd; text-align: left;">Valor</th>
|
|
|
|
|
</tr>
|
2025-09-17 20:56:32 -03:00
|
|
|
<tr>
|
|
|
|
|
<td style="padding: 8px; border: 1px solid #ddd;"><strong>Nome:</strong></td>
|
|
|
|
|
<td style="padding: 8px; border: 1px solid #ddd;">${ticketData.caller_id}</td>
|
|
|
|
|
</tr>
|
2025-09-15 20:55:19 -03:00
|
|
|
<tr>
|
|
|
|
|
<td style="padding: 8px; border: 1px solid #ddd;"><strong>Email:</strong></td>
|
|
|
|
|
<td style="padding: 8px; border: 1px solid #ddd;">${email}</td>
|
|
|
|
|
</tr>
|
|
|
|
|
<tr>
|
|
|
|
|
<td style="padding: 8px; border: 1px solid #ddd;"><strong>Telefone:</strong></td>
|
|
|
|
|
<td style="padding: 8px; border: 1px solid #ddd;">${phone}</td>
|
|
|
|
|
</tr>
|
|
|
|
|
<tr>
|
|
|
|
|
<td style="padding: 8px; border: 1px solid #ddd;"><strong>Ramal:</strong></td>
|
|
|
|
|
<td style="padding: 8px; border: 1px solid #ddd;">${ramal}</td>
|
|
|
|
|
</tr>
|
|
|
|
|
<tr>
|
|
|
|
|
<td style="padding: 8px; border: 1px solid #ddd;"><strong>Ticket Number (ServiceNow):</strong></td>
|
|
|
|
|
<td style="padding: 8px; border: 1px solid #ddd;">${ticketData.ticket_number || 'N/A'}</td>
|
|
|
|
|
</tr>
|
|
|
|
|
</table>
|
|
|
|
|
<br>
|
|
|
|
|
<strong>Descrição:</strong>
|
|
|
|
|
<hr>
|
|
|
|
|
${ticketData.description || 'N/A'}
|
|
|
|
|
`;
|
|
|
|
|
return htmlDescription;
|
2025-09-10 20:52:12 -03:00
|
|
|
}
|
|
|
|
|
|
2025-09-15 20:55:19 -03:00
|
|
|
static async createTicket(ticketData) {
|
|
|
|
|
const connection = await glpiPool.getConnection();
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
await connection.beginTransaction();
|
|
|
|
|
|
2025-10-29 14:20:21 -03:00
|
|
|
let entityId = 127;
|
2025-09-17 20:56:32 -03:00
|
|
|
if (ticketData.location_id) {
|
|
|
|
|
entityId = await LocationMappingModel.getGlpiEntityId(ticketData.location_id) || entityId;
|
|
|
|
|
} else if (ticketData.location_name) {
|
|
|
|
|
entityId = await LocationMappingModel.getGlpiEntityIdByName(ticketData.location_name) || entityId;
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-15 20:55:19 -03:00
|
|
|
let email = ticketData.caller_email;
|
|
|
|
|
if (!email && ticketData.description) {
|
|
|
|
|
const emailRegex = /[\w-\.]+@([\w-]+\.)+[\w-]{2,4}/g;
|
|
|
|
|
const matches = ticketData.description.match(emailRegex);
|
|
|
|
|
if (matches && matches.length > 0) {
|
|
|
|
|
email = matches[0];
|
|
|
|
|
}
|
2025-09-12 12:09:41 -03:00
|
|
|
}
|
2025-09-15 20:55:19 -03:00
|
|
|
|
2025-09-18 20:52:39 -03:00
|
|
|
const { title: formattedTitle } = await TicketGlpiModel.formatTitle(ticketData);
|
2025-09-15 20:55:19 -03:00
|
|
|
const formattedDescription = TicketGlpiModel.formatDescription(ticketData);
|
|
|
|
|
const ticketType = ticketData.type === 'requisicao' ? 2 : 1;
|
|
|
|
|
const slasIdTtr = TicketGlpiModel.mapSlasIdTtr(ticketData);
|
|
|
|
|
|
|
|
|
|
const glpiTicketData = {
|
2025-10-29 14:20:21 -03:00
|
|
|
entities_id: entityId,
|
2025-09-18 20:52:39 -03:00
|
|
|
name: formattedTitle,
|
2025-09-15 20:55:19 -03:00
|
|
|
date: ticketData.opened_at || new Date(),
|
|
|
|
|
date_mod: ticketData.updated_at || new Date(),
|
|
|
|
|
status: TicketGlpiModel.mapStatus(ticketData.state || '1'),
|
2025-10-30 08:15:43 -03:00
|
|
|
users_id_recipient: 1118,
|
2025-09-15 20:55:19 -03:00
|
|
|
requesttypes_id: 1,
|
|
|
|
|
content: formattedDescription,
|
|
|
|
|
urgency: 3,
|
|
|
|
|
impact: 3,
|
|
|
|
|
priority: TicketGlpiModel.mapPriority(ticketData.priority || '3'),
|
|
|
|
|
type: ticketType,
|
|
|
|
|
itilcategories_id: TicketGlpiModel.mapCategory(ticketData.short_description),
|
|
|
|
|
global_validation: 1,
|
|
|
|
|
date_creation: ticketData.opened_at || new Date(),
|
|
|
|
|
slas_id_ttr: slasIdTtr
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const fields = Object.keys(glpiTicketData).join(', ');
|
|
|
|
|
const placeholders = Object.keys(glpiTicketData).map(() => '?').join(', ');
|
|
|
|
|
const values = Object.values(glpiTicketData);
|
|
|
|
|
|
|
|
|
|
const ticketQuery = `INSERT INTO glpi_tickets (${fields}) VALUES (${placeholders})`;
|
|
|
|
|
const [ticketResult] = await connection.execute(ticketQuery, values);
|
|
|
|
|
const ticketId = ticketResult.insertId;
|
|
|
|
|
|
|
|
|
|
await connection.execute(
|
|
|
|
|
`INSERT INTO glpi_tickets_users
|
|
|
|
|
(tickets_id, users_id, type, use_notification, alternative_email)
|
|
|
|
|
VALUES (?, ?, 1, 1, '')`,
|
2025-10-30 08:15:43 -03:00
|
|
|
[ticketId, 1118]
|
2025-09-15 20:55:19 -03:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
await connection.execute(
|
|
|
|
|
`INSERT INTO glpi_groups_tickets
|
|
|
|
|
(tickets_id, groups_id, type)
|
|
|
|
|
VALUES (?, ?, 2)`,
|
|
|
|
|
[ticketId, 30]
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
await connection.commit();
|
|
|
|
|
|
|
|
|
|
logInfo(`✅ Ticket GLPI criado: ${ticketId}`);
|
|
|
|
|
return ticketId;
|
|
|
|
|
|
|
|
|
|
} catch (error) {
|
|
|
|
|
await connection.rollback();
|
|
|
|
|
logError(error, `❌ Erro ao criar ticket GLPI`);
|
|
|
|
|
throw error;
|
|
|
|
|
} finally {
|
|
|
|
|
connection.release();
|
2025-09-10 20:52:12 -03:00
|
|
|
}
|
2025-09-15 20:55:19 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static async ticketExists(snTicketNumber) {
|
|
|
|
|
try {
|
|
|
|
|
const query = `
|
2025-10-17 16:58:14 -03:00
|
|
|
SELECT id FROM glpi_tickets WHERE name LIKE ? OR content LIKE ? LIMIT 1
|
2025-09-15 20:55:19 -03:00
|
|
|
`;
|
|
|
|
|
|
2025-10-17 16:58:14 -03:00
|
|
|
const values = [
|
2025-09-15 20:55:19 -03:00
|
|
|
`%${snTicketNumber}%`,
|
|
|
|
|
`%${snTicketNumber}%`
|
2025-10-17 16:58:14 -03:00
|
|
|
];
|
2025-09-15 20:55:19 -03:00
|
|
|
|
2025-10-17 16:58:14 -03:00
|
|
|
|
|
|
|
|
const [rows] = await glpiPool.execute(query,values);
|
|
|
|
|
|
2025-09-15 20:55:19 -03:00
|
|
|
return rows.length > 0 ? rows[0].id : null;
|
|
|
|
|
} catch (error) {
|
2025-10-17 16:58:14 -03:00
|
|
|
logError(`Erro ao verificar ticket existente: ${error}`);
|
2025-09-15 20:55:19 -03:00
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-10-29 14:20:21 -03:00
|
|
|
|
2025-09-23 21:01:01 -03:00
|
|
|
static async getFollowupsByItemId(itemId) {
|
2025-10-23 17:30:03 -03:00
|
|
|
const connection = await glpiPool.getConnection();
|
2025-09-23 21:01:01 -03:00
|
|
|
try {
|
2025-10-30 08:15:43 -03:00
|
|
|
const query = 'SELECT id, content, date, date_creation, date_mod FROM glpi_itilfollowups WHERE items_id = ? AND users_id != 1118';
|
2025-10-23 17:30:03 -03:00
|
|
|
const [rows] = await connection.execute(query, [itemId]);
|
2025-09-23 21:01:01 -03:00
|
|
|
return rows;
|
|
|
|
|
} catch (error) {
|
|
|
|
|
logError(error, 'Erro ao buscar followups do ticket');
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
2025-10-17 16:58:14 -03:00
|
|
|
static async insertComment(comment, items_id) {
|
2025-10-23 17:30:03 -03:00
|
|
|
const connection = await glpiPool.getConnection();
|
2025-10-17 16:58:14 -03:00
|
|
|
try {
|
2025-10-28 14:06:00 -03:00
|
|
|
const query = 'INSERT INTO glpi_itilfollowups(itemtype, items_id, date, users_id, content, date_creation, date_mod, timeline_position) VALUES(?, ?, NOW(), ?, ?, NOW(), NOW(), ?)';
|
2025-10-30 08:15:43 -03:00
|
|
|
const values = ['Ticket', items_id, 1118, comment.content , 1];
|
2025-10-17 16:58:14 -03:00
|
|
|
|
2025-10-23 17:30:03 -03:00
|
|
|
const [result] = await connection.execute(query, values);
|
2025-10-29 14:20:21 -03:00
|
|
|
return [{ id: result.insertId }];
|
2025-10-17 16:58:14 -03:00
|
|
|
}catch(error){
|
2025-10-23 17:30:03 -03:00
|
|
|
logError(`Erro ao inserir o comentário no GLPI: ${error}`);
|
|
|
|
|
} finally {
|
|
|
|
|
if (connection) connection.release();
|
2025-10-17 16:58:14 -03:00
|
|
|
}
|
|
|
|
|
}
|
2025-09-10 20:52:12 -03:00
|
|
|
|
2025-10-23 17:30:03 -03:00
|
|
|
static async getClosedTickets(watermark, limit = 100) {
|
|
|
|
|
const connection = await glpiPool.getConnection();
|
|
|
|
|
try {
|
|
|
|
|
const query = `
|
|
|
|
|
SELECT
|
|
|
|
|
t.id,
|
|
|
|
|
t.solvedate,
|
|
|
|
|
t.closedate,
|
|
|
|
|
s.content as solution
|
|
|
|
|
FROM glpi_tickets t
|
|
|
|
|
LEFT JOIN glpi_itilsolutions s ON t.id = s.items_id AND s.itemtype = 'Ticket'
|
|
|
|
|
WHERE t.status IN (5, 6) AND t.date_mod >= ? LIMIT ?
|
|
|
|
|
`;
|
|
|
|
|
const [rows] = await connection.execute(query, [watermark, limit]);
|
|
|
|
|
return rows;
|
|
|
|
|
} catch (error) {
|
|
|
|
|
logError(error, '❌ Erro ao buscar tickets fechados no GLPI');
|
|
|
|
|
throw error;
|
|
|
|
|
} finally {
|
|
|
|
|
connection.release();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static async closeTicket(ticketId, solutionContent) {
|
|
|
|
|
const connection = await glpiPool.getConnection();
|
|
|
|
|
try {
|
|
|
|
|
await connection.beginTransaction();
|
|
|
|
|
|
|
|
|
|
const solutionQuery = `INSERT INTO glpi_itilsolutions (itemtype, items_id, content, users_id_editor, date_creation, date_mod) VALUES ('Ticket', ?, ?, ?, NOW(), NOW())`;
|
2025-10-30 08:15:43 -03:00
|
|
|
await connection.execute(solutionQuery, [ticketId, solutionContent, 1118]);
|
2025-10-23 17:30:03 -03:00
|
|
|
|
|
|
|
|
const updateTicketQuery = `UPDATE glpi_tickets SET status = 5, solvedate = NOW() WHERE id = ?`;
|
|
|
|
|
await connection.execute(updateTicketQuery, [ticketId]);
|
|
|
|
|
|
|
|
|
|
await connection.commit();
|
|
|
|
|
} catch (error) {
|
|
|
|
|
await connection.rollback();
|
|
|
|
|
logError(error, `❌ Erro ao fechar ticket ${ticketId} no GLPI`);
|
|
|
|
|
throw error;
|
|
|
|
|
} finally {
|
|
|
|
|
connection.release();
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-10-17 16:58:14 -03:00
|
|
|
|
2025-10-23 17:30:03 -03:00
|
|
|
static async getTicketSolution(glpiTicketId) {
|
|
|
|
|
const connection = await glpiPool.getConnection();
|
|
|
|
|
try {
|
|
|
|
|
const query = `
|
|
|
|
|
SELECT content, date_mod, solutiontypes_id
|
|
|
|
|
FROM glpi_itilsolutions
|
|
|
|
|
WHERE items_id = ? AND itemtype = 'Ticket'
|
|
|
|
|
ORDER BY date_mod DESC LIMIT 1`;
|
|
|
|
|
const [rows] = await connection.execute(query, [glpiTicketId]);
|
|
|
|
|
return rows[0] || null;
|
|
|
|
|
} catch (error) {
|
|
|
|
|
logError(error, `❌ Erro ao buscar solução do ticket GLPI ${glpiTicketId}`);
|
|
|
|
|
throw error;
|
|
|
|
|
} finally {
|
|
|
|
|
connection.release();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static async forceCloseTicket(ticketId) {
|
|
|
|
|
const connection = await glpiPool.getConnection();
|
|
|
|
|
try {
|
|
|
|
|
const updateTicketQuery = `UPDATE glpi_tickets SET status = 6, closedate = NOW() WHERE id = ?`;
|
|
|
|
|
await connection.execute(updateTicketQuery, [ticketId]);
|
|
|
|
|
logInfo(`✅ Ticket GLPI ${ticketId} forçado para o estado Fechado (6).`);
|
|
|
|
|
} catch (error) {
|
|
|
|
|
logError(error, `❌ Erro ao forçar fechamento do ticket ${ticketId} no GLPI`);
|
|
|
|
|
} finally {
|
|
|
|
|
if (connection) connection.release();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static async updateStatus(ticketId, status) {
|
|
|
|
|
const connection = await glpiPool.getConnection();
|
|
|
|
|
try {
|
|
|
|
|
await connection.beginTransaction();
|
|
|
|
|
|
|
|
|
|
status = this.mapStatus(status);
|
|
|
|
|
|
|
|
|
|
const updateQuery = `UPDATE glpi_tickets SET status = ? WHERE id = ?`;
|
|
|
|
|
await connection.execute(updateQuery, [status, ticketId]);
|
|
|
|
|
|
|
|
|
|
await connection.commit();
|
|
|
|
|
} catch (error) {
|
|
|
|
|
await connection.rollback();
|
|
|
|
|
logError(error, `❌ Erro ao atualiza ticket ${ticketId} no GLPI`);
|
|
|
|
|
throw error;
|
|
|
|
|
} finally {
|
|
|
|
|
connection.release();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static async getTicketStatus(glpiTicketId) {
|
|
|
|
|
const connection = await glpiPool.getConnection();
|
|
|
|
|
try {
|
|
|
|
|
const query = `SELECT status FROM glpi_tickets WHERE id = ?`;
|
|
|
|
|
const [rows] = await connection.execute(query, [glpiTicketId]);
|
|
|
|
|
if (rows.length > 0) {
|
|
|
|
|
return rows[0].status;
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
} catch (error) {
|
|
|
|
|
logError(error, `❌ Erro ao buscar status do ticket GLPI ${glpiTicketId}`);
|
|
|
|
|
throw error;
|
|
|
|
|
} finally {
|
|
|
|
|
connection.release();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
2025-10-29 14:20:21 -03:00
|
|
|
|
2025-09-15 20:55:19 -03:00
|
|
|
/**
|
|
|
|
|
* @module TicketGlpiModel
|
2025-10-29 14:20:21 -03:00
|
|
|
* @description Este módulo é a camada de acesso a dados para o banco de dados do GLPI.
|
|
|
|
|
* Ele encapsula todas as interações diretas com o banco de dados MySQL do GLPI,
|
|
|
|
|
* oferecendo uma interface clara para criar, atualizar e consultar tickets e seus componentes.
|
2025-09-15 20:55:19 -03:00
|
|
|
*
|
2025-10-29 14:20:21 -03:00
|
|
|
* Principais Funcionalidades:
|
|
|
|
|
* - **Mapeamento de Dados**: Contém funções (`mapStatus`, `mapPriority`, `mapCategory`, etc.) para traduzir os dados do formato do ServiceNow para o formato esperado pelo GLPI.
|
|
|
|
|
* - **Formatação de Conteúdo**: `formatTitle` e `formatDescription` preparam o título e a descrição do ticket, enriquecendo-os com informações da entidade e formatando a descrição em uma tabela HTML para melhor legibilidade.
|
|
|
|
|
* - **Criação de Tickets (`createTicket`)**: Orquestra a criação de um novo ticket no GLPI, incluindo a inserção na tabela principal (`glpi_tickets`), a associação de usuários e grupos.
|
|
|
|
|
* - **Consulta de Dados**: Oferece métodos para verificar a existência de um ticket (`ticketExists`), buscar seu status (`getTicketStatus`), soluções (`getTicketSolution`) e comentários/follow-ups (`getFollowupsByItemId`).
|
|
|
|
|
* - **Atualização de Tickets**: Inclui métodos para atualizar o status (`updateStatus`), adicionar comentários (`insertComment`), e fechar tickets, seja de forma padrão com uma solução (`closeTicket`) ou forçando o estado para "Fechado" (`forceCloseTicket`).
|
2025-09-15 20:55:19 -03:00
|
|
|
*
|
2025-10-29 14:20:21 -03:00
|
|
|
* Este modelo é fundamental para abstrair a complexidade do esquema de banco de dados do GLPI e garantir que as operações sejam realizadas de forma transacional e segura.
|
2025-09-15 20:55:19 -03:00
|
|
|
*/
|
2025-09-10 20:52:12 -03:00
|
|
|
module.exports = TicketGlpiModel;
|