73 lines
2.1 KiB
TypeScript
73 lines
2.1 KiB
TypeScript
import { Request, Response } from "express";
|
|
import { getWbot } from "../libs/wbot";
|
|
import { removeDir } from "../helpers/DeleteDirectory";
|
|
import ShowWhatsAppService from "../services/WhatsappService/ShowWhatsAppService";
|
|
import { StartWhatsAppSession } from "../services/WbotServices/StartWhatsAppSession";
|
|
import UpdateWhatsAppService from "../services/WhatsappService/UpdateWhatsAppService";
|
|
|
|
import { restartWhatsSession } from "../helpers/RestartWhatsSession";
|
|
|
|
import path from 'path';
|
|
|
|
// import { WWebJsw } from "../../WWebJS/session-bd_40"
|
|
|
|
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." });
|
|
};
|
|
|
|
|
|
|
|
|
|
const restart = async (req: Request, res: Response): Promise<Response> => {
|
|
|
|
const { whatsappId } = req.params;
|
|
|
|
console.log('FROM REQUEST WHATSAPP ID: ', whatsappId)
|
|
|
|
const whatsapp = await ShowWhatsAppService(whatsappId);
|
|
|
|
restartWhatsSession(whatsapp, true)
|
|
|
|
return res.status(200).json({ message: "Starting session." });
|
|
};
|
|
|
|
|
|
|
|
const remove = async (req: Request, res: Response): Promise<Response> => {
|
|
const { whatsappId } = req.params;
|
|
const whatsapp = await ShowWhatsAppService(whatsappId);
|
|
|
|
const wbot = getWbot(whatsapp.id);
|
|
|
|
console.log(
|
|
'Desconectou s whatsapp.id: ', whatsapp.id,
|
|
' | PATH TO DELETE',path.join(process.cwd(),'.wwebjs_auth', `session-bd_${whatsapp.id}`)
|
|
)
|
|
|
|
//removeDir(path.join(process.cwd(),'.wwebjs_auth', `session-bd_${whatsapp.id}`))
|
|
|
|
await wbot.logout();
|
|
|
|
return res.status(200).json({ message: "Session disconnected." });
|
|
};
|
|
|
|
export default { store, remove, update, restart };
|