refactor: Otimização do codigo responsavel por controlar o horario de expediente

pull/21/head
adriano 2023-10-18 11:57:36 -03:00
parent 8b6d7e2597
commit edbb565f62
1 changed files with 49 additions and 37 deletions

View File

@ -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 = ""; let body = "";
if (botOptions.length > 0) { if (botOptions.length > 0) {
@ -685,46 +694,22 @@ const handleMessage = async (
ticketHasQueue = true; ticketHasQueue = true;
} }
if (ticketHasQueue) { if (ticketHasQueue && ticket.status != "open") {
// MESSAGE TO HOLIDAY const outService = await outOfService();
const holiday: any = await isHoliday();
if (holiday && holiday.set) { if (outService.length > 0) {
if (msg.fromMe && holiday.msg == msg.body) { const { type, msg: msgOutService } = outService[0];
console.log("HOLIDAY MESSAGE IGNORED");
return;
}
botSendMessage(ticket, holiday.msg); if (msg.fromMe && msgOutService == msg.body) {
return; console.log(`${type} message ignored`);
} return;
}
// MESSAGES TO SATURDAY OR SUNDAY botSendMessage(ticket, msgOutService);
const weekend: any = await isWeekend(); return;
}
}
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;
}
}
} catch (err) { } catch (err) {
Sentry.captureException(err); Sentry.captureException(err);
console.log("Error handling whatsapp message: Err: ", 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 { export {
wbotMessageListener, wbotMessageListener,
handleMessage, handleMessage,