WhatsAppSessionController.ts 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. import { Request, Response } from "express";
  2. import { getWbot } from "../libs/wbot";
  3. import ShowWhatsAppService from "../services/WhatsappService/ShowWhatsAppService";
  4. import { StartWhatsAppSession } from "../services/WbotServices/StartWhatsAppSession";
  5. import UpdateWhatsAppService from "../services/WhatsappService/UpdateWhatsAppService";
  6. const store = async (req: Request, res: Response): Promise<Response> => {
  7. const { whatsappId } = req.params;
  8. const { companyId } = req.user;
  9. const whatsapp = await ShowWhatsAppService(whatsappId, companyId);
  10. await StartWhatsAppSession(whatsapp, companyId);
  11. return res.status(200).json({ message: "Starting session." });
  12. };
  13. const update = async (req: Request, res: Response): Promise<Response> => {
  14. const { whatsappId } = req.params;
  15. const { companyId } = req.user;
  16. const { whatsapp } = await UpdateWhatsAppService({
  17. whatsappId,
  18. companyId,
  19. whatsappData: { session: "" }
  20. });
  21. await StartWhatsAppSession(whatsapp, companyId);
  22. return res.status(200).json({ message: "Starting session." });
  23. };
  24. const remove = async (req: Request, res: Response): Promise<Response> => {
  25. const { whatsappId } = req.params;
  26. const { companyId } = req.user;
  27. const whatsapp = await ShowWhatsAppService(whatsappId, companyId);
  28. if (whatsapp.session) {
  29. await whatsapp.update({ status: "DISCONNECTED", session: "" });
  30. const wbot = getWbot(whatsapp.id);
  31. await wbot.logout();
  32. }
  33. return res.status(200).json({ message: "Session disconnected." });
  34. };
  35. export default { store, remove, update };