Refatoração do codigo de configuração de tickets

pull/20/head
adriano 2023-08-09 10:24:11 -03:00
parent 9125be43de
commit b3ac76e340
3 changed files with 224 additions and 162 deletions

View File

@ -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
};

View File

@ -7,6 +7,12 @@ import { copyFolder } from "../../helpers/CopyFolder";
import { removeDir } from "../../helpers/DeleteDirectory"; import { removeDir } from "../../helpers/DeleteDirectory";
import path from "path"; import path from "path";
import {
isHoliday,
isOutBusinessTime,
isWeekend
} from "../../helpers/TicketConfig";
import { import {
format as _format, format as _format,
isWithinInterval, isWithinInterval,
@ -608,174 +614,51 @@ const handleMessage = async (msg: any, wbot: any): Promise<void> => {
} }
// MESSAGE TO HOLIDAY // MESSAGE TO HOLIDAY
const holiday = await SettingTicket.findOne({ const holiday: any = await isHoliday();
where: { key: "holiday" }
});
if ( if (holiday.set) {
holiday && if (msg.fromMe && holiday.msg == msg.body) {
holiday.value == "enabled" && console.log("HOLIDAY MESSAGE IGNORED");
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");
return; return;
} }
if (currentDate.fullDate == startTime.fullDate) { botSendMessage(ticket, holiday.msg);
botSendMessage(ticket, holiday.message);
return; return;
} }
}
// MESSAGES TO SATURDAY OR SUNDAY // MESSAGES TO SATURDAY OR SUNDAY
const weekend = await SettingTicket.findOne({ const weekend: any = await isWeekend();
where: { key: "weekend" }
});
if ( if (weekend.set) {
weekend && if (msg.fromMe && weekend.msg == msg.body) {
weekend.value == "enabled" && console.log("WEEKEND MESSAGE IGNORED");
weekend.message?.trim()?.length > 0
) {
if (msg.fromMe && weekend.message.trim() == msg.body.trim()) {
console.log("SATURDAY OR SUNDAY DATE");
return; return;
} }
// Specify your desired timezone botSendMessage(ticket, weekend.msg);
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; 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 // MESSAGE TO BUSINESS TIME
const businessTime: any = await BusinessTime(); const businessTime = await isOutBusinessTime();
if ( if (businessTime.set) {
msg.fromMe && if (msg.fromMe && businessTime.msg == msg.body) {
businessTime && console.log("BUSINESS TIME MESSAGE IGNORED");
!businessTime.isWithinRange &&
businessTime.message.trim() == msg.body.trim()
) {
console.log("BUSINESS TIME OUT");
return; return;
} }
if (!businessTime.isWithinRange && businessTime.message.trim().length > 0) { botSendMessage(ticket, businessTime.msg);
botSendMessage(ticket, businessTime.message); 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);
logger.error(`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) => { const handleMsgAck = async (msg_id: any, ack: any) => {

View File

@ -95,15 +95,11 @@ const ConfigModal = ({ open, onClose, change }) => {
try { try {
const { data } = await api.get('/settings') const { data } = await api.get('/settings')
console.log('data.config: ', data.config)
const outBusinessHours = data.config.find((c) => c.key == "outBusinessHours") const outBusinessHours = data.config.find((c) => c.key == "outBusinessHours")
const ticketExpiration = data.config.find((c) => c.key == "ticketExpiration") const ticketExpiration = data.config.find((c) => c.key == "ticketExpiration")
const saturday = data.config.find((c) => c.key == "saturday") const saturday = data.config.find((c) => c.key == "saturday")
const sunday = data.config.find((c) => c.key == "sunday") 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") const holiday = data.config.find((c) => c.key == "holiday")
setConfig({ setConfig({