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

41 lines
1.3 KiB
TypeScript
Raw Normal View History

import path from "path";
import { number } from "yup";
import { removeWbot } from "../libs/wbot";
import Whatsapp from "../models/Whatsapp";
import { StartWhatsAppSession } from "../services/WbotServices/StartWhatsAppSession";
import { copyFolder } from "./CopyFolder";
import { removeDir } from "./DeleteDirectory";
const fsPromises = require("fs/promises");
const fs = require('fs')
// Restart session
2022-07-12 12:37:34 +00:00
export const restartWhatsSession = async (whatsapp: Whatsapp, backupSession: boolean = false) => {
console.log('RESTARTING THE whatsapp.id: ', whatsapp.id)
const sourcePath = path.join(__dirname, `../../.wwebjs_auth/sessions/`, `session-bd_${whatsapp.id}`)
const destPath = path.join(__dirname, `../../.wwebjs_auth/`, `session-bd_${whatsapp.id}`)
console.log('================> sourcePath: ', sourcePath)
console.log('================> destPath: ', destPath)
removeWbot(whatsapp.id)
await removeDir(destPath)
if (fs.existsSync(sourcePath)) {
// copy the good session for restars the new session
copyFolder(sourcePath, destPath)
}
else {
console.log('Directory not found to copy: ', sourcePath)
}
console.log('RESTARTING SESSION...')
// await StartWhatsAppSession(whatsapp, backupSession);
setTimeout(() => StartWhatsAppSession(whatsapp, backupSession), 2000);
}