2022-01-06 01:26:15 +00:00
|
|
|
import gracefulShutdown from "http-graceful-shutdown";
|
|
|
|
import app from "./app";
|
2023-02-07 15:47:40 +00:00
|
|
|
import { initIO, getIO } from "./libs/socket";
|
2022-01-06 01:26:15 +00:00
|
|
|
import { logger } from "./utils/logger";
|
|
|
|
import { StartAllWhatsAppsSessions } from "./services/WbotServices/StartAllWhatsAppsSessions";
|
2023-02-09 21:09:51 +00:00
|
|
|
import User from "./models/User";
|
|
|
|
import Whatsapp from "./models/Whatsapp";
|
2023-04-17 14:03:50 +00:00
|
|
|
import endPointQuery from "./helpers/old_EndPointQuery";
|
2023-02-10 14:03:23 +00:00
|
|
|
import { cacheSize, flushCache, loadTicketsCache } from "./helpers/TicketCache";
|
|
|
|
import { loadContactsCache } from "./helpers/ContactsCache";
|
2022-11-19 22:34:39 +00:00
|
|
|
|
|
|
|
import "./helpers/CloseBotTickets";
|
2023-02-10 14:03:23 +00:00
|
|
|
import { loadSchedulesCache } from "./helpers/SchedulingNotifyCache";
|
2022-12-14 12:29:42 +00:00
|
|
|
import { delRestoreControllFile } from "./helpers/RestoreControll";
|
2023-08-08 15:09:03 +00:00
|
|
|
import "./helpers/AutoCloseTickets";
|
2023-02-13 23:29:55 +00:00
|
|
|
|
2023-07-28 12:25:13 +00:00
|
|
|
import "./helpers/SchedulingNotifySendMessage";
|
2023-04-08 21:31:37 +00:00
|
|
|
import axios from "axios";
|
2023-07-28 12:25:13 +00:00
|
|
|
import os from "os";
|
|
|
|
import Setting from "./models/Setting";
|
|
|
|
|
|
|
|
import fs from "fs";
|
|
|
|
import dir from "path";
|
|
|
|
import { getSettingValue } from "./helpers/WhaticketSettings";
|
|
|
|
import loadSettings from "./helpers/LoadSettings";
|
2022-10-25 14:16:36 +00:00
|
|
|
|
2022-01-06 01:26:15 +00:00
|
|
|
const server = app.listen(process.env.PORT, () => {
|
|
|
|
logger.info(`Server started on port: ${process.env.PORT}`);
|
|
|
|
});
|
|
|
|
|
2023-02-09 21:14:39 +00:00
|
|
|
// if (global.gc) {
|
|
|
|
// console.log(">> Starting Garbage Collector...");
|
|
|
|
// global.gc();
|
|
|
|
// } else {
|
|
|
|
// console.warn('No GC hook! Start your program as `node --expose-gc file.js`.');
|
|
|
|
// }
|
2023-02-09 21:09:51 +00:00
|
|
|
|
2022-01-06 01:26:15 +00:00
|
|
|
initIO(server);
|
2023-02-07 15:47:40 +00:00
|
|
|
|
2023-01-25 20:44:02 +00:00
|
|
|
// StartAllWhatsAppsSessions();
|
2022-01-06 01:26:15 +00:00
|
|
|
gracefulShutdown(server);
|
2022-03-01 03:27:17 +00:00
|
|
|
|
2023-02-09 21:09:51 +00:00
|
|
|
(async () => {
|
2023-07-28 12:25:13 +00:00
|
|
|
console.log("os.tmpdir(): ", os.tmpdir());
|
2022-11-19 22:34:39 +00:00
|
|
|
|
2023-07-28 12:25:13 +00:00
|
|
|
loadSettings();
|
2022-11-19 22:34:39 +00:00
|
|
|
|
2023-07-28 12:25:13 +00:00
|
|
|
let whatsapps: any = await Whatsapp.findAll({ attributes: ["id", "url"] });
|
2023-02-09 21:09:51 +00:00
|
|
|
|
2023-02-09 21:17:19 +00:00
|
|
|
// console.log('whatsapps: ', whatsapps)
|
2023-02-09 21:09:51 +00:00
|
|
|
|
|
|
|
if (whatsapps && whatsapps.length > 0) {
|
|
|
|
for (let i = 0; i < whatsapps.length; i++) {
|
|
|
|
try {
|
2023-07-28 12:25:13 +00:00
|
|
|
console.log(
|
|
|
|
`API URL: ${whatsapps[i].dataValues.url}/api/connection/status`
|
|
|
|
);
|
2022-10-25 14:16:36 +00:00
|
|
|
|
2023-07-28 12:25:13 +00:00
|
|
|
const response = await axios.get(
|
|
|
|
`${whatsapps[i].dataValues.url}/api/connection/status`,
|
|
|
|
{}
|
|
|
|
);
|
2023-04-08 21:31:37 +00:00
|
|
|
|
2023-07-28 12:25:13 +00:00
|
|
|
console.log(`-------> Response: ${response.data.data}`);
|
2023-02-09 21:09:51 +00:00
|
|
|
|
|
|
|
if (!response) {
|
2023-07-28 12:25:13 +00:00
|
|
|
throw new Error("Response null");
|
2023-02-09 21:09:51 +00:00
|
|
|
}
|
|
|
|
|
2023-07-28 12:25:13 +00:00
|
|
|
if (response.data.data && response.data.data == "CONNECTED") {
|
|
|
|
await whatsapps[i].update({ status: "CONNECTED" });
|
2023-04-08 21:31:37 +00:00
|
|
|
}
|
2023-02-09 21:09:51 +00:00
|
|
|
} catch (error: any) {
|
2023-07-28 12:25:13 +00:00
|
|
|
await whatsapps[i].update({ status: "OPENING" });
|
2023-02-10 14:03:23 +00:00
|
|
|
|
2023-07-28 12:25:13 +00:00
|
|
|
console.log(
|
|
|
|
`There was an error on try acess the api sessions ${whatsapps[i].dataValues.url}`
|
|
|
|
);
|
2023-02-09 21:09:51 +00:00
|
|
|
}
|
|
|
|
|
2023-02-10 12:39:50 +00:00
|
|
|
await new Promise(f => setTimeout(f, 100));
|
2023-02-09 21:09:51 +00:00
|
|
|
}
|
2022-10-25 14:16:36 +00:00
|
|
|
}
|
2022-12-18 16:56:45 +00:00
|
|
|
|
2023-02-10 14:03:23 +00:00
|
|
|
if (process.env.CACHE) {
|
2023-07-28 12:25:13 +00:00
|
|
|
const cacheLength = await cacheSize();
|
2023-02-10 14:03:23 +00:00
|
|
|
|
2023-07-28 12:25:13 +00:00
|
|
|
console.log("cacheSize: ", cacheLength);
|
2023-02-10 14:03:23 +00:00
|
|
|
|
|
|
|
if (cacheLength == 0) {
|
2023-07-28 12:25:13 +00:00
|
|
|
console.log("Loading from cache...");
|
|
|
|
await flushCache();
|
|
|
|
await loadContactsCache();
|
|
|
|
await loadTicketsCache();
|
2023-02-10 14:03:23 +00:00
|
|
|
}
|
|
|
|
|
2023-07-28 12:25:13 +00:00
|
|
|
await loadSchedulesCache();
|
2023-02-10 14:03:23 +00:00
|
|
|
// await loadWhatsappCache()
|
|
|
|
}
|
|
|
|
|
2023-07-28 12:25:13 +00:00
|
|
|
delRestoreControllFile();
|
|
|
|
})();
|
2022-12-18 16:56:45 +00:00
|
|
|
|
2023-02-07 15:47:40 +00:00
|
|
|
setTimeout(async () => {
|
|
|
|
const io = getIO();
|
|
|
|
|
2023-07-28 12:25:13 +00:00
|
|
|
console.log("Triggered socket!");
|
2023-02-07 15:47:40 +00:00
|
|
|
|
2023-07-28 12:25:13 +00:00
|
|
|
let users = await User.findAll({ raw: true, attributes: ["id"] });
|
2023-02-07 15:47:40 +00:00
|
|
|
|
|
|
|
if (users && users.length > 0) {
|
|
|
|
for (let i = 0; i < users.length; i++) {
|
|
|
|
io.emit("reload_page", {
|
|
|
|
action: "update",
|
|
|
|
userId: users[i].id
|
|
|
|
});
|
|
|
|
|
2023-07-28 12:25:13 +00:00
|
|
|
console.log("USER ID: ", users[i].id);
|
2023-02-07 15:47:40 +00:00
|
|
|
|
2023-04-10 12:08:09 +00:00
|
|
|
await new Promise(f => setTimeout(f, 100));
|
2023-02-07 15:47:40 +00:00
|
|
|
}
|
2023-02-09 21:09:51 +00:00
|
|
|
}
|
2023-07-28 12:25:13 +00:00
|
|
|
}, 5000);
|