Merge branch 'update_newtemper' into _dialogflow_omnihit_newtemper

pull/20/head
adriano 2023-08-09 10:33:47 -03:00
commit 1cda5bc25e
3 changed files with 230 additions and 194 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 path from "path";
import {
isHoliday,
isOutBusinessTime,
isWeekend
} from "../../helpers/TicketConfig";
import {
format as _format,
isWithinInterval,
@ -1234,183 +1240,51 @@ const handleMessage = async (msg: any, wbot: any): Promise<void> => {
}
// 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);
botSendMessage(ticket, holiday.msg);
return;
}
}
// MESSAGES TO SATURDAY OR SUNDAY
const weekend = await SettingTicket.findOne({
where: { key: "weekend" }
});
const weekend: any = await isWeekend();
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");
if (weekend.set) {
if (msg.fromMe && weekend.msg == msg.body) {
console.log("WEEKEND MESSAGE IGNORED");
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);
botSendMessage(ticket, weekend.msg);
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();
const businessTime = await isOutBusinessTime();
if (
msg.fromMe &&
businessTime &&
businessTime.value == "enabled" &&
!businessTime.isWithinRange &&
businessTime.message.trim() == msg.body.trim()
) {
console.log("BUSINESS TIME OUT");
if (businessTime.set) {
if (msg.fromMe && businessTime.msg == msg.body) {
console.log("BUSINESS TIME MESSAGE IGNORED");
return;
}
if (
businessTime &&
businessTime.value == "enabled" &&
!businessTime.isWithinRange &&
businessTime.message.trim().length > 0
) {
botSendMessage(ticket, businessTime.message);
botSendMessage(ticket, businessTime.msg);
return;
}
} catch (err) {
Sentry.captureException(err);
console.log("xxxxxxxxxxxxx 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) => {

View File

@ -95,16 +95,12 @@ const ConfigModal = ({ open, onClose, change }) => {
try {
const { data } = await api.get('/settings')
console.log('data.config: ', data.config)
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 holiday = data.config.find((c) => c.key == "holiday")
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 holiday = data.config.find((c) => c.key === "holiday")
setConfig({
startTimeBus: outBusinessHours.startTime,
@ -448,23 +444,6 @@ const ConfigModal = ({ open, onClose, change }) => {
const TimePickerField = ({ field, form, ...other }) => {
const { name, value } = field
const handleChange = (time) => {
form.setFieldValue(name, time, true)
}
return (
<TimePicker
{...other}
name={name}
value={value}
onChange={handleChange}
format="hh:mm a"
/>
)
}
export default React.memo(ConfigModal)