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

135 lines
2.8 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';
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 []
}