projeto-hit/backend/src/helpers/SchedulingNotifySendMessage.ts

76 lines
2.1 KiB
TypeScript
Raw Normal View History

import SetTicketMessagesAsRead from "../helpers/SetTicketMessagesAsRead";
import ShowTicketService from "../services/TicketServices/ShowTicketService";
import SendWhatsAppMessage from "../services/WbotServices/SendWhatsAppMessage";
import ListSchedulingNotifyService from "../services/SchedulingNotifyServices/ListSchedulingNotifyService";
import DeleteSchedulingNotifyService from "../services/SchedulingNotifyServices/DeleteSchedulingNotifyService";
2022-05-03 21:20:58 +00:00
2022-03-07 10:51:04 +00:00
let scheduler_monitor:any;
2022-05-16 04:03:16 +00:00
let timeInterval = 5
2022-03-07 10:51:04 +00:00
const monitor = async () => {
let date = new Date()
let day = date.getDate().toString().padStart(2, '0');
let month = (date.getMonth()+1).toString().padStart(2, '0');
let year = date.getFullYear();
let hour = date.getHours()
let minute = date.getMinutes()
let fullDate = `${year}-${month}-${day}`;
let dateParm = `${fullDate} ${hour.toString().padStart(2, '0')}:${minute.toString().padStart(2, '0')}:00`
//console.log(dateParm);
try {
const { schedulingNotifies, count, hasMore } = await ListSchedulingNotifyService({ searchParam: dateParm, pageNumber: "1" });
// console.log('schedulingNotifies: ',schedulingNotifies)
if(schedulingNotifies && schedulingNotifies.length>0){
const ticket = await ShowTicketService(schedulingNotifies[0].ticketId);
SetTicketMessagesAsRead(ticket);
await SendWhatsAppMessage({
body: schedulingNotifies[0].message, ticket
});
DeleteSchedulingNotifyService(schedulingNotifies[0].id)
}
} catch (error) {
console.log('>>> SchedulingNotifiySendMessage.ts error: ', error)
2022-05-16 04:03:16 +00:00
stopSchedulingMonitor()
startSchedulingMonitor(timeInterval)
}
2022-03-07 10:51:04 +00:00
};
export const startSchedulingMonitor =async (mileseconds: number) => {
2022-05-16 04:03:16 +00:00
timeInterval = mileseconds
scheduler_monitor = setInterval(monitor, mileseconds)
}
export const stopSchedulingMonitor =async ( ) => {
clearInterval(scheduler_monitor)
}