2022-01-06 01:26:15 +00:00
|
|
|
import { Request, Response } from "express";
|
|
|
|
import { getWbot } from "../libs/wbot";
|
2022-02-14 01:39:33 +00:00
|
|
|
import { removeDir } from "../helpers/DeleteDirectory";
|
2022-01-06 01:26:15 +00:00
|
|
|
import ShowWhatsAppService from "../services/WhatsappService/ShowWhatsAppService";
|
|
|
|
import { StartWhatsAppSession } from "../services/WbotServices/StartWhatsAppSession";
|
|
|
|
import UpdateWhatsAppService from "../services/WhatsappService/UpdateWhatsAppService";
|
|
|
|
|
2022-06-27 06:21:04 +00:00
|
|
|
import { restartWhatsSession } from "../helpers/RestartWhatsSession";
|
|
|
|
|
2022-07-19 20:18:36 +00:00
|
|
|
import path from 'path';
|
2022-02-14 01:39:33 +00:00
|
|
|
|
|
|
|
// import { WWebJsw } from "../../WWebJS/session-bd_40"
|
|
|
|
|
2022-01-06 01:26:15 +00:00
|
|
|
const store = async (req: Request, res: Response): Promise<Response> => {
|
|
|
|
const { whatsappId } = req.params;
|
|
|
|
const whatsapp = await ShowWhatsAppService(whatsappId);
|
|
|
|
|
|
|
|
StartWhatsAppSession(whatsapp);
|
|
|
|
|
|
|
|
return res.status(200).json({ message: "Starting session." });
|
|
|
|
};
|
|
|
|
|
|
|
|
const update = async (req: Request, res: Response): Promise<Response> => {
|
|
|
|
const { whatsappId } = req.params;
|
|
|
|
|
|
|
|
const { whatsapp } = await UpdateWhatsAppService({
|
|
|
|
whatsappId,
|
|
|
|
whatsappData: { session: "" }
|
|
|
|
});
|
|
|
|
|
|
|
|
StartWhatsAppSession(whatsapp);
|
|
|
|
|
|
|
|
return res.status(200).json({ message: "Starting session." });
|
|
|
|
};
|
|
|
|
|
2022-06-27 06:21:04 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const restart = async (req: Request, res: Response): Promise<Response> => {
|
2022-07-19 20:18:36 +00:00
|
|
|
|
2022-06-27 06:21:04 +00:00
|
|
|
const { whatsappId } = req.params;
|
|
|
|
|
|
|
|
console.log('FROM REQUEST WHATSAPP ID: ', whatsappId)
|
|
|
|
|
|
|
|
const whatsapp = await ShowWhatsAppService(whatsappId);
|
|
|
|
|
2022-07-12 12:37:34 +00:00
|
|
|
restartWhatsSession(whatsapp, true)
|
2022-06-27 06:21:04 +00:00
|
|
|
|
|
|
|
return res.status(200).json({ message: "Starting session." });
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
2022-01-06 01:26:15 +00:00
|
|
|
const remove = async (req: Request, res: Response): Promise<Response> => {
|
|
|
|
const { whatsappId } = req.params;
|
|
|
|
const whatsapp = await ShowWhatsAppService(whatsappId);
|
|
|
|
|
|
|
|
const wbot = getWbot(whatsapp.id);
|
|
|
|
|
2022-02-14 01:39:33 +00:00
|
|
|
console.log(
|
2022-07-19 20:18:36 +00:00
|
|
|
'Desconectou s whatsapp.id: ', whatsapp.id,
|
|
|
|
' | PATH TO DELETE', path.join(process.cwd(), '.wwebjs_auth', `session-bd_${whatsapp.id}`)
|
|
|
|
)
|
2022-02-14 01:39:33 +00:00
|
|
|
|
2022-04-22 13:35:19 +00:00
|
|
|
//removeDir(path.join(process.cwd(),'.wwebjs_auth', `session-bd_${whatsapp.id}`))
|
2022-02-14 01:39:33 +00:00
|
|
|
|
2022-06-27 06:21:04 +00:00
|
|
|
await wbot.logout();
|
2022-01-06 01:26:15 +00:00
|
|
|
|
|
|
|
return res.status(200).json({ message: "Session disconnected." });
|
|
|
|
};
|
|
|
|
|
2022-06-27 06:21:04 +00:00
|
|
|
export default { store, remove, update, restart };
|