mirror of
https://github.com/cheveguerra/whaticket-community.git
synced 2026-04-18 11:49:19 +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 contact = await ticket.$get("contact");
|
||||
|
||||
await ticket.$get("contact");
|
||||
|
||||
const wapp = await ticket.$get("whatsapp");
|
||||
|
||||
@@ -122,7 +123,7 @@ export const store = async (req: Request, res: Response): Promise<Response> => {
|
||||
// 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> => {
|
||||
|
||||
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");
|
||||
|
||||
exports.index = async (req, res) => {
|
||||
const {
|
||||
pageNumber = 1,
|
||||
status = "",
|
||||
date = "",
|
||||
searchParam = "",
|
||||
showAll,
|
||||
} = req.query;
|
||||
const {
|
||||
pageNumber = 1,
|
||||
status = "",
|
||||
date = "",
|
||||
searchParam = "",
|
||||
showAll
|
||||
} = req.query;
|
||||
|
||||
const userId = req.user.id;
|
||||
const userId = req.user.id;
|
||||
|
||||
const limit = 20;
|
||||
const offset = limit * (pageNumber - 1);
|
||||
const limit = 20;
|
||||
const offset = limit * (pageNumber - 1);
|
||||
|
||||
let includeCondition = [
|
||||
{
|
||||
model: Contact,
|
||||
as: "contact",
|
||||
attributes: ["name", "number", "profilePicUrl"],
|
||||
},
|
||||
];
|
||||
let includeCondition = [
|
||||
{
|
||||
model: Contact,
|
||||
as: "contact",
|
||||
attributes: ["name", "number", "profilePicUrl"]
|
||||
}
|
||||
];
|
||||
|
||||
let whereCondition = { userId: userId };
|
||||
let whereCondition = { userId: userId };
|
||||
|
||||
if (showAll === "true") {
|
||||
whereCondition = {};
|
||||
}
|
||||
if (showAll === "true") {
|
||||
whereCondition = {};
|
||||
}
|
||||
|
||||
if (status) {
|
||||
whereCondition = {
|
||||
...whereCondition,
|
||||
status: status,
|
||||
};
|
||||
}
|
||||
if (status) {
|
||||
whereCondition = {
|
||||
...whereCondition,
|
||||
status: status
|
||||
};
|
||||
}
|
||||
|
||||
if (searchParam) {
|
||||
includeCondition = [
|
||||
...includeCondition,
|
||||
{
|
||||
model: Message,
|
||||
as: "messages",
|
||||
attributes: ["id", "body"],
|
||||
where: {
|
||||
body: Sequelize.where(
|
||||
Sequelize.fn("LOWER", Sequelize.col("body")),
|
||||
"LIKE",
|
||||
"%" + searchParam.toLowerCase() + "%"
|
||||
),
|
||||
},
|
||||
required: false,
|
||||
duplicating: false,
|
||||
},
|
||||
];
|
||||
if (searchParam) {
|
||||
includeCondition = [
|
||||
...includeCondition,
|
||||
{
|
||||
model: Message,
|
||||
as: "messages",
|
||||
attributes: ["id", "body"],
|
||||
where: {
|
||||
body: Sequelize.where(
|
||||
Sequelize.fn("LOWER", Sequelize.col("body")),
|
||||
"LIKE",
|
||||
"%" + searchParam.toLowerCase() + "%"
|
||||
)
|
||||
},
|
||||
required: false,
|
||||
duplicating: false
|
||||
}
|
||||
];
|
||||
|
||||
whereCondition = {
|
||||
[Sequelize.Op.or]: [
|
||||
{
|
||||
"$contact.name$": Sequelize.where(
|
||||
Sequelize.fn("LOWER", Sequelize.col("name")),
|
||||
"LIKE",
|
||||
"%" + searchParam.toLowerCase() + "%"
|
||||
),
|
||||
},
|
||||
{ "$contact.number$": { [Sequelize.Op.like]: `%${searchParam}%` } },
|
||||
{
|
||||
"$message.body$": Sequelize.where(
|
||||
Sequelize.fn("LOWER", Sequelize.col("body")),
|
||||
"LIKE",
|
||||
"%" + searchParam.toLowerCase() + "%"
|
||||
),
|
||||
},
|
||||
],
|
||||
};
|
||||
}
|
||||
whereCondition = {
|
||||
[Sequelize.Op.or]: [
|
||||
{
|
||||
"$contact.name$": Sequelize.where(
|
||||
Sequelize.fn("LOWER", Sequelize.col("name")),
|
||||
"LIKE",
|
||||
"%" + searchParam.toLowerCase() + "%"
|
||||
)
|
||||
},
|
||||
{ "$contact.number$": { [Sequelize.Op.like]: `%${searchParam}%` } },
|
||||
{
|
||||
"$message.body$": Sequelize.where(
|
||||
Sequelize.fn("LOWER", Sequelize.col("body")),
|
||||
"LIKE",
|
||||
"%" + searchParam.toLowerCase() + "%"
|
||||
)
|
||||
}
|
||||
]
|
||||
};
|
||||
}
|
||||
|
||||
if (date) {
|
||||
whereCondition = {
|
||||
...whereCondition,
|
||||
createdAt: {
|
||||
[Sequelize.Op.between]: [
|
||||
startOfDay(parseISO(date)),
|
||||
endOfDay(parseISO(date)),
|
||||
],
|
||||
},
|
||||
};
|
||||
}
|
||||
if (date) {
|
||||
whereCondition = {
|
||||
...whereCondition,
|
||||
createdAt: {
|
||||
[Sequelize.Op.between]: [
|
||||
startOfDay(parseISO(date)),
|
||||
endOfDay(parseISO(date))
|
||||
]
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
const { count, rows: tickets } = await Ticket.findAndCountAll({
|
||||
where: whereCondition,
|
||||
distinct: true,
|
||||
include: includeCondition,
|
||||
limit,
|
||||
offset,
|
||||
order: [["updatedAt", "DESC"]],
|
||||
});
|
||||
const { count, rows: tickets } = await Ticket.findAndCountAll({
|
||||
where: whereCondition,
|
||||
distinct: true,
|
||||
include: includeCondition,
|
||||
limit,
|
||||
offset,
|
||||
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) => {
|
||||
const io = getIO();
|
||||
const io = getIO();
|
||||
|
||||
const defaultWhatsapp = await Whatsapp.findOne({
|
||||
where: { default: true },
|
||||
});
|
||||
const defaultWhatsapp = await Whatsapp.findOne({
|
||||
where: { default: true }
|
||||
});
|
||||
|
||||
if (!defaultWhatsapp) {
|
||||
return res
|
||||
.status(404)
|
||||
.json({ error: "No default WhatsApp found. Check Connection page." });
|
||||
}
|
||||
if (!defaultWhatsapp) {
|
||||
return res
|
||||
.status(404)
|
||||
.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", {
|
||||
action: "create",
|
||||
ticket: serializaedTicket,
|
||||
});
|
||||
io.to("notification").emit("ticket", {
|
||||
action: "create",
|
||||
ticket: serializaedTicket
|
||||
});
|
||||
|
||||
return res.status(200).json(ticket);
|
||||
return res.status(200).json(ticket);
|
||||
};
|
||||
|
||||
exports.update = async (req, res) => {
|
||||
const io = getIO();
|
||||
const { ticketId } = req.params;
|
||||
const io = getIO();
|
||||
const { ticketId } = req.params;
|
||||
|
||||
const ticket = await Ticket.findByPk(ticketId, {
|
||||
include: [
|
||||
{
|
||||
model: Contact,
|
||||
as: "contact",
|
||||
attributes: ["name", "number", "profilePicUrl"],
|
||||
},
|
||||
],
|
||||
});
|
||||
const ticket = await Ticket.findByPk(ticketId, {
|
||||
include: [
|
||||
{
|
||||
model: Contact,
|
||||
as: "contact",
|
||||
attributes: ["name", "number", "profilePicUrl"]
|
||||
}
|
||||
]
|
||||
});
|
||||
|
||||
if (!ticket) {
|
||||
return res.status(404).json({ error: "No ticket found with this ID" });
|
||||
}
|
||||
if (!ticket) {
|
||||
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", {
|
||||
action: "updateStatus",
|
||||
ticket: ticket,
|
||||
});
|
||||
io.to("notification").emit("ticket", {
|
||||
action: "updateStatus",
|
||||
ticket: ticket
|
||||
});
|
||||
|
||||
return res.status(200).json(ticket);
|
||||
return res.status(200).json(ticket);
|
||||
};
|
||||
|
||||
exports.delete = async (req, res) => {
|
||||
const io = getIO();
|
||||
const { ticketId } = req.params;
|
||||
const io = getIO();
|
||||
const { ticketId } = req.params;
|
||||
|
||||
const ticket = await Ticket.findByPk(ticketId);
|
||||
const ticket = await Ticket.findByPk(ticketId);
|
||||
|
||||
if (!ticket) {
|
||||
return res.status(400).json({ error: "No ticket found with this ID" });
|
||||
}
|
||||
if (!ticket) {
|
||||
return res.status(400).json({ error: "No ticket found with this ID" });
|
||||
}
|
||||
|
||||
await ticket.destroy();
|
||||
await ticket.destroy();
|
||||
|
||||
io.to("notification").emit("ticket", {
|
||||
action: "delete",
|
||||
ticketId: ticket.id,
|
||||
});
|
||||
io.to("notification").emit("ticket", {
|
||||
action: "delete",
|
||||
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");
|
||||
|
||||
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) => {
|
||||
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;
|
||||
}
|
||||
),
|
||||
});
|
||||
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(req.body);
|
||||
} catch (err) {
|
||||
return res.status(400).json({ error: err.message });
|
||||
}
|
||||
try {
|
||||
await schema.validate(req.body);
|
||||
} catch (err) {
|
||||
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) {
|
||||
return res.status(400).json({ error: "Cannot create whatsapp session." });
|
||||
}
|
||||
if (!whatsapp) {
|
||||
return res.status(400).json({ error: "Cannot create whatsapp session." });
|
||||
}
|
||||
|
||||
initWbot(whatsapp)
|
||||
.then(() => {
|
||||
wbotMessageListener(whatsapp);
|
||||
wbotMonitor(whatsapp);
|
||||
})
|
||||
.catch(err => console.log(err));
|
||||
initWbot(whatsapp)
|
||||
.then(() => {
|
||||
wbotMessageListener(whatsapp);
|
||||
wbotMonitor(whatsapp);
|
||||
})
|
||||
.catch(err => console.log(err));
|
||||
|
||||
io.emit("whatsapp", {
|
||||
action: "update",
|
||||
whatsapp: whatsapp,
|
||||
});
|
||||
io.emit("whatsapp", {
|
||||
action: "update",
|
||||
whatsapp: whatsapp
|
||||
});
|
||||
|
||||
return res.status(200).json(whatsapp);
|
||||
return res.status(200).json(whatsapp);
|
||||
};
|
||||
|
||||
exports.show = async (req, res) => {
|
||||
const { whatsappId } = req.params;
|
||||
const whatsapp = await Whatsapp.findByPk(whatsappId);
|
||||
const { whatsappId } = req.params;
|
||||
const whatsapp = await Whatsapp.findByPk(whatsappId);
|
||||
|
||||
if (!whatsapp) {
|
||||
return res.status(200).json({ message: "Session not found" });
|
||||
}
|
||||
if (!whatsapp) {
|
||||
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) => {
|
||||
const { whatsappId } = req.params;
|
||||
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;
|
||||
}
|
||||
),
|
||||
});
|
||||
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 });
|
||||
}
|
||||
try {
|
||||
await schema.validate(req.body);
|
||||
} catch (err) {
|
||||
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) {
|
||||
return res.status(404).json({ message: "Whatsapp not found" });
|
||||
}
|
||||
if (!whatsapp) {
|
||||
return res.status(404).json({ message: "Whatsapp not found" });
|
||||
}
|
||||
|
||||
await whatsapp.update(req.body);
|
||||
await whatsapp.update(req.body);
|
||||
|
||||
io.emit("whatsapp", {
|
||||
action: "update",
|
||||
whatsapp: whatsapp,
|
||||
});
|
||||
io.emit("whatsapp", {
|
||||
action: "update",
|
||||
whatsapp: whatsapp
|
||||
});
|
||||
|
||||
return res.status(200).json({ message: "Whatsapp updated" });
|
||||
return res.status(200).json({ message: "Whatsapp updated" });
|
||||
};
|
||||
|
||||
exports.delete = async (req, res) => {
|
||||
const io = getIO();
|
||||
const { whatsappId } = req.params;
|
||||
const io = getIO();
|
||||
const { whatsappId } = req.params;
|
||||
|
||||
const whatsapp = await Whatsapp.findByPk(whatsappId);
|
||||
const whatsapp = await Whatsapp.findByPk(whatsappId);
|
||||
|
||||
if (!whatsapp) {
|
||||
return res.status(404).json({ message: "Whatsapp not found" });
|
||||
}
|
||||
if (!whatsapp) {
|
||||
return res.status(404).json({ message: "Whatsapp not found" });
|
||||
}
|
||||
|
||||
await whatsapp.destroy();
|
||||
removeWbot(whatsapp.id);
|
||||
await whatsapp.destroy();
|
||||
removeWbot(whatsapp.id);
|
||||
|
||||
io.emit("whatsapp", {
|
||||
action: "delete",
|
||||
whatsappId: whatsapp.id,
|
||||
});
|
||||
io.emit("whatsapp", {
|
||||
action: "delete",
|
||||
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 contactRoutes from "./contactRoutes";
|
||||
import ticketRoutes from "./ticketRoutes";
|
||||
|
||||
// const MessagesRoutes = require("./routes/messages");
|
||||
// const WhatsRoutes = require("./routes/whatsapp");
|
||||
// const UsersRoutes = require("./routes/users");
|
||||
import whatsappRoutes from "./whatsappRoutes";
|
||||
import messageRoutes from "./messageRoutes";
|
||||
|
||||
const routes = Router();
|
||||
|
||||
@@ -17,7 +15,7 @@ routes.use("/auth", authRoutes);
|
||||
routes.use(settingRoutes);
|
||||
routes.use(contactRoutes);
|
||||
routes.use(ticketRoutes);
|
||||
// routes.use(MessagesRoutes);
|
||||
// routes.use(WhatsRoutes);
|
||||
routes.use(whatsappRoutes);
|
||||
routes.use(messageRoutes);
|
||||
|
||||
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