diff --git a/backend/src/controllers/HitController.ts b/backend/src/controllers/HitController.ts index 10f684d..3ba79a8 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 { @@ -41,17 +41,20 @@ export const hit = async (req: Request, res: Response): Promise => { try { - let response = await hitPortalMonitoring(req.body['centro_custo']) + let response = await hitPortalMonitoring(req.body['centro_custo']) - // console.log('RESPONSE: ', response) + 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); + + await sendMessageHitMonitoring(response, ticket, contact); } else if (!ticket.id) { @@ -62,7 +65,7 @@ export const hit = async (req: Request, res: Response): Promise => { await UpdateTicketService({ ticketData: { queueId: botInfo.botQueueId }, ticketId: ticket.id }); - await sendMessageHitMonitoring(response, ticket); + await sendMessageHitMonitoring(response, ticket, contact); } @@ -84,12 +87,14 @@ export const hit = async (req: Request, res: Response): Promise => { return res.status(200).json({ "message": "Ok" }); }; -async function sendMessageHitMonitoring(response: any, ticket: Ticket) { - for (let i = 0; i < response.length; i++) { +async function sendMessageHitMonitoring(response: any, ticket: Ticket, contact: any) { - await SendWhatsAppMessage({ body: `*Situação do chamado na Operadora*\n\n*Incidente*:\n\n ${response[i].header}\n${response[i].body}`, ticket }); + if (response && response.length > 0) { - await new Promise(f => setTimeout(f, 1000)); + 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 }); + + } - } }