2022-01-06 01:26:15 +00:00
|
|
|
import { Request, Response } from "express";
|
|
|
|
import { getIO } from "../libs/socket";
|
|
|
|
import { removeWbot } from "../libs/wbot";
|
|
|
|
import { StartWhatsAppSession } from "../services/WbotServices/StartWhatsAppSession";
|
|
|
|
|
2022-02-14 01:39:33 +00:00
|
|
|
import { removeDir } from "../helpers/DeleteDirectory";
|
|
|
|
|
2022-01-06 01:26:15 +00:00
|
|
|
import CreateWhatsAppService from "../services/WhatsappService/CreateWhatsAppService";
|
|
|
|
import DeleteWhatsAppService from "../services/WhatsappService/DeleteWhatsAppService";
|
|
|
|
import ListWhatsAppsService from "../services/WhatsappService/ListWhatsAppsService";
|
|
|
|
import ShowWhatsAppService from "../services/WhatsappService/ShowWhatsAppService";
|
|
|
|
import UpdateWhatsAppService from "../services/WhatsappService/UpdateWhatsAppService";
|
|
|
|
|
2022-01-13 17:11:50 +00:00
|
|
|
import AppError from "../errors/AppError";
|
2022-06-29 00:34:43 +00:00
|
|
|
|
2022-02-14 01:39:33 +00:00
|
|
|
import path from 'path';
|
2022-06-29 00:34:43 +00:00
|
|
|
|
2022-02-14 01:39:33 +00:00
|
|
|
|
2022-01-06 01:26:15 +00:00
|
|
|
interface WhatsappData {
|
|
|
|
name: string;
|
|
|
|
queueIds: number[];
|
|
|
|
greetingMessage?: string;
|
|
|
|
farewellMessage?: string;
|
|
|
|
status?: string;
|
|
|
|
isDefault?: boolean;
|
|
|
|
}
|
|
|
|
|
|
|
|
export const index = async (req: Request, res: Response): Promise<Response> => {
|
2022-06-29 00:34:43 +00:00
|
|
|
const whatsapps = await ListWhatsAppsService();
|
|
|
|
|
2022-01-06 01:26:15 +00:00
|
|
|
return res.status(200).json(whatsapps);
|
|
|
|
};
|
|
|
|
|
|
|
|
export const store = async (req: Request, res: Response): Promise<Response> => {
|
|
|
|
const {
|
|
|
|
name,
|
|
|
|
status,
|
|
|
|
isDefault,
|
|
|
|
greetingMessage,
|
|
|
|
farewellMessage,
|
|
|
|
queueIds
|
|
|
|
}: WhatsappData = req.body;
|
|
|
|
|
2022-01-13 17:11:50 +00:00
|
|
|
if (req.user.profile !== "master") {
|
|
|
|
throw new AppError("ERR_NO_PERMISSION", 403);
|
|
|
|
}
|
2022-06-29 00:34:43 +00:00
|
|
|
|
2022-01-06 01:26:15 +00:00
|
|
|
const { whatsapp, oldDefaultWhatsapp } = await CreateWhatsAppService({
|
|
|
|
name,
|
|
|
|
status,
|
|
|
|
isDefault,
|
|
|
|
greetingMessage,
|
|
|
|
farewellMessage,
|
|
|
|
queueIds
|
|
|
|
});
|
|
|
|
|
|
|
|
StartWhatsAppSession(whatsapp);
|
|
|
|
|
|
|
|
const io = getIO();
|
|
|
|
io.emit("whatsapp", {
|
|
|
|
action: "update",
|
|
|
|
whatsapp
|
|
|
|
});
|
|
|
|
|
|
|
|
if (oldDefaultWhatsapp) {
|
|
|
|
io.emit("whatsapp", {
|
|
|
|
action: "update",
|
|
|
|
whatsapp: oldDefaultWhatsapp
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return res.status(200).json(whatsapp);
|
|
|
|
};
|
|
|
|
|
|
|
|
export const show = async (req: Request, res: Response): Promise<Response> => {
|
|
|
|
const { whatsappId } = req.params;
|
|
|
|
|
|
|
|
const whatsapp = await ShowWhatsAppService(whatsappId);
|
2022-06-29 00:34:43 +00:00
|
|
|
|
2022-01-06 01:26:15 +00:00
|
|
|
return res.status(200).json(whatsapp);
|
|
|
|
};
|
|
|
|
|
|
|
|
export const update = async (
|
|
|
|
req: Request,
|
|
|
|
res: Response
|
|
|
|
): Promise<Response> => {
|
|
|
|
const { whatsappId } = req.params;
|
|
|
|
const whatsappData = req.body;
|
|
|
|
|
|
|
|
const { whatsapp, oldDefaultWhatsapp } = await UpdateWhatsAppService({
|
|
|
|
whatsappData,
|
|
|
|
whatsappId
|
|
|
|
});
|
|
|
|
|
|
|
|
const io = getIO();
|
|
|
|
io.emit("whatsapp", {
|
|
|
|
action: "update",
|
|
|
|
whatsapp
|
|
|
|
});
|
|
|
|
|
|
|
|
if (oldDefaultWhatsapp) {
|
|
|
|
io.emit("whatsapp", {
|
|
|
|
action: "update",
|
|
|
|
whatsapp: oldDefaultWhatsapp
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return res.status(200).json(whatsapp);
|
|
|
|
};
|
|
|
|
|
|
|
|
export const remove = async (
|
|
|
|
req: Request,
|
|
|
|
res: Response
|
|
|
|
): Promise<Response> => {
|
2022-01-13 17:11:50 +00:00
|
|
|
|
|
|
|
if (req.user.profile !== "master") {
|
|
|
|
throw new AppError("ERR_NO_PERMISSION", 403);
|
2022-06-29 00:34:43 +00:00
|
|
|
}
|
2022-01-13 17:11:50 +00:00
|
|
|
|
2022-06-29 00:34:43 +00:00
|
|
|
const { whatsappId } = req.params;
|
2022-01-06 01:26:15 +00:00
|
|
|
|
|
|
|
await DeleteWhatsAppService(whatsappId);
|
2022-02-14 10:57:41 +00:00
|
|
|
|
2022-06-29 00:34:43 +00:00
|
|
|
removeDir(path.join(process.cwd(), '.wwebjs_auth', `session-bd_${whatsappId}`))
|
|
|
|
|
2022-01-06 01:26:15 +00:00
|
|
|
removeWbot(+whatsappId);
|
|
|
|
|
2022-02-14 01:39:33 +00:00
|
|
|
console.log('Deleteou o whatsapp service com id = ', whatsappId)
|
|
|
|
|
2022-01-06 01:26:15 +00:00
|
|
|
const io = getIO();
|
|
|
|
io.emit("whatsapp", {
|
|
|
|
action: "delete",
|
|
|
|
whatsappId: +whatsappId
|
2022-06-29 00:34:43 +00:00
|
|
|
});
|
2022-02-14 01:39:33 +00:00
|
|
|
|
2022-01-06 01:26:15 +00:00
|
|
|
return res.status(200).json({ message: "Whatsapp deleted." });
|
|
|
|
};
|