import os from 'os'; import dir from 'path'; import fs from 'fs'; import ShowWhatsAppService from '../services/WhatsappService/ShowWhatsAppService'; import { insertOrUpeateWhatsCache } from './WhatsCache'; import autoRestore from './AutoRestore'; import Whatsapp from '../models/Whatsapp'; import { getIO } from '../libs/socket'; 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 [] } export const _restore = async (whatsapp: Whatsapp, msg_file_title: string) => { 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); } }