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

190 lines
5.2 KiB
TypeScript
Raw Normal View History

2022-06-29 00:34:43 +00:00
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";
import { getIO } from "../libs/socket";
import ListWhatsAppsService from "../services/WhatsappService/ListWhatsAppsService";
import path from "path";
import { convertBytes } from "./ConvertBytes";
import { deleteScheduleByTicketIdCache } from "./SchedulingNotifyCache";
import SchedulingNotify from "../models/SchedulingNotify";
import Ticket from "../models/Ticket";
import { Sequelize, Op } from "sequelize";
2022-06-29 00:34:43 +00:00
const fastFolderSize = require('fast-folder-size')
const { promisify } = require('util')
const fs = require('fs')
2022-06-29 00:34:43 +00:00
const { exec } = require("child_process");
let _fifo: any
2022-06-29 00:34:43 +00:00
let scheduler_monitor: any;
2022-05-16 04:03:16 +00:00
let timeInterval = 5
2022-06-29 00:34:43 +00:00
const monitor = async () => {
let date = new Date()
let day = date.getDate().toString().padStart(2, '0');
2022-06-29 00:34:43 +00:00
let month = (date.getMonth() + 1).toString().padStart(2, '0');
let year = date.getFullYear();
let hour = date.getHours()
2022-06-29 00:34:43 +00:00
let minute = date.getMinutes()
2022-06-29 00:34:43 +00:00
let fullDate = `${year}-${month}-${day}`;
let dateParm = `${fullDate} ${hour.toString().padStart(2, '0')}:${minute.toString().padStart(2, '0')}:00`
//console.log(dateParm);
2022-06-29 00:34:43 +00:00
try {
const { schedulingNotifies, count, hasMore } = await ListSchedulingNotifyService({ searchParam: dateParm, pageNumber: "1" });
if (schedulingNotifies && schedulingNotifies.length > 0) {
for (let i = 0; i < schedulingNotifies.length; i++) {
const ticket: any = await ShowTicketService(+schedulingNotifies[i].ticketId);
let _ticket = await Ticket.findOne({
where: {
contactId: ticket.contactId,
status: { [Op.in]: ['open', 'pending'] }
}
})
await deleteScheduleByTicketIdCache(schedulingNotifies[i].ticketId)
await DeleteSchedulingNotifyService(schedulingNotifies[i].id)
2022-06-29 00:34:43 +00:00
if (_ticket) continue
if (ticket.dataValues.status == 'closed') {
await ticket.update({ status: 'pending' })
}
await new Promise(f => setTimeout(f, 3000));
await SendWhatsAppMessage({
body: schedulingNotifies[i].message, ticket
});
2022-06-29 00:34:43 +00:00
}
}
2022-06-29 00:34:43 +00:00
exec("df -h /", (error: any, stdout: any, stderr: any) => {
if (error) {
console.log(`exec error: ${error.message}`);
return;
}
if (stderr) {
console.log(`exec stderr: ${stderr}`);
return;
}
stdout = stdout.split(/\r?\n/)
stdout = stdout[1].trim().split(/\s+/)
// DISK SPACE MONITORING
const io = getIO();
io.emit("diskSpaceMonit", {
action: "update",
diskSpace: {
size: stdout[1],
used: stdout[2],
available: stdout[3],
use: stdout[4]
}
});
});
// WHATS SESSION SIZE MONITORING
// const whatsapps = await ListWhatsAppsService();
2022-06-29 00:34:43 +00:00
// whatsapps.forEach(async whats => {
2022-06-29 00:34:43 +00:00
2022-06-29 00:34:43 +00:00
// const sourcePath = path.join(__dirname, `../../.wwebjs_auth/`, `session-bd_${whats.id}`)
2022-06-29 00:34:43 +00:00
// if (fs.existsSync(sourcePath)) {
// try {
// const fastFolderSizeAsync = promisify(fastFolderSize)
// let size = await fastFolderSizeAsync(sourcePath)
// size = convertBytes(size)
// // SESSION MONITORING
// const io = getIO();
// io.emit("whatsappSessionMonit", {
// action: "update",
// whatsappSessionSize: {
// id: whats.id,
// sessionSize: size
// }
// });
// }
// catch (err) {
// console.log('An error occurred while get info from the directory: ', err);
// }
// }
// else {
// // console.log('Directory not found to get size info: ', sourcePath)
// }
2022-06-29 00:34:43 +00:00
// });
2022-06-29 00:34:43 +00:00
} catch (error) {
console.log('>>> SchedulingNotifiySendMessage.ts error: ', error)
2022-06-29 00:34:43 +00:00
}
};
const SchedulingNotifySendMessage = async () => {
2022-06-29 00:34:43 +00:00
try {
clearInterval(_fifo);
await monitor()
} catch (error) {
console.log('error on SchedulingNotifySendMessage: ', error)
}
finally {
_fifo = setInterval(SchedulingNotifySendMessage, 5000);
}
}
_fifo = setInterval(SchedulingNotifySendMessage, 5000);
2022-06-29 00:34:43 +00:00
module.exports = SchedulingNotifySendMessage