WIP: Atualiza bancos de dados em situação de fechamento de chamado.
This commit is contained in:
parent
8e47f46459
commit
f8095c80e5
@ -208,8 +208,26 @@ class HubglpiModel {
|
||||
logError(`Erro ao inserir mensagem de erro na tabela sync_data: ${err}`);
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
|
||||
static async updateSyncDataStatus(id_atendimento, status_sync, source_last) {
|
||||
const query = `
|
||||
UPDATE sync_data
|
||||
SET status_sync = $1,
|
||||
source_last = $2
|
||||
WHERE hubsoft_ticket_id = $3
|
||||
RETURNING *;
|
||||
`;
|
||||
const values = [status_sync, source_last, id_atendimento];
|
||||
|
||||
try {
|
||||
const res = await pool.query(query, values);
|
||||
return res.rows[0];
|
||||
} catch (err) {
|
||||
logError(`Erro ao atualizar status_sync na tabela sync_data: ${err}`);
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
module.exports = HubglpiModel;
|
||||
|
||||
@ -26,6 +26,27 @@ const validateMensagensByAtendimento = (id_atendimento) => {
|
||||
return pool.query(query);
|
||||
}
|
||||
|
||||
const updateFechaAtendimento = async (id_atendimento, closingMessage) => {
|
||||
const query = `
|
||||
UPDATE atendimento
|
||||
SET descricao_fechamento = $1,
|
||||
data_fechamento = $2,
|
||||
status_atendimento = $3
|
||||
WHERE id_atendimento = $4;
|
||||
`;
|
||||
|
||||
// Precisamos descobrir qual é o status de "Fechado" no Hubsoft
|
||||
|
||||
const values = [closingMessage, new Date(), 'Solucionado', id_atendimento];
|
||||
try {
|
||||
const res = await pool.query(query, values);
|
||||
return res.rows[0];
|
||||
} catch (err) {
|
||||
logError(`Erro ao atualizar fechamento de atendimento: ${err}`);
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
getAtendimentosFromDB, validateAtendimentoStatus, validateMensagensByAtendimento
|
||||
};
|
||||
@ -1,7 +1,122 @@
|
||||
// HUBXGLPI/src/services/ticketService.js
|
||||
|
||||
const { log } = require("winston");
|
||||
const { get_idSyncByGlpiID } = require("../model/hubglpiModel.js");
|
||||
const { updateSyncDataStatus } = require("../model/syncDataModel.js");
|
||||
const { closeAtendimento } = require("./hubsoftService.js");
|
||||
|
||||
// {
|
||||
// "item": {
|
||||
// "id": 27779,
|
||||
// "itemtype": "Ticket",
|
||||
// "items_id": 34213,
|
||||
// "content": "<p>Closing Task<\/p>",
|
||||
// "user": {
|
||||
// "id": 917,
|
||||
// "name": "rafael.lopes"
|
||||
// },
|
||||
// "user_editor": null
|
||||
// },
|
||||
// "event": "new",
|
||||
// "parent_item": {
|
||||
// "id": 34213,
|
||||
// "name": "TESTE",
|
||||
// "content": "<p>teste<\/p>",
|
||||
// "is_deleted": false,
|
||||
// "urgency": 3,
|
||||
// "impact": 3,
|
||||
// "priority": 3,
|
||||
// "actiontime": 0,
|
||||
// "date_creation": "2025-11-03T16:26:12-03:00",
|
||||
// "date_mod": "2025-11-05T10:44:03-03:00",
|
||||
// "date": "2025-11-03T16:26:12-03:00",
|
||||
// "type": 1,
|
||||
// "external_id": "",
|
||||
// "status": {
|
||||
// "id": 5,
|
||||
// "name": "Solucionado"
|
||||
// },
|
||||
// "category": {
|
||||
// "id": 5814,
|
||||
// "name": "HubSoft"
|
||||
// },
|
||||
// "location": null,
|
||||
// "request_type": {
|
||||
// "id": 1,
|
||||
// "name": "Helpdesk"
|
||||
// },
|
||||
// "entity": {
|
||||
// "id": 0,
|
||||
// "name": "Contratos Ativos",
|
||||
// "completename": "Contratos Ativos"
|
||||
// },
|
||||
// "team": []
|
||||
// }
|
||||
// }
|
||||
|
||||
const updateSyncStatus = async (ticketId, ticketTitle) => {
|
||||
try {
|
||||
//verificar se titulo contem "Mundiale"
|
||||
if (ticketTitle.includes("Mundiale")) {
|
||||
const exists = await get_idSyncByGlpiID(ticketId);
|
||||
if (exists) {
|
||||
log.info(
|
||||
`-> Atualizando banco de dados para o glpi ticket ID ${ticketId} que será fechado.`
|
||||
);
|
||||
const updateResult = await updateSyncDataStatus(
|
||||
ticketId,
|
||||
"pending_close",
|
||||
"glpi"
|
||||
);
|
||||
|
||||
if (updateResult) {
|
||||
log.info(
|
||||
`Status de sync_data atualizado para fechamento do glpi ticket ID ${ticketId}.`
|
||||
);
|
||||
return true;
|
||||
} else {
|
||||
log.error(
|
||||
`Falha ao atualizar status de sync_data para o glpi ticket ID ${ticketId}.`
|
||||
);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
log.info(
|
||||
`Ticket ID ${ticketId} não relacionado à Mundiale. Ignorando fechamento.`
|
||||
);
|
||||
}
|
||||
} catch (error) {}
|
||||
};
|
||||
|
||||
const fechaTicket = async (bodyRequest) => {
|
||||
console.log('Ticket recebido no service:', bodyRequest);
|
||||
return { bodyRequest };
|
||||
}
|
||||
const ticketId = bodyRequest.item.items_id;
|
||||
const ticketTitle = bodyRequest.parent_item.name;
|
||||
const closingMessage = bodyRequest.item.content || "Fechamento automático do ticket.";
|
||||
|
||||
try {
|
||||
const updateStatus = await updateSyncStatus(ticketId, ticketTitle);
|
||||
if (updateStatus) {
|
||||
log.info(`-> Iniciando fechamento do atendimento no HubSoft para o ticket ID ${ticketId}.`);
|
||||
const closeResponse = await closeAtendimento(ticketId, closingMessage);
|
||||
|
||||
if (closeResponse.status === "success") {
|
||||
log.info(`Atendimento no HubSoft fechado com sucesso para o ticket ID ${ticketId}.`);
|
||||
const dbHubsoftClose = await updateFechaAtendimento(ticketId, closingMessage);
|
||||
if (dbHubsoftClose) {
|
||||
log.info(`Banco de dados atualizado com sucesso para o fechamento do atendimento ID ${ticketId}.`);
|
||||
return closeResponse;
|
||||
} else {
|
||||
log.error(`Falha ao atualizar o banco de dados para o fechamento do atendimento ID ${ticketId}.`);
|
||||
return closeResponse;
|
||||
}
|
||||
} else {
|
||||
log.error(`Falha ao fechar atendimento no HubSoft para o ticket ID ${ticketId}. Resposta: ${JSON.stringify(closeResponse)}`);
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
log.error(`Erro ao fechar atendimento no HubSoft para o ticket ID ${ticketId}: ${error}`);
|
||||
}
|
||||
};
|
||||
|
||||
module.exports = { fechaTicket };
|
||||
|
||||
Loading…
Reference in New Issue
Block a user