From c952fa79608d86031824560d73a3347a6071bd13 Mon Sep 17 00:00:00 2001 From: adriano Date: Mon, 16 Jan 2023 17:52:35 -0300 Subject: [PATCH] =?UTF-8?q?Altera=C3=A7=C3=A3o=20para=20incluir=20o=20stat?= =?UTF-8?q?us=20500=20no=20erro=20a=20consulta=20a=20api=20da=20espacolaze?= =?UTF-8?q?r?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/src/controllers/HitController.ts | 84 ++++++++++--------- backend/src/helpers/EndpointQuery.ts | 2 +- .../WbotServices/wbotMessageListener.ts | 13 ++- 3 files changed, 55 insertions(+), 44 deletions(-) diff --git a/backend/src/controllers/HitController.ts b/backend/src/controllers/HitController.ts index 3ba79a8..9950778 100644 --- a/backend/src/controllers/HitController.ts +++ b/backend/src/controllers/HitController.ts @@ -19,7 +19,7 @@ import hitPortalMonitoring from "../helpers/HitPortalMonitoring"; // type IndexQuery = { // centro_custo: string; // }; - + export const hit = async (req: Request, res: Response): Promise => { // const { @@ -32,47 +32,53 @@ export const hit = async (req: Request, res: Response): Promise => { if (req.headers["auth"] === '0424bd59b807674191e7d77572075f33') { - let contact = await ContactByCustomField(req.body['centro_custo']) + if (req.body['action'] === 'closed') { - console.log('--------------> contact: ', contact) + } + else { + + let contact = await ContactByCustomField(req.body['centro_custo']) + + console.log('--------------> contact: ', contact) + + if (contact) { + + try { + + let response = await hitPortalMonitoring(req.body['centro_custo']) + + if (!response || response.length == 0) { + console.log('Empty result from hit portal monitoring. Centro_de_custo: ', req.body['centro_custo']) + return res.status(200).json({ "message": "Ok" }); + } + + const botInfo = await BotIsOnQueue('botqueue') + + let ticket = await ShowTicketServiceByContactId(contact['contact.id']) + + if (ticket.id && ticket.status == 'pending') { + + await sendMessageHitMonitoring(response, ticket, contact); + + } + else if (!ticket.id) { + + ticket = await CreateTicketService({ contactId: contact['contact.id'], status: 'open', userId: botInfo.userIdBot }); + + console.log('botInfo.botQueueId: ', botInfo.botQueueId) + + await UpdateTicketService({ ticketData: { queueId: botInfo.botQueueId }, ticketId: ticket.id }); + + await sendMessageHitMonitoring(response, ticket, contact); + + } - if (contact) { + } catch (error) { - try { - - let response = await hitPortalMonitoring(req.body['centro_custo']) - - if (!response || response.length == 0) { - console.log('Empty result from hit portal monitoring. Centro_de_custo: ',req.body['centro_custo']) - return res.status(200).json({ "message": "Ok" }); - } - - const botInfo = await BotIsOnQueue('botqueue') - - let ticket = await ShowTicketServiceByContactId(contact['contact.id']) - - if (ticket.id && ticket.status == 'pending') { - - await sendMessageHitMonitoring(response, ticket, contact); + console.log(`Error on try sending the message monitor: `, error) } - else if (!ticket.id) { - - ticket = await CreateTicketService({ contactId: contact['contact.id'], status: 'open', userId: botInfo.userIdBot }); - - console.log('botInfo.botQueueId: ', botInfo.botQueueId) - - await UpdateTicketService({ ticketData: { queueId: botInfo.botQueueId }, ticketId: ticket.id }); - - await sendMessageHitMonitoring(response, ticket, contact); - - } - - - } catch (error) { - - console.log(`Error on try sending the message monitor: `, error) } @@ -89,12 +95,12 @@ export const hit = async (req: Request, res: Response): Promise => { async function sendMessageHitMonitoring(response: any, ticket: Ticket, contact: any) { - if (response && response.length > 0) { + if (response && response.length > 0) { console.log('MESSAGE WILL BE SENT!') await SendWhatsAppMessage({ body: `*Olá. Somos a TI Espaçolaser.*\nIdentificamos em nossos monitoramentos que há um problema na internet da sua loja ${contact['contact.name']} e já estamos resolvendo. Abaixo seguem informações sobre o incidente para que possam acompanhar.\n\n*Situação do chamado na Operadora*\n\n*Incidente*:\n\n ${response[0].header}\n${response[0].body}`, ticket }); - - } + + } } diff --git a/backend/src/helpers/EndpointQuery.ts b/backend/src/helpers/EndpointQuery.ts index f06b0d5..e39caa3 100644 --- a/backend/src/helpers/EndpointQuery.ts +++ b/backend/src/helpers/EndpointQuery.ts @@ -41,7 +41,7 @@ const endPointQuery = async (url: string, method: string, param: string = '') => } } catch (error) { - console.error(error); + console.error(`Erro ao consultar endpoint ${url}: ${error}`); } return response diff --git a/backend/src/services/WbotServices/wbotMessageListener.ts b/backend/src/services/WbotServices/wbotMessageListener.ts index 45f004b..c57ae48 100644 --- a/backend/src/services/WbotServices/wbotMessageListener.ts +++ b/backend/src/services/WbotServices/wbotMessageListener.ts @@ -306,8 +306,9 @@ async function sendDelayedMessages(wbot: Session, ticket: Ticket, contact: Conta let endPointResponse = await endPointQuery(msgAction.actions[1], 'get') console.log('Object.entries(endPointResponse.data).length: ', Object.entries(endPointResponse.data).length) + // console.log('endPointResonse.status: ',typeof(endPointResponse.status)) - if (endPointResponse && Object.entries(endPointResponse.data).length > 0) { + if (endPointResponse && endPointResponse.status == 200 && Object.entries(endPointResponse.data).length > 0) { // endPointResponse.data.categoria = 'ELOS' // endPointResponse.data.categoria = 'INFRAESTRUTURA' @@ -331,7 +332,7 @@ async function sendDelayedMessages(wbot: Session, ticket: Ticket, contact: Conta if (['id_solicitante', 'centro_custo_departamento', 'departamento_do_formulario', 'centro_de_custo_do_departamento_do_formulario'].includes(response[i][0])) continue - + msg_endpoint_response += `*${response[i][0]}*: ${String(response[i][1]).replace(/(<([^>]+)>)/gi, "")}\n` } @@ -450,10 +451,14 @@ async function sendDelayedMessages(wbot: Session, ticket: Ticket, contact: Conta // } } - else if (endPointResponse && Object.entries(endPointResponse.data).length == 0) { + else if (endPointResponse && endPointResponse.status == 200 && Object.entries(endPointResponse.data).length == 0) { botSendMessage(ticket, contact, wbot, `Não existe nenhum chamado para consulta com esse número!\n _Digite *0* para voltar ao menu principal._`) return } + else if (endPointResponse && endPointResponse.status == 500) { + botSendMessage(ticket, contact, wbot, `Houve um erro ao realizar a consulta no sos espacolaser!\n _Digite *0* para voltar ao menu principal._`) + return + } else { botSendMessage(ticket, contact, wbot, `Desculpe, nao foi possível realizar a consulta!\n _Digite *0* para voltar ao menu principal._`) return @@ -1081,7 +1086,7 @@ const handleMessage = async ( } else if (`${err}`.includes('[object Object]')) { - await _restore(whatsapp, 'auto_object_error') + await _restore(whatsapp, 'auto_object_error') }