mirror of
https://github.com/cheveguerra/whaticket-community.git
synced 2026-04-21 05:09:18 +00:00
changed whatsapp controllers to typescript
This commit is contained in:
@@ -1,5 +1,10 @@
|
|||||||
import { Request, Response } from "express";
|
import { Request, Response } from "express";
|
||||||
|
|
||||||
import CreateWhatsAppService from "../services/WhatsappService/CreateWhatsAppService";
|
import CreateWhatsAppService from "../services/WhatsappService/CreateWhatsAppService";
|
||||||
|
import DeleteWhatsApprService from "../services/WhatsappService/DeleteWhatsApprService";
|
||||||
|
import ListWhatsAppsService from "../services/WhatsappService/ListWhatsAppsService";
|
||||||
|
import ShowWhatsAppService from "../services/WhatsappService/ShowWhatsAppService";
|
||||||
|
import UpdateWhatsAppService from "../services/WhatsappService/UpdateWhatsAppService";
|
||||||
// import Yup from "yup";
|
// import Yup from "yup";
|
||||||
// import Whatsapp from "../models/Whatsapp";
|
// import Whatsapp from "../models/Whatsapp";
|
||||||
// import { getIO } from "../libs/socket";
|
// import { getIO } from "../libs/socket";
|
||||||
@@ -7,8 +12,6 @@ import CreateWhatsAppService from "../services/WhatsappService/CreateWhatsAppSer
|
|||||||
// import wbotMessageListener from "../services/wbotMessageListener";
|
// import wbotMessageListener from "../services/wbotMessageListener";
|
||||||
// import wbotMonitor from "../services/wbotMonitor";
|
// import wbotMonitor from "../services/wbotMonitor";
|
||||||
|
|
||||||
import ListWhatsAppsService from "../services/WhatsappService/ListWhatsAppsService";
|
|
||||||
|
|
||||||
export const index = async (req: Request, res: Response): Promise<Response> => {
|
export const index = async (req: Request, res: Response): Promise<Response> => {
|
||||||
const whatsapps = await ListWhatsAppsService();
|
const whatsapps = await ListWhatsAppsService();
|
||||||
|
|
||||||
@@ -47,51 +50,22 @@ export const store = async (req: Request, res: Response): Promise<Response> => {
|
|||||||
return res.status(200).json(whatsapp);
|
return res.status(200).json(whatsapp);
|
||||||
};
|
};
|
||||||
|
|
||||||
// export const show = async (req: Request, res: Response): Promise<Response> => {
|
export const show = async (req: Request, res: Response): Promise<Response> => {
|
||||||
// const { whatsappId } = req.params;
|
const { whatsappId } = req.params;
|
||||||
// const whatsapp = await Whatsapp.findByPk(whatsappId);
|
|
||||||
|
|
||||||
// if (!whatsapp) {
|
const whatsapp = await ShowWhatsAppService(whatsappId);
|
||||||
// return res.status(200).json({ message: "Session not found" });
|
|
||||||
// }
|
|
||||||
|
|
||||||
// return res.status(200).json(whatsapp);
|
return res.status(200).json(whatsapp);
|
||||||
// };
|
};
|
||||||
|
|
||||||
// export const update = async (
|
export const update = async (
|
||||||
// req: Request,
|
req: Request,
|
||||||
// res: Response
|
res: Response
|
||||||
// ): Promise<Response> => {
|
): Promise<Response> => {
|
||||||
// const { whatsappId } = req.params;
|
const { whatsappId } = req.params;
|
||||||
|
const whatsappData = req.body;
|
||||||
// const schema = Yup.object().shape({
|
|
||||||
// name: Yup.string().required().min(2),
|
|
||||||
// default: Yup.boolean()
|
|
||||||
// .required()
|
|
||||||
// .test(
|
|
||||||
// "Check-default",
|
|
||||||
// "Only one default whatsapp is permited",
|
|
||||||
// async value => {
|
|
||||||
// if (value === true) {
|
|
||||||
// const whatsappFound = await Whatsapp.findOne({
|
|
||||||
// where: { default: true }
|
|
||||||
// });
|
|
||||||
// if (whatsappFound) {
|
|
||||||
// return !(whatsappFound.id !== +whatsappId);
|
|
||||||
// } else {
|
|
||||||
// return true;
|
|
||||||
// }
|
|
||||||
// } else return true;
|
|
||||||
// }
|
|
||||||
// )
|
|
||||||
// });
|
|
||||||
|
|
||||||
// try {
|
|
||||||
// await schema.validate(req.body);
|
|
||||||
// } catch (err) {
|
|
||||||
// return res.status(400).json({ error: err.message });
|
|
||||||
// }
|
|
||||||
|
|
||||||
|
const whatsapp = await UpdateWhatsAppService({ whatsappData, whatsappId });
|
||||||
// const io = getIO();
|
// const io = getIO();
|
||||||
|
|
||||||
// const whatsapp = await Whatsapp.findByPk(whatsappId);
|
// const whatsapp = await Whatsapp.findByPk(whatsappId);
|
||||||
@@ -107,23 +81,17 @@ export const store = async (req: Request, res: Response): Promise<Response> => {
|
|||||||
// whatsapp: whatsapp
|
// whatsapp: whatsapp
|
||||||
// });
|
// });
|
||||||
|
|
||||||
// return res.status(200).json({ message: "Whatsapp updated" });
|
return res.status(200).json(whatsapp);
|
||||||
// };
|
};
|
||||||
|
|
||||||
// export const remove = async (
|
export const remove = async (
|
||||||
// req: Request,
|
req: Request,
|
||||||
// res: Response
|
res: Response
|
||||||
// ): Promise<Response> => {
|
): Promise<Response> => {
|
||||||
// const io = getIO();
|
// const io = getIO();
|
||||||
// const { whatsappId } = req.params;
|
const { whatsappId } = req.params;
|
||||||
|
|
||||||
// const whatsapp = await Whatsapp.findByPk(whatsappId);
|
await DeleteWhatsApprService(whatsappId);
|
||||||
|
|
||||||
// if (!whatsapp) {
|
|
||||||
// return res.status(404).json({ message: "Whatsapp not found" });
|
|
||||||
// }
|
|
||||||
|
|
||||||
// await whatsapp.destroy();
|
|
||||||
// removeWbot(whatsapp.id);
|
// removeWbot(whatsapp.id);
|
||||||
|
|
||||||
// io.emit("whatsapp", {
|
// io.emit("whatsapp", {
|
||||||
@@ -131,5 +99,5 @@ export const store = async (req: Request, res: Response): Promise<Response> => {
|
|||||||
// whatsappId: whatsapp.id
|
// whatsappId: whatsapp.id
|
||||||
// });
|
// });
|
||||||
|
|
||||||
// return res.status(200).json({ message: "Whatsapp deleted." });
|
return res.status(200).json({ message: "Whatsapp deleted." });
|
||||||
// };
|
};
|
||||||
|
|||||||
@@ -9,14 +9,14 @@ whatsappRoutes.get("/whatsapp/", isAuth, WhatsAppController.index);
|
|||||||
|
|
||||||
whatsappRoutes.post("/whatsapp/", isAuth, WhatsAppController.store);
|
whatsappRoutes.post("/whatsapp/", isAuth, WhatsAppController.store);
|
||||||
|
|
||||||
// whatsappRoutes.get("/whatsapp/:whatsappId", isAuth, WhatsAppController.show);
|
whatsappRoutes.get("/whatsapp/:whatsappId", isAuth, WhatsAppController.show);
|
||||||
|
|
||||||
// whatsappRoutes.put("/whatsapp/:whatsappId", isAuth, WhatsAppController.update);
|
whatsappRoutes.put("/whatsapp/:whatsappId", isAuth, WhatsAppController.update);
|
||||||
|
|
||||||
// whatsappRoutes.remove(
|
whatsappRoutes.delete(
|
||||||
// "/whatsapp/:whatsappId",
|
"/whatsapp/:whatsappId",
|
||||||
// isAuth,
|
isAuth,
|
||||||
// WhatsAppController.delete
|
WhatsAppController.remove
|
||||||
// );
|
);
|
||||||
|
|
||||||
export default whatsappRoutes;
|
export default whatsappRoutes;
|
||||||
|
|||||||
@@ -0,0 +1,16 @@
|
|||||||
|
import Whatsapp from "../../models/Whatsapp";
|
||||||
|
import AppError from "../../errors/AppError";
|
||||||
|
|
||||||
|
const DeleteWhatsApprService = async (id: string): Promise<void> => {
|
||||||
|
const whatsapp = await Whatsapp.findOne({
|
||||||
|
where: { id }
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!whatsapp) {
|
||||||
|
throw new AppError("No whatsapp found with this ID.", 404);
|
||||||
|
}
|
||||||
|
|
||||||
|
await whatsapp.destroy();
|
||||||
|
};
|
||||||
|
|
||||||
|
export default DeleteWhatsApprService;
|
||||||
18
backend/src/services/WhatsappService/ShowWhatsAppService.ts
Normal file
18
backend/src/services/WhatsappService/ShowWhatsAppService.ts
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
import Whatsapp from "../../models/Whatsapp";
|
||||||
|
import AppError from "../../errors/AppError";
|
||||||
|
|
||||||
|
const ShowWhatsAppService = async (
|
||||||
|
id: string
|
||||||
|
): Promise<Whatsapp | undefined> => {
|
||||||
|
const whatsapp = await Whatsapp.findOne({
|
||||||
|
where: { id }
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!whatsapp) {
|
||||||
|
throw new AppError("No whatsapp found with this ID.", 404);
|
||||||
|
}
|
||||||
|
|
||||||
|
return whatsapp;
|
||||||
|
};
|
||||||
|
|
||||||
|
export default ShowWhatsAppService;
|
||||||
@@ -0,0 +1,66 @@
|
|||||||
|
import * as Yup from "yup";
|
||||||
|
|
||||||
|
import AppError from "../../errors/AppError";
|
||||||
|
import Whatsapp from "../../models/Whatsapp";
|
||||||
|
|
||||||
|
interface WhatsappData {
|
||||||
|
name?: string;
|
||||||
|
status?: string;
|
||||||
|
isDefault?: boolean;
|
||||||
|
}
|
||||||
|
|
||||||
|
interface Request {
|
||||||
|
whatsappData: WhatsappData;
|
||||||
|
whatsappId: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
const UpdateWhatsAppService = async ({
|
||||||
|
whatsappData,
|
||||||
|
whatsappId
|
||||||
|
}: Request): Promise<Whatsapp> => {
|
||||||
|
const schema = Yup.object().shape({
|
||||||
|
name: Yup.string().min(2),
|
||||||
|
default: Yup.boolean().test(
|
||||||
|
"Check-default",
|
||||||
|
"Only one default whatsapp is permited",
|
||||||
|
async value => {
|
||||||
|
if (value === true) {
|
||||||
|
const whatsappFound = await Whatsapp.findOne({
|
||||||
|
where: { default: true }
|
||||||
|
});
|
||||||
|
if (whatsappFound) {
|
||||||
|
return !(whatsappFound.id !== +whatsappId);
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
)
|
||||||
|
});
|
||||||
|
|
||||||
|
const { name, status, isDefault } = whatsappData;
|
||||||
|
|
||||||
|
try {
|
||||||
|
await schema.validate({ name, status, isDefault });
|
||||||
|
} catch (err) {
|
||||||
|
throw new AppError(err.message);
|
||||||
|
}
|
||||||
|
|
||||||
|
const whatsapp = await Whatsapp.findOne({
|
||||||
|
where: { id: whatsappId }
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!whatsapp) {
|
||||||
|
throw new AppError("No whatsapp found with this ID.", 404);
|
||||||
|
}
|
||||||
|
|
||||||
|
await whatsapp.update({
|
||||||
|
name,
|
||||||
|
status,
|
||||||
|
isDefault
|
||||||
|
});
|
||||||
|
|
||||||
|
return whatsapp;
|
||||||
|
};
|
||||||
|
|
||||||
|
export default UpdateWhatsAppService;
|
||||||
Reference in New Issue
Block a user