From fd5d9829e4b4d1f5d83b313e52f96b06e27f11dc Mon Sep 17 00:00:00 2001 From: adriano Date: Wed, 18 Oct 2023 11:32:06 -0300 Subject: [PATCH] =?UTF-8?q?refactor:=20otimiza=C3=A7=C3=A3o=20do=20codigo?= =?UTF-8?q?=20que=20trata=20o=20expediente=20de=20atendimento?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../WbotServices/wbotMessageListener.ts | 103 ++++++++++-------- 1 file changed, 57 insertions(+), 46 deletions(-) diff --git a/backend/src/services/WbotServices/wbotMessageListener.ts b/backend/src/services/WbotServices/wbotMessageListener.ts index b83d9a7..ea7843e 100644 --- a/backend/src/services/WbotServices/wbotMessageListener.ts +++ b/backend/src/services/WbotServices/wbotMessageListener.ts @@ -290,6 +290,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) { @@ -439,26 +448,26 @@ const _clear_lst = () => { const handleMessage = async (msg: any, wbot: any): Promise => { // if (!msg.fromMe) { - _clear_lst(); + _clear_lst(); - let index = lst.findIndex((x: any) => x.id == msg.id.id); + let index = lst.findIndex((x: any) => x.id == msg.id.id); - console.log("INDEX: ", index); + console.log("INDEX: ", index); - if (index == -1) { - // console.log('-----------------> LST: ', lst):q + if (index == -1) { + // console.log('-----------------> LST: ', lst):q - lst.push({ id: msg.id.id }); + lst.push({ id: msg.id.id }); - setWhatsappId(msg.id.id); - } else { - console.log("IGNORED ID: ", msg.id.id); + setWhatsappId(msg.id.id); + } else { + console.log("IGNORED ID: ", msg.id.id); - return; - } + return; + } + + // console.log('LIST OF ID MESSAGE lst: ', lst) - // console.log('LIST OF ID MESSAGE lst: ', lst) - // } if (!isValidMsg(msg)) { @@ -628,43 +637,18 @@ const handleMessage = async (msg: any, wbot: any): Promise => { 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"); + if (outService.length > 0) { + const { type, msg: msgOutService } = outService[0]; + + if (msg.fromMe && msgOutService == msg.body) { + console.log(`${type} message ignored`); return; } - botSendMessage(ticket, holiday.msg); - 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); + botSendMessage(ticket, msgOutService); return; } } @@ -723,4 +707,31 @@ 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, handleMsgAck, lst };