mirror of
https://github.com/cheveguerra/whaticket-community.git
synced 2026-04-20 04:39:20 +00:00
all routes changed to typescript
This commit is contained in:
@@ -109,7 +109,8 @@ export const store = async (req: Request, res: Response): Promise<Response> => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const ticket: Ticket = await defaultWhatsapp.$create("ticket", req.body);
|
const ticket: Ticket = await defaultWhatsapp.$create("ticket", req.body);
|
||||||
const contact = await ticket.$get("contact");
|
|
||||||
|
await ticket.$get("contact");
|
||||||
|
|
||||||
const wapp = await ticket.$get("whatsapp");
|
const wapp = await ticket.$get("whatsapp");
|
||||||
|
|
||||||
@@ -122,7 +123,7 @@ export const store = async (req: Request, res: Response): Promise<Response> => {
|
|||||||
// ticket: serializaedTicket
|
// ticket: serializaedTicket
|
||||||
// });
|
// });
|
||||||
|
|
||||||
return res.status(200).json({ ticket, contact, wapp, tickets });
|
return res.status(200).json({ ticket });
|
||||||
};
|
};
|
||||||
|
|
||||||
// export const update = (req: Request, res: Response): Promise<Response> => {
|
// export const update = (req: Request, res: Response): Promise<Response> => {
|
||||||
|
|||||||
136
backend/src/controllers/WhatsAppController.ts
Normal file
136
backend/src/controllers/WhatsAppController.ts
Normal file
@@ -0,0 +1,136 @@
|
|||||||
|
import { Request, Response } from "express";
|
||||||
|
import CreateWhatsAppService from "../services/WhatsappService/CreateWhatsAppService";
|
||||||
|
// import Yup from "yup";
|
||||||
|
// import Whatsapp from "../models/Whatsapp";
|
||||||
|
// import { getIO } from "../libs/socket";
|
||||||
|
// import { getWbot, initWbot, removeWbot } from "../libs/wbot";
|
||||||
|
// import wbotMessageListener from "../services/wbotMessageListener";
|
||||||
|
// import wbotMonitor from "../services/wbotMonitor";
|
||||||
|
|
||||||
|
import ListWhatsAppsService from "../services/WhatsappService/ListWhatsAppsService";
|
||||||
|
|
||||||
|
export const index = async (req: Request, res: Response): Promise<Response> => {
|
||||||
|
const whatsapps = await ListWhatsAppsService();
|
||||||
|
|
||||||
|
return res.status(200).json(whatsapps);
|
||||||
|
};
|
||||||
|
|
||||||
|
interface WhatsappData {
|
||||||
|
name: string;
|
||||||
|
status: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export const store = async (req: Request, res: Response): Promise<Response> => {
|
||||||
|
// const io = getIO();
|
||||||
|
|
||||||
|
console.log("aqui");
|
||||||
|
|
||||||
|
const { name, status }: WhatsappData = req.body;
|
||||||
|
|
||||||
|
const whatsapp = await CreateWhatsAppService({ name, status });
|
||||||
|
|
||||||
|
// if (!whatsapp) {
|
||||||
|
// return res.status(400).json({ error: "Cannot create whatsapp session." });
|
||||||
|
// }
|
||||||
|
|
||||||
|
// initWbot(whatsapp)
|
||||||
|
// .then(() => {
|
||||||
|
// wbotMessageListener(whatsapp);
|
||||||
|
// wbotMonitor(whatsapp);
|
||||||
|
// })
|
||||||
|
// .catch(err => console.log(err));
|
||||||
|
|
||||||
|
// io.emit("whatsapp", {
|
||||||
|
// action: "update",
|
||||||
|
// whatsapp: whatsapp
|
||||||
|
// });
|
||||||
|
|
||||||
|
return res.status(200).json(whatsapp);
|
||||||
|
};
|
||||||
|
|
||||||
|
// export const show = async (req: Request, res: Response): Promise<Response> => {
|
||||||
|
// const { whatsappId } = req.params;
|
||||||
|
// const whatsapp = await Whatsapp.findByPk(whatsappId);
|
||||||
|
|
||||||
|
// if (!whatsapp) {
|
||||||
|
// return res.status(200).json({ message: "Session not found" });
|
||||||
|
// }
|
||||||
|
|
||||||
|
// return res.status(200).json(whatsapp);
|
||||||
|
// };
|
||||||
|
|
||||||
|
// export const update = async (
|
||||||
|
// req: Request,
|
||||||
|
// res: Response
|
||||||
|
// ): Promise<Response> => {
|
||||||
|
// const { whatsappId } = req.params;
|
||||||
|
|
||||||
|
// 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 io = getIO();
|
||||||
|
|
||||||
|
// const whatsapp = await Whatsapp.findByPk(whatsappId);
|
||||||
|
|
||||||
|
// if (!whatsapp) {
|
||||||
|
// return res.status(404).json({ message: "Whatsapp not found" });
|
||||||
|
// }
|
||||||
|
|
||||||
|
// await whatsapp.update(req.body);
|
||||||
|
|
||||||
|
// io.emit("whatsapp", {
|
||||||
|
// action: "update",
|
||||||
|
// whatsapp: whatsapp
|
||||||
|
// });
|
||||||
|
|
||||||
|
// return res.status(200).json({ message: "Whatsapp updated" });
|
||||||
|
// };
|
||||||
|
|
||||||
|
// export const remove = async (
|
||||||
|
// req: Request,
|
||||||
|
// res: Response
|
||||||
|
// ): Promise<Response> => {
|
||||||
|
// const io = getIO();
|
||||||
|
// const { whatsappId } = req.params;
|
||||||
|
|
||||||
|
// const whatsapp = await Whatsapp.findByPk(whatsappId);
|
||||||
|
|
||||||
|
// if (!whatsapp) {
|
||||||
|
// return res.status(404).json({ message: "Whatsapp not found" });
|
||||||
|
// }
|
||||||
|
|
||||||
|
// await whatsapp.destroy();
|
||||||
|
// removeWbot(whatsapp.id);
|
||||||
|
|
||||||
|
// io.emit("whatsapp", {
|
||||||
|
// action: "delete",
|
||||||
|
// whatsappId: whatsapp.id
|
||||||
|
// });
|
||||||
|
|
||||||
|
// return res.status(200).json({ message: "Whatsapp deleted." });
|
||||||
|
// };
|
||||||
@@ -9,177 +9,177 @@ const Whatsapp = require("../models/Whatsapp");
|
|||||||
const { getIO } = require("../libs/socket");
|
const { getIO } = require("../libs/socket");
|
||||||
|
|
||||||
exports.index = async (req, res) => {
|
exports.index = async (req, res) => {
|
||||||
const {
|
const {
|
||||||
pageNumber = 1,
|
pageNumber = 1,
|
||||||
status = "",
|
status = "",
|
||||||
date = "",
|
date = "",
|
||||||
searchParam = "",
|
searchParam = "",
|
||||||
showAll,
|
showAll
|
||||||
} = req.query;
|
} = req.query;
|
||||||
|
|
||||||
const userId = req.user.id;
|
const userId = req.user.id;
|
||||||
|
|
||||||
const limit = 20;
|
const limit = 20;
|
||||||
const offset = limit * (pageNumber - 1);
|
const offset = limit * (pageNumber - 1);
|
||||||
|
|
||||||
let includeCondition = [
|
let includeCondition = [
|
||||||
{
|
{
|
||||||
model: Contact,
|
model: Contact,
|
||||||
as: "contact",
|
as: "contact",
|
||||||
attributes: ["name", "number", "profilePicUrl"],
|
attributes: ["name", "number", "profilePicUrl"]
|
||||||
},
|
}
|
||||||
];
|
];
|
||||||
|
|
||||||
let whereCondition = { userId: userId };
|
let whereCondition = { userId: userId };
|
||||||
|
|
||||||
if (showAll === "true") {
|
if (showAll === "true") {
|
||||||
whereCondition = {};
|
whereCondition = {};
|
||||||
}
|
}
|
||||||
|
|
||||||
if (status) {
|
if (status) {
|
||||||
whereCondition = {
|
whereCondition = {
|
||||||
...whereCondition,
|
...whereCondition,
|
||||||
status: status,
|
status: status
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
if (searchParam) {
|
if (searchParam) {
|
||||||
includeCondition = [
|
includeCondition = [
|
||||||
...includeCondition,
|
...includeCondition,
|
||||||
{
|
{
|
||||||
model: Message,
|
model: Message,
|
||||||
as: "messages",
|
as: "messages",
|
||||||
attributes: ["id", "body"],
|
attributes: ["id", "body"],
|
||||||
where: {
|
where: {
|
||||||
body: Sequelize.where(
|
body: Sequelize.where(
|
||||||
Sequelize.fn("LOWER", Sequelize.col("body")),
|
Sequelize.fn("LOWER", Sequelize.col("body")),
|
||||||
"LIKE",
|
"LIKE",
|
||||||
"%" + searchParam.toLowerCase() + "%"
|
"%" + searchParam.toLowerCase() + "%"
|
||||||
),
|
)
|
||||||
},
|
},
|
||||||
required: false,
|
required: false,
|
||||||
duplicating: false,
|
duplicating: false
|
||||||
},
|
}
|
||||||
];
|
];
|
||||||
|
|
||||||
whereCondition = {
|
whereCondition = {
|
||||||
[Sequelize.Op.or]: [
|
[Sequelize.Op.or]: [
|
||||||
{
|
{
|
||||||
"$contact.name$": Sequelize.where(
|
"$contact.name$": Sequelize.where(
|
||||||
Sequelize.fn("LOWER", Sequelize.col("name")),
|
Sequelize.fn("LOWER", Sequelize.col("name")),
|
||||||
"LIKE",
|
"LIKE",
|
||||||
"%" + searchParam.toLowerCase() + "%"
|
"%" + searchParam.toLowerCase() + "%"
|
||||||
),
|
)
|
||||||
},
|
},
|
||||||
{ "$contact.number$": { [Sequelize.Op.like]: `%${searchParam}%` } },
|
{ "$contact.number$": { [Sequelize.Op.like]: `%${searchParam}%` } },
|
||||||
{
|
{
|
||||||
"$message.body$": Sequelize.where(
|
"$message.body$": Sequelize.where(
|
||||||
Sequelize.fn("LOWER", Sequelize.col("body")),
|
Sequelize.fn("LOWER", Sequelize.col("body")),
|
||||||
"LIKE",
|
"LIKE",
|
||||||
"%" + searchParam.toLowerCase() + "%"
|
"%" + searchParam.toLowerCase() + "%"
|
||||||
),
|
)
|
||||||
},
|
}
|
||||||
],
|
]
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
if (date) {
|
if (date) {
|
||||||
whereCondition = {
|
whereCondition = {
|
||||||
...whereCondition,
|
...whereCondition,
|
||||||
createdAt: {
|
createdAt: {
|
||||||
[Sequelize.Op.between]: [
|
[Sequelize.Op.between]: [
|
||||||
startOfDay(parseISO(date)),
|
startOfDay(parseISO(date)),
|
||||||
endOfDay(parseISO(date)),
|
endOfDay(parseISO(date))
|
||||||
],
|
]
|
||||||
},
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
const { count, rows: tickets } = await Ticket.findAndCountAll({
|
const { count, rows: tickets } = await Ticket.findAndCountAll({
|
||||||
where: whereCondition,
|
where: whereCondition,
|
||||||
distinct: true,
|
distinct: true,
|
||||||
include: includeCondition,
|
include: includeCondition,
|
||||||
limit,
|
limit,
|
||||||
offset,
|
offset,
|
||||||
order: [["updatedAt", "DESC"]],
|
order: [["updatedAt", "DESC"]]
|
||||||
});
|
});
|
||||||
|
|
||||||
const hasMore = count > offset + tickets.length;
|
const hasMore = count > offset + tickets.length;
|
||||||
|
|
||||||
return res.status(200).json({ count, tickets, hasMore });
|
return res.status(200).json({ count, tickets, hasMore });
|
||||||
};
|
};
|
||||||
|
|
||||||
exports.store = async (req, res) => {
|
exports.store = async (req, res) => {
|
||||||
const io = getIO();
|
const io = getIO();
|
||||||
|
|
||||||
const defaultWhatsapp = await Whatsapp.findOne({
|
const defaultWhatsapp = await Whatsapp.findOne({
|
||||||
where: { default: true },
|
where: { default: true }
|
||||||
});
|
});
|
||||||
|
|
||||||
if (!defaultWhatsapp) {
|
if (!defaultWhatsapp) {
|
||||||
return res
|
return res
|
||||||
.status(404)
|
.status(404)
|
||||||
.json({ error: "No default WhatsApp found. Check Connection page." });
|
.json({ error: "No default WhatsApp found. Check Connection page." });
|
||||||
}
|
}
|
||||||
|
|
||||||
const ticket = await defaultWhatsapp.createTicket(req.body);
|
const ticket = await defaultWhatsapp.createTicket(req.body);
|
||||||
|
|
||||||
const contact = await ticket.getContact();
|
const contact = await ticket.getContact();
|
||||||
|
|
||||||
const serializaedTicket = { ...ticket.dataValues, contact: contact };
|
const serializaedTicket = { ...ticket.dataValues, contact: contact };
|
||||||
|
|
||||||
io.to("notification").emit("ticket", {
|
io.to("notification").emit("ticket", {
|
||||||
action: "create",
|
action: "create",
|
||||||
ticket: serializaedTicket,
|
ticket: serializaedTicket
|
||||||
});
|
});
|
||||||
|
|
||||||
return res.status(200).json(ticket);
|
return res.status(200).json(ticket);
|
||||||
};
|
};
|
||||||
|
|
||||||
exports.update = async (req, res) => {
|
exports.update = async (req, res) => {
|
||||||
const io = getIO();
|
const io = getIO();
|
||||||
const { ticketId } = req.params;
|
const { ticketId } = req.params;
|
||||||
|
|
||||||
const ticket = await Ticket.findByPk(ticketId, {
|
const ticket = await Ticket.findByPk(ticketId, {
|
||||||
include: [
|
include: [
|
||||||
{
|
{
|
||||||
model: Contact,
|
model: Contact,
|
||||||
as: "contact",
|
as: "contact",
|
||||||
attributes: ["name", "number", "profilePicUrl"],
|
attributes: ["name", "number", "profilePicUrl"]
|
||||||
},
|
}
|
||||||
],
|
]
|
||||||
});
|
});
|
||||||
|
|
||||||
if (!ticket) {
|
if (!ticket) {
|
||||||
return res.status(404).json({ error: "No ticket found with this ID" });
|
return res.status(404).json({ error: "No ticket found with this ID" });
|
||||||
}
|
}
|
||||||
|
|
||||||
await ticket.update(req.body);
|
await ticket.update(req.body);
|
||||||
|
|
||||||
io.to("notification").emit("ticket", {
|
io.to("notification").emit("ticket", {
|
||||||
action: "updateStatus",
|
action: "updateStatus",
|
||||||
ticket: ticket,
|
ticket: ticket
|
||||||
});
|
});
|
||||||
|
|
||||||
return res.status(200).json(ticket);
|
return res.status(200).json(ticket);
|
||||||
};
|
};
|
||||||
|
|
||||||
exports.delete = async (req, res) => {
|
exports.delete = async (req, res) => {
|
||||||
const io = getIO();
|
const io = getIO();
|
||||||
const { ticketId } = req.params;
|
const { ticketId } = req.params;
|
||||||
|
|
||||||
const ticket = await Ticket.findByPk(ticketId);
|
const ticket = await Ticket.findByPk(ticketId);
|
||||||
|
|
||||||
if (!ticket) {
|
if (!ticket) {
|
||||||
return res.status(400).json({ error: "No ticket found with this ID" });
|
return res.status(400).json({ error: "No ticket found with this ID" });
|
||||||
}
|
}
|
||||||
|
|
||||||
await ticket.destroy();
|
await ticket.destroy();
|
||||||
|
|
||||||
io.to("notification").emit("ticket", {
|
io.to("notification").emit("ticket", {
|
||||||
action: "delete",
|
action: "delete",
|
||||||
ticketId: ticket.id,
|
ticketId: ticket.id
|
||||||
});
|
});
|
||||||
|
|
||||||
return res.status(200).json({ message: "ticket deleted" });
|
return res.status(200).json({ message: "ticket deleted" });
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -6,136 +6,136 @@ const wbotMessageListener = require("../services/wbotMessageListener");
|
|||||||
const wbotMonitor = require("../services/wbotMonitor");
|
const wbotMonitor = require("../services/wbotMonitor");
|
||||||
|
|
||||||
exports.index = async (req, res) => {
|
exports.index = async (req, res) => {
|
||||||
const whatsapps = await Whatsapp.findAll();
|
const whatsapps = await Whatsapp.findAll();
|
||||||
|
|
||||||
return res.status(200).json(whatsapps);
|
return res.status(200).json(whatsapps);
|
||||||
};
|
};
|
||||||
|
|
||||||
exports.store = async (req, res) => {
|
exports.store = async (req, res) => {
|
||||||
const schema = Yup.object().shape({
|
const schema = Yup.object().shape({
|
||||||
name: Yup.string().required().min(2),
|
name: Yup.string().required().min(2),
|
||||||
default: Yup.boolean()
|
default: Yup.boolean()
|
||||||
.required()
|
.required()
|
||||||
.test(
|
.test(
|
||||||
"Check-default",
|
"Check-default",
|
||||||
"Only one default whatsapp is permited",
|
"Only one default whatsapp is permited",
|
||||||
async value => {
|
async value => {
|
||||||
if (value === true) {
|
if (value === true) {
|
||||||
const whatsappFound = await Whatsapp.findOne({
|
const whatsappFound = await Whatsapp.findOne({
|
||||||
where: { default: true },
|
where: { default: true }
|
||||||
});
|
});
|
||||||
return !Boolean(whatsappFound);
|
return !Boolean(whatsappFound);
|
||||||
} else return true;
|
} else return true;
|
||||||
}
|
}
|
||||||
),
|
)
|
||||||
});
|
});
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await schema.validate(req.body);
|
await schema.validate(req.body);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
return res.status(400).json({ error: err.message });
|
return res.status(400).json({ error: err.message });
|
||||||
}
|
}
|
||||||
|
|
||||||
const io = getIO();
|
const io = getIO();
|
||||||
|
|
||||||
const whatsapp = await Whatsapp.create(req.body);
|
const whatsapp = await Whatsapp.create(req.body);
|
||||||
|
|
||||||
if (!whatsapp) {
|
if (!whatsapp) {
|
||||||
return res.status(400).json({ error: "Cannot create whatsapp session." });
|
return res.status(400).json({ error: "Cannot create whatsapp session." });
|
||||||
}
|
}
|
||||||
|
|
||||||
initWbot(whatsapp)
|
initWbot(whatsapp)
|
||||||
.then(() => {
|
.then(() => {
|
||||||
wbotMessageListener(whatsapp);
|
wbotMessageListener(whatsapp);
|
||||||
wbotMonitor(whatsapp);
|
wbotMonitor(whatsapp);
|
||||||
})
|
})
|
||||||
.catch(err => console.log(err));
|
.catch(err => console.log(err));
|
||||||
|
|
||||||
io.emit("whatsapp", {
|
io.emit("whatsapp", {
|
||||||
action: "update",
|
action: "update",
|
||||||
whatsapp: whatsapp,
|
whatsapp: whatsapp
|
||||||
});
|
});
|
||||||
|
|
||||||
return res.status(200).json(whatsapp);
|
return res.status(200).json(whatsapp);
|
||||||
};
|
};
|
||||||
|
|
||||||
exports.show = async (req, res) => {
|
exports.show = async (req, res) => {
|
||||||
const { whatsappId } = req.params;
|
const { whatsappId } = req.params;
|
||||||
const whatsapp = await Whatsapp.findByPk(whatsappId);
|
const whatsapp = await Whatsapp.findByPk(whatsappId);
|
||||||
|
|
||||||
if (!whatsapp) {
|
if (!whatsapp) {
|
||||||
return res.status(200).json({ message: "Session not found" });
|
return res.status(200).json({ message: "Session not found" });
|
||||||
}
|
}
|
||||||
|
|
||||||
return res.status(200).json(whatsapp);
|
return res.status(200).json(whatsapp);
|
||||||
};
|
};
|
||||||
|
|
||||||
exports.update = async (req, res) => {
|
exports.update = async (req, res) => {
|
||||||
const { whatsappId } = req.params;
|
const { whatsappId } = req.params;
|
||||||
|
|
||||||
const schema = Yup.object().shape({
|
const schema = Yup.object().shape({
|
||||||
name: Yup.string().required().min(2),
|
name: Yup.string().required().min(2),
|
||||||
default: Yup.boolean()
|
default: Yup.boolean()
|
||||||
.required()
|
.required()
|
||||||
.test(
|
.test(
|
||||||
"Check-default",
|
"Check-default",
|
||||||
"Only one default whatsapp is permited",
|
"Only one default whatsapp is permited",
|
||||||
async value => {
|
async value => {
|
||||||
if (value === true) {
|
if (value === true) {
|
||||||
const whatsappFound = await Whatsapp.findOne({
|
const whatsappFound = await Whatsapp.findOne({
|
||||||
where: { default: true },
|
where: { default: true }
|
||||||
});
|
});
|
||||||
if (whatsappFound) {
|
if (whatsappFound) {
|
||||||
return !(whatsappFound.id !== +whatsappId);
|
return !(whatsappFound.id !== +whatsappId);
|
||||||
} else {
|
} else {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
} else return true;
|
} else return true;
|
||||||
}
|
}
|
||||||
),
|
)
|
||||||
});
|
});
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await schema.validate(req.body);
|
await schema.validate(req.body);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
return res.status(400).json({ error: err.message });
|
return res.status(400).json({ error: err.message });
|
||||||
}
|
}
|
||||||
|
|
||||||
const io = getIO();
|
const io = getIO();
|
||||||
|
|
||||||
const whatsapp = await Whatsapp.findByPk(whatsappId);
|
const whatsapp = await Whatsapp.findByPk(whatsappId);
|
||||||
|
|
||||||
if (!whatsapp) {
|
if (!whatsapp) {
|
||||||
return res.status(404).json({ message: "Whatsapp not found" });
|
return res.status(404).json({ message: "Whatsapp not found" });
|
||||||
}
|
}
|
||||||
|
|
||||||
await whatsapp.update(req.body);
|
await whatsapp.update(req.body);
|
||||||
|
|
||||||
io.emit("whatsapp", {
|
io.emit("whatsapp", {
|
||||||
action: "update",
|
action: "update",
|
||||||
whatsapp: whatsapp,
|
whatsapp: whatsapp
|
||||||
});
|
});
|
||||||
|
|
||||||
return res.status(200).json({ message: "Whatsapp updated" });
|
return res.status(200).json({ message: "Whatsapp updated" });
|
||||||
};
|
};
|
||||||
|
|
||||||
exports.delete = async (req, res) => {
|
exports.delete = async (req, res) => {
|
||||||
const io = getIO();
|
const io = getIO();
|
||||||
const { whatsappId } = req.params;
|
const { whatsappId } = req.params;
|
||||||
|
|
||||||
const whatsapp = await Whatsapp.findByPk(whatsappId);
|
const whatsapp = await Whatsapp.findByPk(whatsappId);
|
||||||
|
|
||||||
if (!whatsapp) {
|
if (!whatsapp) {
|
||||||
return res.status(404).json({ message: "Whatsapp not found" });
|
return res.status(404).json({ message: "Whatsapp not found" });
|
||||||
}
|
}
|
||||||
|
|
||||||
await whatsapp.destroy();
|
await whatsapp.destroy();
|
||||||
removeWbot(whatsapp.id);
|
removeWbot(whatsapp.id);
|
||||||
|
|
||||||
io.emit("whatsapp", {
|
io.emit("whatsapp", {
|
||||||
action: "delete",
|
action: "delete",
|
||||||
whatsappId: whatsapp.id,
|
whatsappId: whatsapp.id
|
||||||
});
|
});
|
||||||
|
|
||||||
return res.status(200).json({ message: "Whatsapp deleted." });
|
return res.status(200).json({ message: "Whatsapp deleted." });
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -5,10 +5,8 @@ import authRoutes from "./authRoutes";
|
|||||||
import settingRoutes from "./settingRoutes";
|
import settingRoutes from "./settingRoutes";
|
||||||
import contactRoutes from "./contactRoutes";
|
import contactRoutes from "./contactRoutes";
|
||||||
import ticketRoutes from "./ticketRoutes";
|
import ticketRoutes from "./ticketRoutes";
|
||||||
|
import whatsappRoutes from "./whatsappRoutes";
|
||||||
// const MessagesRoutes = require("./routes/messages");
|
import messageRoutes from "./messageRoutes";
|
||||||
// const WhatsRoutes = require("./routes/whatsapp");
|
|
||||||
// const UsersRoutes = require("./routes/users");
|
|
||||||
|
|
||||||
const routes = Router();
|
const routes = Router();
|
||||||
|
|
||||||
@@ -17,7 +15,7 @@ routes.use("/auth", authRoutes);
|
|||||||
routes.use(settingRoutes);
|
routes.use(settingRoutes);
|
||||||
routes.use(contactRoutes);
|
routes.use(contactRoutes);
|
||||||
routes.use(ticketRoutes);
|
routes.use(ticketRoutes);
|
||||||
// routes.use(MessagesRoutes);
|
routes.use(whatsappRoutes);
|
||||||
// routes.use(WhatsRoutes);
|
routes.use(messageRoutes);
|
||||||
|
|
||||||
export default routes;
|
export default routes;
|
||||||
|
|||||||
12
backend/src/routes/messageRoutes.ts
Normal file
12
backend/src/routes/messageRoutes.ts
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
import { Router } from "express";
|
||||||
|
// import isAuth from "../middleware/isAuth";
|
||||||
|
|
||||||
|
// import { index, store } from "../controllers/MessageController";
|
||||||
|
|
||||||
|
const messageRoutes = Router();
|
||||||
|
|
||||||
|
// messageRoutes.get("/messages/:ticketId", isAuth, index);
|
||||||
|
|
||||||
|
// messageRoutes.post("/messages/:ticketId", isAuth, store);
|
||||||
|
|
||||||
|
export default messageRoutes;
|
||||||
@@ -1,12 +0,0 @@
|
|||||||
const express = require("express");
|
|
||||||
const isAuth = require("../../middleware/is-auth");
|
|
||||||
|
|
||||||
const MessageController = require("../../controllers/MessageController");
|
|
||||||
|
|
||||||
const routes = express.Router();
|
|
||||||
|
|
||||||
routes.get("/messages/:ticketId", isAuth, MessageController.index);
|
|
||||||
|
|
||||||
routes.post("/messages/:ticketId", isAuth, MessageController.store);
|
|
||||||
|
|
||||||
module.exports = routes;
|
|
||||||
@@ -1,18 +0,0 @@
|
|||||||
const express = require("express");
|
|
||||||
const isAuth = require("../../middleware/is-auth");
|
|
||||||
|
|
||||||
const WhatsAppController = require("../../controllers/WhatsAppController");
|
|
||||||
|
|
||||||
const routes = express.Router();
|
|
||||||
|
|
||||||
routes.get("/whatsapp/", isAuth, WhatsAppController.index);
|
|
||||||
|
|
||||||
routes.post("/whatsapp/", isAuth, WhatsAppController.store);
|
|
||||||
|
|
||||||
routes.get("/whatsapp/:whatsappId", isAuth, WhatsAppController.show);
|
|
||||||
|
|
||||||
routes.put("/whatsapp/:whatsappId", isAuth, WhatsAppController.update);
|
|
||||||
|
|
||||||
routes.delete("/whatsapp/:whatsappId", isAuth, WhatsAppController.delete);
|
|
||||||
|
|
||||||
module.exports = routes;
|
|
||||||
22
backend/src/routes/whatsappRoutes.ts
Normal file
22
backend/src/routes/whatsappRoutes.ts
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
import express from "express";
|
||||||
|
import isAuth from "../middleware/isAuth";
|
||||||
|
|
||||||
|
import * as WhatsAppController from "../controllers/WhatsAppController";
|
||||||
|
|
||||||
|
const whatsappRoutes = express.Router();
|
||||||
|
|
||||||
|
whatsappRoutes.get("/whatsapp/", isAuth, WhatsAppController.index);
|
||||||
|
|
||||||
|
whatsappRoutes.post("/whatsapp/", isAuth, WhatsAppController.store);
|
||||||
|
|
||||||
|
// whatsappRoutes.get("/whatsapp/:whatsappId", isAuth, WhatsAppController.show);
|
||||||
|
|
||||||
|
// whatsappRoutes.put("/whatsapp/:whatsappId", isAuth, WhatsAppController.update);
|
||||||
|
|
||||||
|
// whatsappRoutes.remove(
|
||||||
|
// "/whatsapp/:whatsappId",
|
||||||
|
// isAuth,
|
||||||
|
// WhatsAppController.delete
|
||||||
|
// );
|
||||||
|
|
||||||
|
export default whatsappRoutes;
|
||||||
@@ -0,0 +1,47 @@
|
|||||||
|
import * as Yup from "yup";
|
||||||
|
|
||||||
|
import AppError from "../../errors/AppError";
|
||||||
|
import Whatsapp from "../../models/Whatsapp";
|
||||||
|
|
||||||
|
interface Request {
|
||||||
|
name: string;
|
||||||
|
status?: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
const CreateWhatsAppService = async ({
|
||||||
|
name,
|
||||||
|
status = "INITIALIZING"
|
||||||
|
}: Request): Promise<Whatsapp> => {
|
||||||
|
// 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 }
|
||||||
|
// });
|
||||||
|
// return !Boolean(whatsappFound);
|
||||||
|
// } else return true;
|
||||||
|
// }
|
||||||
|
// )
|
||||||
|
// });
|
||||||
|
|
||||||
|
// try {
|
||||||
|
// await schema.validate({ name, status });
|
||||||
|
// } catch (err) {
|
||||||
|
// throw new AppError(err.message);
|
||||||
|
// }
|
||||||
|
|
||||||
|
const whatsapp = await Whatsapp.create({
|
||||||
|
name,
|
||||||
|
status
|
||||||
|
});
|
||||||
|
|
||||||
|
return whatsapp;
|
||||||
|
};
|
||||||
|
|
||||||
|
export default CreateWhatsAppService;
|
||||||
13
backend/src/services/WhatsappService/ListWhatsAppsService.ts
Normal file
13
backend/src/services/WhatsappService/ListWhatsAppsService.ts
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
import Whatsapp from "../../models/Whatsapp";
|
||||||
|
|
||||||
|
interface Response {
|
||||||
|
whatsapps: Whatsapp[];
|
||||||
|
}
|
||||||
|
|
||||||
|
const ListWhatsAppsService = async (): Promise<Response> => {
|
||||||
|
const whatsapps = await Whatsapp.findAll();
|
||||||
|
|
||||||
|
return { whatsapps };
|
||||||
|
};
|
||||||
|
|
||||||
|
export default ListWhatsAppsService;
|
||||||
Reference in New Issue
Block a user