Merge branch 'master' of ssh://10.0.120.75:60000/home/desenvolvimento/HUBXGLPI/HUBXGLPI
This commit is contained in:
commit
d197d9903b
7
app.js
7
app.js
@ -1,4 +1,9 @@
|
||||
const hubsoftController = require('./controller/processController.js');
|
||||
|
||||
logInfo('Aplicação iniciada', {
|
||||
timestamp: new Date().toISOString(),
|
||||
environment: process.env.NODE_ENV || 'development'
|
||||
});
|
||||
|
||||
// Inicia o processamento dos atendimentos
|
||||
hubsoftController.processaAtendimentos(skipHubSoft = true);
|
||||
hubsoftController.processaAtendimentos();
|
||||
@ -61,22 +61,9 @@ const formatDescription = (ticketData) => {
|
||||
</table>
|
||||
`;
|
||||
|
||||
console.log(htmlDescription);
|
||||
|
||||
return htmlDescription;
|
||||
};
|
||||
|
||||
|
||||
// // Gera o entity_Name baseado nos campos do ticket
|
||||
// const generateEntityName = (ticketData) => {
|
||||
// const codigoClienteParts = (ticketData.codigo_cliente || '').split('-');
|
||||
// const codigoServicoFirstPart = (ticketData.codigo_servico || '').split('-')[0]?.trim() || '';
|
||||
// const clienteNome = ticketData.cliente_nome || '';
|
||||
|
||||
// return [...codigoClienteParts, codigoServicoFirstPart, clienteNome].join('');
|
||||
// };
|
||||
|
||||
|
||||
// Formata os dados do ticket para o GLPI
|
||||
const formatTicketDataForGlpi = async (ticketData) => {
|
||||
const formattedData = {
|
||||
@ -102,24 +89,39 @@ const formatTicketDataForGlpi = async (ticketData) => {
|
||||
|
||||
const createGlpiTicket = async (ticketData) => {
|
||||
try {
|
||||
|
||||
|
||||
const formattedTicketData = await formatTicketDataForGlpi(ticketData);
|
||||
// console.log('Dados do ticket formatados para GLPI:', formattedTicketData);
|
||||
|
||||
const glpiTicket = await glpiModel.insertTicket(formattedTicketData);
|
||||
console.log('Ticket criado no GLPI:', glpiTicket);
|
||||
logInfo(`Ticket criado no GLPI: ${glpiTicket.insertId} `);
|
||||
|
||||
await glpiModel.insertGroupTickets(glpiTicket.insertId);
|
||||
|
||||
//Atualiza que ticket foi criado
|
||||
ticketData.status_sync = 'created_glpi';
|
||||
ticketData.glpi_ticket_id = glpiTicket.insertId;
|
||||
ticketData.created_at = new Date();
|
||||
ticketData.updated_at = new Date();
|
||||
|
||||
|
||||
const updateSyncData = await hubglpiModel.update_syncData(ticketData);
|
||||
logInfo('Sync Data atualizado com o ID do ticket do GLPI:', updateSyncData);
|
||||
logInfo(`Sync Data atualizado com o ID do ticket do GLPI: ${updateSyncData.glpi_ticket_id}`);
|
||||
|
||||
|
||||
try {
|
||||
//Insetingo ao grupo Operacao NOC
|
||||
await glpiModel.insertGroupTickets(glpiTicket.insertId);
|
||||
logInfo(`Atribuido grupo Operação NOC ao ticket no GLPI: ${glpiTicket.insertId} `)
|
||||
|
||||
} catch (error) {
|
||||
console.error('Erro ao criar ticket no GLPI:', error);
|
||||
// const updateSyncDataError = await hubglpiModel.update_syncaDataError(error.message, ticketData.id_atendimento)
|
||||
logError('Erro ao criar ticket no GLPI. Sync Data atualizado com a mensagem de erro: ', /*updateSyncDataError*/ error);
|
||||
const updateSyncDataError = await hubglpiModel.update_syncaDataError(error.message, ticketData.id_atendimento)
|
||||
logError(`Erro ao criar ticket no GLPI. Sync Data atualizado com a mensagem de erro: ${error}`);
|
||||
}
|
||||
|
||||
|
||||
} catch (error) {
|
||||
|
||||
const updateSyncDataError = await hubglpiModel.update_syncaDataError(error.message, ticketData.id_atendimento)
|
||||
logError(`Erro ao criar ticket no GLPI. Sync Data atualizado com a mensagem de erro: ${error}`);
|
||||
}
|
||||
};
|
||||
|
||||
@ -139,10 +141,6 @@ const processTicketFromHubSoft = async (atendimento) => {
|
||||
|
||||
ticketData.status_atendimento = statusAtendimentoHubGlpi[ticketData.id_atendimento_status] || ticketData.id_atendimento_status;
|
||||
|
||||
console.log(typeof ticketData.descricao_abertura)
|
||||
console.log(ticketData.descricao_abertura)
|
||||
|
||||
|
||||
ticketData.descricao_abertura = ticketData.descricao_abertura.replace(/[^0-9]/g, '');
|
||||
ticketData.ticket_mundiale = parseInt(ticketData.descricao_abertura) || null;
|
||||
|
||||
@ -152,20 +150,29 @@ const processTicketFromHubSoft = async (atendimento) => {
|
||||
const saveTicketToHubGlpi = async (ticketData) => {
|
||||
try {
|
||||
const insertedTicket = await hubglpiModel.insertTicket(ticketData);
|
||||
console.log('Ticket inserido/atualizado na tabela hubsoft_tickets:', insertedTicket);
|
||||
logInfo(`Ticket inserido/atualizado na tabela hubsoft_tickets: ${insertedTicket.protocolo_hub}`);
|
||||
|
||||
const insertSyncData = await hubglpiModel.insertSyncData(ticketData.id_atendimento);
|
||||
console.log('Dados inseridos/atualizados na tabela sync_data:', insertSyncData);
|
||||
await hubglpiModel.insertSyncData(ticketData.id_atendimento);
|
||||
logInfo('Dados inseridos/atualizados na tabela sync_data');
|
||||
|
||||
return insertedTicket;
|
||||
} catch (error) {
|
||||
console.error('Erro ao salvar ticket no hubglpi:', error);
|
||||
logError(`Erro ao salvar ticket no hubglpi: ${error}`);
|
||||
throw error; // Rejeita a promise para que o erro seja tratado no nível superior
|
||||
}
|
||||
};
|
||||
|
||||
const processAtendimento = async (ticketData) => {
|
||||
|
||||
try {
|
||||
const glpiTicketId = await hubglpiModel.getGlpiTicketIdByAtendimentoId(ticketData.id_atendimento);
|
||||
if (glpiTicketId) {
|
||||
logInfo(`Ticket já inserido no GLPI para id_atendimento ${ticketData.id_atendimento}, pulando criação.`);
|
||||
ticketData.status_sync = 'created_glpi';
|
||||
ticketData.glpi_ticket_id = glpiTicketId;
|
||||
await hubglpiModel.update_syncData(ticketData);
|
||||
return;
|
||||
}
|
||||
|
||||
const titulo = `Mundiale - Protocolo: ${ticketData.ticket_mundiale} - ${ticketData.cliente_nome}`;
|
||||
ticketData.titulo = titulo;
|
||||
@ -179,11 +186,10 @@ const processAtendimento = async (ticketData) => {
|
||||
} else {
|
||||
ticketData.entidades_id = 0;
|
||||
}
|
||||
console.log(ticketData.entidades_id)
|
||||
await createGlpiTicket(ticketData);
|
||||
|
||||
} catch (error) {
|
||||
console.error(`Erro ao processar atendimento ${id_atendimento}:`, error);
|
||||
console.error(`Erro ao processar atendimento ${ticketData.id_atendimento}:`, error);
|
||||
}
|
||||
};
|
||||
|
||||
@ -197,9 +203,9 @@ const processaAtendimentos = async (skipHubSoft = false) => {
|
||||
|
||||
if (!skipHubSoft) {
|
||||
// Esta parte depende da VPN
|
||||
console.log('Buscando atendimentos do HubSoft...');
|
||||
logInfo('Buscando atendimentos do HubSoft...');
|
||||
atendimentosDB = await hubsoftModel.getAtendimentosFromDB();
|
||||
console.log(`Total de atendimentos obtidos do HubSoft: ${atendimentosDB.length}`);
|
||||
logInfo(`Total de atendimentos obtidos do HubSoft: ${atendimentosDB.length}`);
|
||||
|
||||
for (const atendimento of atendimentosDB) {
|
||||
try {
|
||||
@ -209,12 +215,12 @@ const processaAtendimentos = async (skipHubSoft = false) => {
|
||||
await saveTicketToHubGlpi(ticketData);
|
||||
}
|
||||
catch (error) {
|
||||
console.error(`Erro ao processar atendimento ${atendimento.id_atendimento}:`, error);
|
||||
logError(`Erro ao processar atendimento ${atendimento.id_atendimento}:`, error);
|
||||
}
|
||||
}
|
||||
|
||||
} else {
|
||||
console.log('Pulando a busca de atendimentos do HubSoft (skipHubSoft = true).');
|
||||
logInfo('Pulando a busca de atendimentos do HubSoft (skipHubSoft = true)');
|
||||
}
|
||||
|
||||
// Coletando atendimentos do banco intermediário (hubglpi)
|
||||
@ -224,18 +230,16 @@ const processaAtendimentos = async (skipHubSoft = false) => {
|
||||
for (const atendimento of atendimentosDB) {
|
||||
try {
|
||||
// Processa o atendimento para o GLPI
|
||||
|
||||
console.log(atendimento.id_atendimento)
|
||||
// console.log(atendimento)
|
||||
await processAtendimento(atendimento);
|
||||
|
||||
|
||||
} catch (error) {
|
||||
console.error(`Erro ao processar atendimento ${atendimento.id_atendimento}:`, error);
|
||||
logError(`Erro ao processar atendimento ${atendimento.id_atendimento}:, ${error}`);
|
||||
}
|
||||
}
|
||||
|
||||
} catch (error) {
|
||||
console.error('Erro ao processar atendimentos:', error);
|
||||
logError(`Erro ao processar atendimentos: ${error}`);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@ -55,10 +55,8 @@ class GlpiModel {
|
||||
ticketData.date_creation,
|
||||
37
|
||||
];
|
||||
console.log(query, values);
|
||||
try {
|
||||
const [rows] = await pool.execute(query, values);
|
||||
logInfo('Ticket inserido com sucesso:', rows);
|
||||
const [rows] = await pool.execute(query, values)
|
||||
return rows;
|
||||
} catch (error) {
|
||||
logError('Erro ao inserir ticket:', error);
|
||||
@ -74,13 +72,16 @@ class GlpiModel {
|
||||
try {
|
||||
const [rows] = await pool.execute(query, values);
|
||||
if (!rows || rows.length === 0) {
|
||||
logInfo('Entidade não encontrada para:', id);
|
||||
logInfo(`Entidade não encontrada para: ${id} `);
|
||||
return null;
|
||||
}
|
||||
logInfo('Entidade encontrada:', rows[0]);
|
||||
|
||||
logInfo(`Entidade encontrada para: ${id}`);
|
||||
|
||||
|
||||
return Number(rows[0].id);
|
||||
} catch (err) {
|
||||
logError('Erro ao buscar entidade', err);
|
||||
logError(`Erro ao buscar entidade: ${err}`);
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
@ -92,12 +93,11 @@ class GlpiModel {
|
||||
`;
|
||||
const values = [glpiTicketId, 25, 2];
|
||||
try {
|
||||
const res = await pool.query(query, values);
|
||||
logInfo('Grupo atribuído inserido com sucesso');
|
||||
return res || null;
|
||||
const [rows] = await pool.query(query, values);
|
||||
return rows && rows.insertId ? rows.insertId : null;
|
||||
}
|
||||
catch (err) {
|
||||
logError('Erro ao inserir grupo atribuído', err);
|
||||
logError(`Erro ao Adicionar Grupo Operação NOC: ${err}`);
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
|
||||
@ -54,15 +54,11 @@ class HubglpiModel {
|
||||
ticketData.data_cadastro
|
||||
];
|
||||
|
||||
console.log('Executando query de inserção/atualização na tabela hubsoft_tickets:', query, values);
|
||||
|
||||
|
||||
try {
|
||||
const res = await pool.query(query, values);
|
||||
logInfo('Ticket Inserido/Atualizado na tabela hubsoft_tickets com sucesso:', res.rows[0]);
|
||||
return res.rows[0];
|
||||
} catch (err) {
|
||||
logError('Erro ao inserir/atualizar ticket na tabela hubsoft_tickets', err);
|
||||
logError(`Erro ao inserir/atualizar ticket na tabela hubsoft_tickets: ${err}`);
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
@ -87,10 +83,9 @@ class HubglpiModel {
|
||||
|
||||
try {
|
||||
const res = await pool.query(query, values);
|
||||
logInfo('Dados inseridos/atualizados na tabela sync_data com sucesso:', res.rows[0]);
|
||||
return res.rows[0];
|
||||
} catch (err) {
|
||||
logError('Erro ao inserir/atualizar dados na tabela sync_data', err);
|
||||
logError(`Erro ao inserir/atualizar dados na tabela sync_data: ${err}`);
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
@ -112,24 +107,24 @@ class HubglpiModel {
|
||||
sync_update.glpi_ticket_id,
|
||||
sync_update.status_sync,
|
||||
sync_update.sync_metadata,
|
||||
sync_update.last_sync_attempt,
|
||||
new Date(),
|
||||
sync_update.sync_error_message,
|
||||
sync_update.sync_created_at,
|
||||
sync_update.sync_updated_at,
|
||||
sync_update.created_at,
|
||||
sync_update.updated_at,
|
||||
sync_update.sync_data_id
|
||||
|
||||
]; //Todo colocar parametros dinamicos || null se não tiver
|
||||
|
||||
try {
|
||||
const res = await pool.query(query, values);
|
||||
logInfo('Dados atualizados na tabela sync_data com sucesso:', res.rows[0]);
|
||||
return res.rows[0];
|
||||
} catch (err) {
|
||||
logError('Erro ao atualizar dados na tabela sync_data', err);
|
||||
logError(`Erro ao atualizar dados na tabela sync_data: ${err}`);
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static async get_idSyncByHubsoftId(hubsoft_ticket_id) {
|
||||
const query = `
|
||||
SELECT id FROM sync_data
|
||||
@ -175,15 +170,53 @@ class HubglpiModel {
|
||||
|
||||
try {
|
||||
const res = await pool.query(query);
|
||||
logInfo('Tickets pendentes obtidos com sucesso:', res.rows);
|
||||
return res.rows;
|
||||
} catch (err) {
|
||||
logError('Erro ao obter tickets pendentes', err);
|
||||
logError(`Erro ao obter tickets pendentes ${err}`);
|
||||
throw err;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
static async getGlpiTicketIdByAtendimentoId(id_atendimento) {
|
||||
const query = `
|
||||
SELECT glpi_ticket_id FROM sync_data
|
||||
WHERE hubsoft_ticket_id = $1;
|
||||
`;
|
||||
const values = [id_atendimento];
|
||||
try {
|
||||
const res = await pool.query(query, values);
|
||||
return res.rows[0] ? res.rows[0].glpi_ticket_id : null;
|
||||
} catch (err) {
|
||||
logError(`Erro ao obter glpi_ticket_id por id_atendimento, ${err}`);
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
|
||||
static async update_syncaDataError(sync_error_message, id_atendimento) {
|
||||
const query = `
|
||||
UPDATE sync_data
|
||||
set sync_error_message = $1,
|
||||
status_sync = 'sync_error',
|
||||
last_sync_attempt = $2
|
||||
WHERE hubsoft_ticket_id = $3
|
||||
RETURNING *;
|
||||
`;
|
||||
const values = [
|
||||
sync_error_message,
|
||||
new Date(),
|
||||
id_atendimento
|
||||
];
|
||||
|
||||
try {
|
||||
const res = await pool.query(query, values);
|
||||
return res.rows[0];
|
||||
} catch (err){
|
||||
logError(`Erro ao inserir mensagem de erro na tabela sync_data: ${err}`);
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
BIN
scripts/data/database2.sql
Normal file
BIN
scripts/data/database2.sql
Normal file
Binary file not shown.
Loading…
Reference in New Issue
Block a user