projeto-hit/TEST_SERVER1/api/helpers/sessionCRUD.js

152 lines
3.3 KiB
JavaScript
Raw Permalink Normal View History

import os from 'os';
import dir from 'path';
import fs from 'fs';
export const setJSON = (obj) => {
const sessionControlFile = dir.join(process.cwd(), `sessionsDir.json`);
try {
if (fs.existsSync(sessionControlFile)) {
const sessionsDir = fs.readFileSync(sessionControlFile, { encoding: 'utf8', flag: 'r' });
let lstRestore = JSON.parse(sessionsDir)
lstRestore.push(obj)
fs.writeFileSync(sessionControlFile, JSON.stringify(lstRestore), "utf8");
} else {
console.log('sessionsDir.json file not found! It will be created.');
if (Array.isArray(obj)) {
fs.writeFileSync(sessionControlFile, JSON.stringify(obj), "utf8");
}
else {
fs.writeFileSync(sessionControlFile, JSON.stringify([obj]), "utf8");
}
}
} catch (error) {
console.log('There was an error on try to read the sessionsDir.json file: ', error)
}
}
export const shifRestoreControll = () => {
const sessionControlFile = dir.join(os.tmpdir(), `sessionsDir.json`);
try {
if (fs.existsSync(sessionControlFile)) {
const sessionsDir = fs.readFileSync(sessionControlFile, { encoding: 'utf8', flag: 'r' });
let lstRestore: any = JSON.parse(sessionsDir)
let whatsapp: any = lstRestore.shift()
fs.writeFileSync(sessionControlFile, JSON.stringify(lstRestore), "utf8");
return whatsapp
}
} catch (error) {
console.log('There was an error on try to read the sessionsDir.json file: ', error)
}
return {}
}
export const delRestoreControllFile = () => {
const sessionControlFile = dir.join(os.tmpdir(), `sessionsDir.json`);
try {
if (fs.existsSync(sessionControlFile)) {
fs.unlinkSync(sessionControlFile)
} else {
console.log('sessionsDir.json file not found!');
}
} catch (error) {
console.log('There was an error on try delete the sessionsDir.json file: ', error)
}
}
export const getRestoreControll = () => {
const sessionControlFile = dir.join(os.tmpdir(), `sessionsDir.json`);
try {
if (fs.existsSync(sessionControlFile)) {
const sessionsDir = fs.readFileSync(sessionControlFile, { encoding: 'utf8', flag: 'r' });
let lstRestore: any = JSON.parse(sessionsDir)
return lstRestore
} else {
console.log('sessionsDir.json file not found!');
}
} catch (error) {
console.log('There was an error on try to read the sessionsDir.json file: ', error)
}
return []
}
export const _restore = async (whatsapp: Whatsapp, msg_file_title: string) => {
return
if (whatsapp.status != 'RESTORING' && whatsapp.status != 'qrcode') {
console.log('THE WHATSAAP ID: ', whatsapp.id, ' WILL BE RESTORED SOON!')
await whatsapp.update({ status: "RESTORING", });
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);
}
}