Merge branch 'master' of ssh://10.0.120.75:60000/home/desenvolvimento/HUBXGLPI/HUBXGLPI
This commit is contained in:
commit
668300814e
7
app.js
7
app.js
@ -1,4 +1,9 @@
|
|||||||
const hubsoftController = require('./controller/processController.js');
|
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
|
// Inicia o processamento dos atendimentos
|
||||||
hubsoftController.processaAtendimentos(skipHubSoft = true);
|
hubsoftController.processaAtendimentos();
|
||||||
@ -61,22 +61,9 @@ const formatDescription = (ticketData) => {
|
|||||||
</table>
|
</table>
|
||||||
`;
|
`;
|
||||||
|
|
||||||
console.log(htmlDescription);
|
|
||||||
|
|
||||||
return 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
|
// Formata os dados do ticket para o GLPI
|
||||||
const formatTicketDataForGlpi = async (ticketData) => {
|
const formatTicketDataForGlpi = async (ticketData) => {
|
||||||
const formattedData = {
|
const formattedData = {
|
||||||
@ -102,24 +89,39 @@ const formatTicketDataForGlpi = async (ticketData) => {
|
|||||||
|
|
||||||
const createGlpiTicket = async (ticketData) => {
|
const createGlpiTicket = async (ticketData) => {
|
||||||
try {
|
try {
|
||||||
|
|
||||||
|
|
||||||
const formattedTicketData = await formatTicketDataForGlpi(ticketData);
|
const formattedTicketData = await formatTicketDataForGlpi(ticketData);
|
||||||
// console.log('Dados do ticket formatados para GLPI:', formattedTicketData);
|
|
||||||
|
|
||||||
const glpiTicket = await glpiModel.insertTicket(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.status_sync = 'created_glpi';
|
||||||
ticketData.glpi_ticket_id = glpiTicket.insertId;
|
ticketData.glpi_ticket_id = glpiTicket.insertId;
|
||||||
|
ticketData.created_at = new Date();
|
||||||
|
ticketData.updated_at = new Date();
|
||||||
|
|
||||||
|
|
||||||
const updateSyncData = await hubglpiModel.update_syncData(ticketData);
|
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) {
|
} catch (error) {
|
||||||
console.error('Erro ao criar ticket no GLPI:', error);
|
const updateSyncDataError = await hubglpiModel.update_syncaDataError(error.message, ticketData.id_atendimento)
|
||||||
// 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}`);
|
||||||
logError('Erro ao criar ticket no GLPI. Sync Data atualizado com a mensagem de erro: ', /*updateSyncDataError*/ 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;
|
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.descricao_abertura = ticketData.descricao_abertura.replace(/[^0-9]/g, '');
|
||||||
ticketData.ticket_mundiale = parseInt(ticketData.descricao_abertura) || null;
|
ticketData.ticket_mundiale = parseInt(ticketData.descricao_abertura) || null;
|
||||||
|
|
||||||
@ -152,20 +150,29 @@ const processTicketFromHubSoft = async (atendimento) => {
|
|||||||
const saveTicketToHubGlpi = async (ticketData) => {
|
const saveTicketToHubGlpi = async (ticketData) => {
|
||||||
try {
|
try {
|
||||||
const insertedTicket = await hubglpiModel.insertTicket(ticketData);
|
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);
|
await hubglpiModel.insertSyncData(ticketData.id_atendimento);
|
||||||
console.log('Dados inseridos/atualizados na tabela sync_data:', insertSyncData);
|
logInfo('Dados inseridos/atualizados na tabela sync_data');
|
||||||
|
|
||||||
return insertedTicket;
|
return insertedTicket;
|
||||||
} catch (error) {
|
} 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
|
throw error; // Rejeita a promise para que o erro seja tratado no nível superior
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const processAtendimento = async (ticketData) => {
|
const processAtendimento = async (ticketData) => {
|
||||||
|
|
||||||
try {
|
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}`;
|
const titulo = `Mundiale - Protocolo: ${ticketData.ticket_mundiale} - ${ticketData.cliente_nome}`;
|
||||||
ticketData.titulo = titulo;
|
ticketData.titulo = titulo;
|
||||||
@ -179,11 +186,10 @@ const processAtendimento = async (ticketData) => {
|
|||||||
} else {
|
} else {
|
||||||
ticketData.entidades_id = 0;
|
ticketData.entidades_id = 0;
|
||||||
}
|
}
|
||||||
console.log(ticketData.entidades_id)
|
|
||||||
await createGlpiTicket(ticketData);
|
await createGlpiTicket(ticketData);
|
||||||
|
|
||||||
} catch (error) {
|
} 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) {
|
if (!skipHubSoft) {
|
||||||
// Esta parte depende da VPN
|
// Esta parte depende da VPN
|
||||||
console.log('Buscando atendimentos do HubSoft...');
|
logInfo('Buscando atendimentos do HubSoft...');
|
||||||
atendimentosDB = await hubsoftModel.getAtendimentosFromDB();
|
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) {
|
for (const atendimento of atendimentosDB) {
|
||||||
try {
|
try {
|
||||||
@ -209,12 +215,12 @@ const processaAtendimentos = async (skipHubSoft = false) => {
|
|||||||
await saveTicketToHubGlpi(ticketData);
|
await saveTicketToHubGlpi(ticketData);
|
||||||
}
|
}
|
||||||
catch (error) {
|
catch (error) {
|
||||||
console.error(`Erro ao processar atendimento ${atendimento.id_atendimento}:`, error);
|
logError(`Erro ao processar atendimento ${atendimento.id_atendimento}:`, error);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
} else {
|
} 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)
|
// Coletando atendimentos do banco intermediário (hubglpi)
|
||||||
@ -224,18 +230,16 @@ const processaAtendimentos = async (skipHubSoft = false) => {
|
|||||||
for (const atendimento of atendimentosDB) {
|
for (const atendimento of atendimentosDB) {
|
||||||
try {
|
try {
|
||||||
// Processa o atendimento para o GLPI
|
// Processa o atendimento para o GLPI
|
||||||
|
|
||||||
console.log(atendimento.id_atendimento)
|
|
||||||
// console.log(atendimento)
|
|
||||||
await processAtendimento(atendimento);
|
await processAtendimento(atendimento);
|
||||||
|
|
||||||
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(`Erro ao processar atendimento ${atendimento.id_atendimento}:`, error);
|
logError(`Erro ao processar atendimento ${atendimento.id_atendimento}:, ${error}`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
} catch (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,
|
ticketData.date_creation,
|
||||||
37
|
37
|
||||||
];
|
];
|
||||||
console.log(query, values);
|
|
||||||
try {
|
try {
|
||||||
const [rows] = await pool.execute(query, values);
|
const [rows] = await pool.execute(query, values)
|
||||||
logInfo('Ticket inserido com sucesso:', rows);
|
|
||||||
return rows;
|
return rows;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
logError('Erro ao inserir ticket:', error);
|
logError('Erro ao inserir ticket:', error);
|
||||||
@ -74,13 +72,16 @@ class GlpiModel {
|
|||||||
try {
|
try {
|
||||||
const [rows] = await pool.execute(query, values);
|
const [rows] = await pool.execute(query, values);
|
||||||
if (!rows || rows.length === 0) {
|
if (!rows || rows.length === 0) {
|
||||||
logInfo('Entidade não encontrada para:', id);
|
logInfo(`Entidade não encontrada para: ${id} `);
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
logInfo('Entidade encontrada:', rows[0]);
|
|
||||||
|
logInfo(`Entidade encontrada para: ${id}`);
|
||||||
|
|
||||||
|
|
||||||
return Number(rows[0].id);
|
return Number(rows[0].id);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
logError('Erro ao buscar entidade', err);
|
logError(`Erro ao buscar entidade: ${err}`);
|
||||||
throw err;
|
throw err;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -92,12 +93,11 @@ class GlpiModel {
|
|||||||
`;
|
`;
|
||||||
const values = [glpiTicketId, 25, 2];
|
const values = [glpiTicketId, 25, 2];
|
||||||
try {
|
try {
|
||||||
const res = await pool.query(query, values);
|
const [rows] = await pool.query(query, values);
|
||||||
logInfo('Grupo atribuído inserido com sucesso');
|
return rows && rows.insertId ? rows.insertId : null;
|
||||||
return res || null;
|
|
||||||
}
|
}
|
||||||
catch (err) {
|
catch (err) {
|
||||||
logError('Erro ao inserir grupo atribuído', err);
|
logError(`Erro ao Adicionar Grupo Operação NOC: ${err}`);
|
||||||
throw err;
|
throw err;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -54,15 +54,11 @@ class HubglpiModel {
|
|||||||
ticketData.data_cadastro
|
ticketData.data_cadastro
|
||||||
];
|
];
|
||||||
|
|
||||||
console.log('Executando query de inserção/atualização na tabela hubsoft_tickets:', query, values);
|
|
||||||
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const res = await pool.query(query, values);
|
const res = await pool.query(query, values);
|
||||||
logInfo('Ticket Inserido/Atualizado na tabela hubsoft_tickets com sucesso:', res.rows[0]);
|
|
||||||
return res.rows[0];
|
return res.rows[0];
|
||||||
} catch (err) {
|
} 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;
|
throw err;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -87,10 +83,9 @@ class HubglpiModel {
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
const res = await pool.query(query, values);
|
const res = await pool.query(query, values);
|
||||||
logInfo('Dados inseridos/atualizados na tabela sync_data com sucesso:', res.rows[0]);
|
|
||||||
return res.rows[0];
|
return res.rows[0];
|
||||||
} catch (err) {
|
} 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;
|
throw err;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -112,24 +107,24 @@ class HubglpiModel {
|
|||||||
sync_update.glpi_ticket_id,
|
sync_update.glpi_ticket_id,
|
||||||
sync_update.status_sync,
|
sync_update.status_sync,
|
||||||
sync_update.sync_metadata,
|
sync_update.sync_metadata,
|
||||||
sync_update.last_sync_attempt,
|
new Date(),
|
||||||
sync_update.sync_error_message,
|
sync_update.sync_error_message,
|
||||||
sync_update.sync_created_at,
|
sync_update.created_at,
|
||||||
sync_update.sync_updated_at,
|
sync_update.updated_at,
|
||||||
sync_update.sync_data_id
|
sync_update.sync_data_id
|
||||||
|
|
||||||
]; //Todo colocar parametros dinamicos || null se não tiver
|
]; //Todo colocar parametros dinamicos || null se não tiver
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const res = await pool.query(query, values);
|
const res = await pool.query(query, values);
|
||||||
logInfo('Dados atualizados na tabela sync_data com sucesso:', res.rows[0]);
|
|
||||||
return res.rows[0];
|
return res.rows[0];
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
logError('Erro ao atualizar dados na tabela sync_data', err);
|
logError(`Erro ao atualizar dados na tabela sync_data: ${err}`);
|
||||||
throw err;
|
throw err;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static async get_idSyncByHubsoftId(hubsoft_ticket_id) {
|
static async get_idSyncByHubsoftId(hubsoft_ticket_id) {
|
||||||
const query = `
|
const query = `
|
||||||
SELECT id FROM sync_data
|
SELECT id FROM sync_data
|
||||||
@ -175,15 +170,53 @@ class HubglpiModel {
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
const res = await pool.query(query);
|
const res = await pool.query(query);
|
||||||
logInfo('Tickets pendentes obtidos com sucesso:', res.rows);
|
|
||||||
return res.rows;
|
return res.rows;
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
logError('Erro ao obter tickets pendentes', err);
|
logError(`Erro ao obter tickets pendentes ${err}`);
|
||||||
throw 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