From edbb565f62a195f21b14855fdec8e6735e793677 Mon Sep 17 00:00:00 2001 From: adriano Date: Wed, 18 Oct 2023 11:57:36 -0300 Subject: [PATCH] =?UTF-8?q?refactor:=20Otimiza=C3=A7=C3=A3o=20do=20codigo?= =?UTF-8?q?=20responsavel=20por=20controlar=20o=20horario=20de=20expedient?= =?UTF-8?q?e?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../WbotServices/wbotMessageListener.ts | 86 +++++++++++-------- 1 file changed, 49 insertions(+), 37 deletions(-) diff --git a/backend/src/services/WbotServices/wbotMessageListener.ts b/backend/src/services/WbotServices/wbotMessageListener.ts index e4b74f6..59f971b 100644 --- a/backend/src/services/WbotServices/wbotMessageListener.ts +++ b/backend/src/services/WbotServices/wbotMessageListener.ts @@ -347,6 +347,15 @@ const verifyQueue = async ( } // + const outService = await outOfService(); + + if (outService.length > 0) { + const { type, msg: msgOutService } = outService[0]; + console.log(`${type} message ignored on queue`); + botSendMessage(ticket, msgOutService); + return; + } + let body = ""; if (botOptions.length > 0) { @@ -685,46 +694,22 @@ const handleMessage = async ( ticketHasQueue = true; } - if (ticketHasQueue) { - // MESSAGE TO HOLIDAY - const holiday: any = await isHoliday(); + if (ticketHasQueue && ticket.status != "open") { + const outService = await outOfService(); - if (holiday && holiday.set) { - if (msg.fromMe && holiday.msg == msg.body) { - console.log("HOLIDAY MESSAGE IGNORED"); - return; - } + if (outService.length > 0) { + const { type, msg: msgOutService } = outService[0]; - botSendMessage(ticket, holiday.msg); - return; - } + if (msg.fromMe && msgOutService == msg.body) { + console.log(`${type} message ignored`); + return; + } - // MESSAGES TO SATURDAY OR SUNDAY - const weekend: any = await isWeekend(); - - if (weekend && weekend.set) { - if (msg.fromMe && weekend.msg == msg.body) { - console.log("WEEKEND MESSAGE IGNORED"); - return; - } - - botSendMessage(ticket, weekend.msg); - return; - } - - // MESSAGE TO BUSINESS TIME - const businessTime = await isOutBusinessTime(); - - if (businessTime && businessTime.set) { - if (msg.fromMe && businessTime.msg == msg.body) { - console.log("BUSINESS TIME MESSAGE IGNORED"); - return; - } - - botSendMessage(ticket, businessTime.msg); - return; - } - } + botSendMessage(ticket, msgOutService); + return; + } + } + } catch (err) { Sentry.captureException(err); console.log("Error handling whatsapp message: Err: ", err); @@ -793,6 +778,33 @@ const wbotMessageListener = (wbot: Session): void => { }); }; +const outOfService = async () => { + // MESSAGE TO HOLIDAY + const holiday: any = await isHoliday(); + + let objs: any = []; + + if (holiday && holiday.set) { + objs.push({ type: "holiday", msg: holiday.msg }); + } + + // MESSAGES TO SATURDAY OR SUNDAY + const weekend: any = await isWeekend(); + + if (weekend && weekend.set) { + objs.push({ type: "weekend", msg: weekend.msg }); + } + + // MESSAGE TO BUSINESS TIME + const businessTime = await isOutBusinessTime(); + + if (businessTime && businessTime.set) { + objs.push({ type: "businessTime", msg: businessTime.msg }); + } + + return objs; +}; + export { wbotMessageListener, handleMessage,