diff --git a/backend/src/helpers/TicketConfig.ts b/backend/src/helpers/TicketConfig.ts new file mode 100644 index 0000000..830646a --- /dev/null +++ b/backend/src/helpers/TicketConfig.ts @@ -0,0 +1,183 @@ +import SettingTicket from "../models/SettingTicket"; +import { splitDateTime } from "./SplitDateTime"; +import { utcToZonedTime, zonedTimeToUtc } from "date-fns-tz"; + +import { + format as _format, + isWithinInterval, + parse, + subMinutes, + isSaturday, + isSunday, + parseISO +} from "date-fns"; +import ptBR from "date-fns/locale/pt-BR"; + +const isHoliday = async () => { + let obj = { set: false, msg: "" }; + + const holiday = await SettingTicket.findOne({ + where: { key: "holiday" } + }); + + if ( + holiday && + holiday.value == "enabled" && + holiday.message?.trim()?.length > 0 + ) { + const startTime = splitDateTime( + new Date( + _format(new Date(holiday.startTime), "yyyy-MM-dd HH:mm:ss", { + locale: ptBR + }) + ) + ); + + const currentDate = splitDateTime( + new Date( + _format(new Date(), "yyyy-MM-dd HH:mm:ss", { + locale: ptBR + }) + ) + ); + + if (currentDate.fullDate == startTime.fullDate) { + obj.set = true; + obj.msg = holiday.message.trim(); + } + } + + return obj; +}; + +const isWeekend = async () => { + let obj = { set: false, msg: "" }; + + const weekend = await SettingTicket.findOne({ + where: { key: "weekend" } + }); + + if ( + weekend && + weekend.value == "enabled" && + weekend.message?.trim()?.length > 0 + ) { + + // Specify your desired timezone + const brazilTimeZone = "America/Sao_Paulo"; + + const currentDateUtc = new Date(); + + // Convert UTC date to Brazil time zone + const currentDate = utcToZonedTime(currentDateUtc, brazilTimeZone); + + // Format the date using the desired format + const formattedDate = _format(currentDate, "yyyy-MM-dd HH:mm:ssXXX"); + + const parsedDate = parseISO(formattedDate); + + // Convert parsed date to Brazil time zone + const localDate = utcToZonedTime(parsedDate, brazilTimeZone); + + // Check if it's Saturday or Sunday + if (isSaturday(localDate)) { + const saturday = await SettingTicket.findOne({ + where: { key: "saturday" } + }); + + if (saturday && saturday.value == "enabled") { + // botSendMessage(ticket, weekend.message); + obj.set = true; + obj.msg = weekend.message; + } + } else if (isSunday(localDate)) { + const sunday = await SettingTicket.findOne({ + where: { key: "sunday" } + }); + + if (sunday && sunday.value == "enabled") { + // botSendMessage(ticket, weekend.message); + obj.set = true; + obj.msg = weekend.message; + } + } + else{ + // obj.set = true; + // obj.msg = weekend.message; + } + + return obj; + } +}; + +async function isOutBusinessTime() { + let obj = { set: false, msg: "" }; + + const outBusinessHours = await SettingTicket.findOne({ + where: { key: "outBusinessHours" } + }); + + let isWithinRange = false; + + if ( + outBusinessHours && + outBusinessHours.value == "enabled" && + outBusinessHours?.message?.trim()?.length > 0 + ) { + const ticketDateTimeUpdate = splitDateTime( + new Date( + _format(new Date(), "yyyy-MM-dd HH:mm:ss", { + locale: ptBR + }) + ) + ); + + const startTime = splitDateTime( + new Date( + _format(new Date(outBusinessHours.startTime), "yyyy-MM-dd HH:mm:ss", { + locale: ptBR + }) + ) + ); + + const endTime = splitDateTime( + new Date( + _format(new Date(outBusinessHours.endTime), "yyyy-MM-dd HH:mm:ss", { + locale: ptBR + }) + ) + ); + + const format = "HH:mm:ss"; + const parsedStartTime = parse( + ticketDateTimeUpdate.fullTime, + format, + new Date() + ); + const parsedEndTime = parse(startTime.fullTime, format, new Date()); + const parsedTimeToCheck = parse(endTime.fullTime, format, new Date()); + const timeInterval = { start: parsedStartTime, end: parsedEndTime }; + + // If the time range spans across different days, handle the date part + if (parsedEndTime < parsedStartTime) { + const nextDay = new Date(parsedStartTime); + nextDay.setDate(nextDay.getDate() + 1); + timeInterval.end = nextDay; + } + + isWithinRange = isWithinInterval(parsedTimeToCheck, timeInterval); + + if (!isWithinRange) { + obj.set = true; + obj.msg = outBusinessHours.message; + } + } + + return obj; +} + +export { + isWeekend, + isHoliday, + isOutBusinessTime +}; diff --git a/backend/src/services/WbotServices/wbotMessageListener.ts b/backend/src/services/WbotServices/wbotMessageListener.ts index d5dfae9..7e812b6 100644 --- a/backend/src/services/WbotServices/wbotMessageListener.ts +++ b/backend/src/services/WbotServices/wbotMessageListener.ts @@ -7,6 +7,12 @@ import { copyFolder } from "../../helpers/CopyFolder"; import { removeDir } from "../../helpers/DeleteDirectory"; import path from "path"; +import { + isHoliday, + isOutBusinessTime, + isWeekend +} from "../../helpers/TicketConfig"; + import { format as _format, isWithinInterval, @@ -608,174 +614,51 @@ const handleMessage = async (msg: any, wbot: any): Promise => { } // MESSAGE TO HOLIDAY - const holiday = await SettingTicket.findOne({ - where: { key: "holiday" } - }); + const holiday: any = await isHoliday(); - if ( - holiday && - holiday.value == "enabled" && - holiday.message?.trim()?.length > 0 - ) { - const startTime = splitDateTime( - new Date( - _format(new Date(holiday.startTime), "yyyy-MM-dd HH:mm:ss", { - locale: ptBR - }) - ) - ); - - const currentDate = splitDateTime( - new Date( - _format(new Date(), "yyyy-MM-dd HH:mm:ss", { - locale: ptBR - }) - ) - ); - - if (msg.fromMe && holiday && holiday.message.trim() == msg.body.trim()) { - console.log("HOLIDAY DAY"); + if (holiday.set) { + if (msg.fromMe && holiday.msg == msg.body) { + console.log("HOLIDAY MESSAGE IGNORED"); return; } - if (currentDate.fullDate == startTime.fullDate) { - botSendMessage(ticket, holiday.message); - return; - } - } - - // MESSAGES TO SATURDAY OR SUNDAY - const weekend = await SettingTicket.findOne({ - where: { key: "weekend" } - }); - - if ( - weekend && - weekend.value == "enabled" && - weekend.message?.trim()?.length > 0 - ) { - if (msg.fromMe && weekend.message.trim() == msg.body.trim()) { - console.log("SATURDAY OR SUNDAY DATE"); - return; - } - - // Specify your desired timezone - const brazilTimeZone = "America/Sao_Paulo"; - - const currentDateUtc = new Date(); - - // Convert UTC date to Brazil time zone - const currentDate = utcToZonedTime(currentDateUtc, brazilTimeZone); - - // Format the date using the desired format - const formattedDate = _format(currentDate, "yyyy-MM-dd HH:mm:ssXXX"); - - const parsedDate = parseISO(formattedDate); - - // Convert parsed date to Brazil time zone - const localDate = utcToZonedTime(parsedDate, brazilTimeZone); - - // Check if it's Saturday or Sunday - if (isSaturday(localDate)) { - const saturday = await SettingTicket.findOne({ - where: { key: "saturday" } - }); - - if (saturday && saturday.value == "enabled") { - botSendMessage(ticket, weekend.message); - return; - } - } else if (isSunday(localDate)) { - const sunday = await SettingTicket.findOne({ - where: { key: "sunday" } - }); - - if (sunday && sunday.value == "enabled") { - botSendMessage(ticket, weekend.message); - return; - } - } - } - - // MESSAGE TO BUSINESS TIME - const businessTime: any = await BusinessTime(); - - if ( - msg.fromMe && - businessTime && - !businessTime.isWithinRange && - businessTime.message.trim() == msg.body.trim() - ) { - console.log("BUSINESS TIME OUT"); + botSendMessage(ticket, holiday.msg); return; } - if (!businessTime.isWithinRange && businessTime.message.trim().length > 0) { - botSendMessage(ticket, businessTime.message); + // MESSAGES TO SATURDAY OR SUNDAY + const weekend: any = await isWeekend(); + + if (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.set) { + if (msg.fromMe && businessTime.msg == msg.body) { + console.log("BUSINESS TIME MESSAGE IGNORED"); + return; + } + + botSendMessage(ticket, businessTime.msg); + return; + } + + } catch (err) { Sentry.captureException(err); console.log("Error handling whatsapp message: Err: ", err); logger.error(`Error handling whatsapp message: Err: ${err}`); - } - - async function BusinessTime() { - const outBusinessHours = await SettingTicket.findOne({ - where: { key: "outBusinessHours" } - }); - - let isWithinRange = false; - - if (outBusinessHours && outBusinessHours.value == "enabled") { - const ticketDateTimeUpdate = splitDateTime( - new Date( - _format(new Date(), "yyyy-MM-dd HH:mm:ss", { - locale: ptBR - }) - ) - ); - - const startTime = splitDateTime( - new Date( - _format(new Date(outBusinessHours.startTime), "yyyy-MM-dd HH:mm:ss", { - locale: ptBR - }) - ) - ); - - const endTime = splitDateTime( - new Date( - _format(new Date(outBusinessHours.endTime), "yyyy-MM-dd HH:mm:ss", { - locale: ptBR - }) - ) - ); - - const format = "HH:mm:ss"; - const parsedStartTime = parse( - ticketDateTimeUpdate.fullTime, - format, - new Date() - ); - const parsedEndTime = parse(startTime.fullTime, format, new Date()); - const parsedTimeToCheck = parse(endTime.fullTime, format, new Date()); - - const timeInterval = { start: parsedStartTime, end: parsedEndTime }; - - // If the time range spans across different days, handle the date part - if (parsedEndTime < parsedStartTime) { - const nextDay = new Date(parsedStartTime); - nextDay.setDate(nextDay.getDate() + 1); - timeInterval.end = nextDay; - } - - isWithinRange = isWithinInterval(parsedTimeToCheck, timeInterval); - - console.log("Is the time within the range?", isWithinRange); - - return { isWithinRange, message: outBusinessHours.message }; - } - } + } + }; const handleMsgAck = async (msg_id: any, ack: any) => { diff --git a/frontend/src/components/ConfigModal/index.js b/frontend/src/components/ConfigModal/index.js index 2187fdc..0f2ef88 100644 --- a/frontend/src/components/ConfigModal/index.js +++ b/frontend/src/components/ConfigModal/index.js @@ -93,17 +93,13 @@ const ConfigModal = ({ open, onClose, change }) => { const fetchSession = async () => { try { - const { data } = await api.get('/settings') - - console.log('data.config: ', data.config) + const { data } = await api.get('/settings') const outBusinessHours = data.config.find((c) => c.key == "outBusinessHours") const ticketExpiration = data.config.find((c) => c.key == "ticketExpiration") const saturday = data.config.find((c) => c.key == "saturday") const sunday = data.config.find((c) => c.key == "sunday") - const weekend = data.config.find((c) => c.key == "weekend") - - + const weekend = data.config.find((c) => c.key == "weekend") const holiday = data.config.find((c) => c.key == "holiday") setConfig({