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

164 lines
3.7 KiB
TypeScript
Raw Normal View History

2022-12-14 12:29:42 +00:00
import os from 'os';
import dir from 'path';
import fs from 'fs';
2022-12-22 21:49:44 +00:00
import ShowWhatsAppService from '../services/WhatsappService/ShowWhatsAppService';
import { insertOrUpeateWhatsCache } from './WhatsCache';
import autoRestore from './AutoRestore';
import Whatsapp from '../models/Whatsapp';
import { getIO } from '../libs/socket';
2022-12-14 12:29:42 +00:00
export const setRestoreControll = (obj: object) => {
const restoreInfoFile = dir.join(os.tmpdir(), `restoreInfo.json`);
try {
if (fs.existsSync(restoreInfoFile)) {
if (Array.isArray(obj)) {
fs.writeFileSync(restoreInfoFile, JSON.stringify(obj), "utf8");
}
else {
const restoreInfo = fs.readFileSync(restoreInfoFile, { encoding: 'utf8', flag: 'r' });
let lstRestore: any = JSON.parse(restoreInfo)
lstRestore.push(obj)
fs.writeFileSync(restoreInfoFile, JSON.stringify(lstRestore), "utf8");
}
} else {
console.log('restoreInfo.json file not found! It will be created.');
if (Array.isArray(obj)) {
fs.writeFileSync(restoreInfoFile, JSON.stringify(obj), "utf8");
}
else {
fs.writeFileSync(restoreInfoFile, JSON.stringify([obj]), "utf8");
}
}
} catch (error) {
console.log('There was an error on try to read the restoreInfo.json file: ', error)
}
}
export const shifRestoreControll = () => {
const restoreInfoFile = dir.join(os.tmpdir(), `restoreInfo.json`);
try {
if (fs.existsSync(restoreInfoFile)) {
const restoreInfo = fs.readFileSync(restoreInfoFile, { encoding: 'utf8', flag: 'r' });
let lstRestore: any = JSON.parse(restoreInfo)
let whatsapp: any = lstRestore.shift()
fs.writeFileSync(restoreInfoFile, JSON.stringify(lstRestore), "utf8");
return whatsapp
}
} catch (error) {
console.log('There was an error on try to read the restoreInfo.json file: ', error)
}
return {}
}
export const delRestoreControllFile = () => {
const restoreInfoFile = dir.join(os.tmpdir(), `restoreInfo.json`);
try {
if (fs.existsSync(restoreInfoFile)) {
fs.unlinkSync(restoreInfoFile)
} else {
console.log('restoreInfo.json file not found!');
}
} catch (error) {
console.log('There was an error on try delete the restoreInfo.json file: ', error)
}
}
export const getRestoreControll = () => {
const restoreInfoFile = dir.join(os.tmpdir(), `restoreInfo.json`);
try {
if (fs.existsSync(restoreInfoFile)) {
const restoreInfo = fs.readFileSync(restoreInfoFile, { encoding: 'utf8', flag: 'r' });
let lstRestore: any = JSON.parse(restoreInfo)
return lstRestore
} else {
console.log('restoreInfo.json file not found!');
}
} catch (error) {
console.log('There was an error on try to read the restoreInfo.json file: ', error)
}
return []
2022-12-22 21:49:44 +00:00
}
export const _restore = async (whatsapp: Whatsapp, msg_file_title: string) => {
2022-12-23 16:35:07 +00:00
if (whatsapp.status != 'RESTORING' && whatsapp.status != 'qrcode') {
2022-12-22 21:49:44 +00:00
console.log('THE WHATSAAP ID: ', whatsapp.id, ' WILL BE RESTORED SOON!')
await whatsapp.update({ status: "RESTORING", });
2022-12-22 21:49:44 +00:00
const io = getIO();
io.emit("whatsappSession", {
action: "update",
session: whatsapp
});
await insertOrUpeateWhatsCache(`whatsapp:${whatsapp.id}`, { status: "RESTORING", })
setTimeout(async () => await autoRestore(whatsapp.id, msg_file_title), 95000);
}
}