mirror of
https://github.com/cheveguerra/whaticket-community.git
synced 2026-04-18 19:59:20 +00:00
All models using classes and sequelize migrations
This commit is contained in:
67
backend/src/controllers/ContactController copy.js
Normal file
67
backend/src/controllers/ContactController copy.js
Normal file
@@ -0,0 +1,67 @@
|
||||
const Contact = require("../models/Contact");
|
||||
const Message = require("../models/Message");
|
||||
const Sequelize = require("sequelize");
|
||||
const { getIO } = require("../libs/socket");
|
||||
const { getWbot } = require("../libs/wbot");
|
||||
|
||||
exports.index = async (req, res) => {
|
||||
const { searchParam = "" } = req.query;
|
||||
|
||||
const lowerSerachParam = searchParam.toLowerCase();
|
||||
|
||||
const whereCondition = {
|
||||
name: Sequelize.where(
|
||||
Sequelize.fn("LOWER", Sequelize.col("name")),
|
||||
"LIKE",
|
||||
"%" + lowerSerachParam + "%"
|
||||
),
|
||||
};
|
||||
|
||||
//todo >> add contact number to search where condition
|
||||
|
||||
const contacts = await Contact.findAll({
|
||||
where: whereCondition,
|
||||
attributes: {
|
||||
include: [
|
||||
[
|
||||
Sequelize.literal(`(
|
||||
SELECT COUNT(*)
|
||||
FROM messages AS message
|
||||
WHERE
|
||||
message.contactId = contact.id
|
||||
AND
|
||||
message.read = 0
|
||||
|
||||
)`),
|
||||
"unreadMessages",
|
||||
],
|
||||
],
|
||||
},
|
||||
order: [["updatedAt", "DESC"]],
|
||||
});
|
||||
|
||||
return res.json(contacts);
|
||||
};
|
||||
|
||||
exports.store = async (req, res) => {
|
||||
const wbot = getWbot();
|
||||
const io = getIO();
|
||||
const { number, name } = req.body;
|
||||
|
||||
const result = await wbot.isRegisteredUser(`55${number}@c.us`);
|
||||
|
||||
if (!result) {
|
||||
return res
|
||||
.status(400)
|
||||
.json({ error: "The suplied number is not a valid Whatsapp number" });
|
||||
}
|
||||
const profilePicUrl = await wbot.getProfilePicUrl(`55${number}@c.us`);
|
||||
|
||||
const contact = await Contact.create({
|
||||
name,
|
||||
number: `55${number}`,
|
||||
profilePicUrl,
|
||||
});
|
||||
|
||||
res.status(200).json(contact);
|
||||
};
|
||||
@@ -1,67 +1,44 @@
|
||||
const Contact = require("../models/Contact");
|
||||
const Message = require("../models/Message");
|
||||
const Sequelize = require("sequelize");
|
||||
const { getIO } = require("../libs/socket");
|
||||
const { getWbot } = require("../libs/wbot");
|
||||
// const Message = require("../models/Message");
|
||||
// const Sequelize = require("sequelize");
|
||||
// const { getIO } = require("../libs/socket");
|
||||
// const { getWbot } = require("../libs/wbot");
|
||||
|
||||
exports.index = async (req, res) => {
|
||||
const { searchParam = "" } = req.query;
|
||||
// const { searchParam = "" } = req.query;
|
||||
|
||||
const lowerSerachParam = searchParam.toLowerCase();
|
||||
// const lowerSerachParam = searchParam.toLowerCase();
|
||||
|
||||
const whereCondition = {
|
||||
name: Sequelize.where(
|
||||
Sequelize.fn("LOWER", Sequelize.col("name")),
|
||||
"LIKE",
|
||||
"%" + lowerSerachParam + "%"
|
||||
),
|
||||
};
|
||||
// const whereCondition = {
|
||||
// name: Sequelize.where(
|
||||
// Sequelize.fn("LOWER", Sequelize.col("name")),
|
||||
// "LIKE",
|
||||
// "%" + lowerSerachParam + "%"
|
||||
// ),
|
||||
// };
|
||||
|
||||
//todo >> add contact number to search where condition
|
||||
|
||||
const contacts = await Contact.findAll({
|
||||
where: whereCondition,
|
||||
attributes: {
|
||||
include: [
|
||||
[
|
||||
Sequelize.literal(`(
|
||||
SELECT COUNT(*)
|
||||
FROM messages AS message
|
||||
WHERE
|
||||
message.contactId = contact.id
|
||||
AND
|
||||
message.read = 0
|
||||
|
||||
)`),
|
||||
"unreadMessages",
|
||||
],
|
||||
],
|
||||
},
|
||||
order: [["updatedAt", "DESC"]],
|
||||
});
|
||||
const contacts = await Contact.findAll();
|
||||
|
||||
return res.json(contacts);
|
||||
};
|
||||
|
||||
exports.store = async (req, res) => {
|
||||
const wbot = getWbot();
|
||||
const io = getIO();
|
||||
const { number, name } = req.body;
|
||||
// const wbot = getWbot();
|
||||
// const io = getIO();
|
||||
// const { number, name } = req.body;
|
||||
|
||||
const result = await wbot.isRegisteredUser(`55${number}@c.us`);
|
||||
// const result = await wbot.isRegisteredUser(`55${number}@c.us`);
|
||||
|
||||
if (!result) {
|
||||
return res
|
||||
.status(400)
|
||||
.json({ error: "The suplied number is not a valid Whatsapp number" });
|
||||
}
|
||||
const profilePicUrl = await wbot.getProfilePicUrl(`55${number}@c.us`);
|
||||
// if (!result) {
|
||||
// return res
|
||||
// .status(400)
|
||||
// .json({ error: "The suplied number is not a valid Whatsapp number" });
|
||||
// }
|
||||
// const profilePicUrl = await wbot.getProfilePicUrl(`55${number}@c.us`);
|
||||
|
||||
const contact = await Contact.create({
|
||||
name,
|
||||
number: `55${number}`,
|
||||
profilePicUrl,
|
||||
});
|
||||
const { number, name } = await Contact.create(req.body);
|
||||
|
||||
res.status(200).json(contact);
|
||||
res.status(200).json({ number, name });
|
||||
};
|
||||
|
||||
@@ -1,64 +1,71 @@
|
||||
const Message = require("../models/Message");
|
||||
const Contact = require("../models/Contact");
|
||||
const Ticket = require("../models/Ticket");
|
||||
const { getIO } = require("../libs/socket");
|
||||
const { getWbot } = require("../libs/wbot");
|
||||
const Sequelize = require("sequelize");
|
||||
|
||||
const { MessageMedia } = require("whatsapp-web.js");
|
||||
|
||||
const setMessagesAsRead = async contactId => {
|
||||
const setMessagesAsRead = async ticketId => {
|
||||
const io = getIO();
|
||||
await Message.update(
|
||||
{ read: true },
|
||||
{
|
||||
where: {
|
||||
contactId: contactId,
|
||||
ticketId: ticketId,
|
||||
read: false,
|
||||
},
|
||||
}
|
||||
);
|
||||
|
||||
io.to("notification").emit("contact", {
|
||||
io.to("notification").emit("ticket", {
|
||||
action: "updateUnread",
|
||||
contactId: contactId,
|
||||
ticketId: ticketId,
|
||||
});
|
||||
};
|
||||
|
||||
exports.index = async (req, res, next) => {
|
||||
const wbot = getWbot();
|
||||
const io = getIO();
|
||||
// const wbot = getWbot();
|
||||
// const io = getIO();
|
||||
|
||||
const { contactId } = req.params;
|
||||
const { ticketId } = req.params;
|
||||
const { searchParam = "", pageNumber = 1 } = req.query;
|
||||
|
||||
const lowerSerachParam = searchParam.toLowerCase();
|
||||
|
||||
const whereCondition = {
|
||||
messageBody: Sequelize.where(
|
||||
Sequelize.fn("LOWER", Sequelize.col("messageBody")),
|
||||
body: Sequelize.where(
|
||||
Sequelize.fn("LOWER", Sequelize.col("body")),
|
||||
"LIKE",
|
||||
"%" + lowerSerachParam + "%"
|
||||
"%" + searchParam.toLowerCase() + "%"
|
||||
),
|
||||
};
|
||||
|
||||
let limit = 20;
|
||||
let offset = limit * (pageNumber - 1);
|
||||
|
||||
const contact = await Contact.findByPk(contactId);
|
||||
if (!contact) {
|
||||
return res.status(400).json({ error: "No contact found with this ID" });
|
||||
const ticket = await Ticket.findByPk(ticketId, {
|
||||
include: [
|
||||
{
|
||||
model: Contact,
|
||||
attributes: ["name", "number", "profilePicUrl"],
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
if (!ticket) {
|
||||
return res.status(400).json({ error: "No ticket found with this ID" });
|
||||
}
|
||||
|
||||
await setMessagesAsRead(contactId);
|
||||
await setMessagesAsRead(ticketId);
|
||||
|
||||
const contactMessages = await contact.getMessages({
|
||||
const ticketMessages = await ticket.getMessages({
|
||||
where: whereCondition,
|
||||
limit,
|
||||
offset,
|
||||
order: [["createdAt", "DESC"]],
|
||||
});
|
||||
|
||||
const serializedMessages = contactMessages.map(message => {
|
||||
const serializedMessages = ticketMessages.map(message => {
|
||||
return {
|
||||
...message.dataValues,
|
||||
mediaUrl: `${
|
||||
@@ -71,7 +78,8 @@ exports.index = async (req, res, next) => {
|
||||
|
||||
return res.json({
|
||||
messages: serializedMessages.reverse(),
|
||||
contact: contact,
|
||||
ticket: ticket,
|
||||
contact: ticket.Contact,
|
||||
});
|
||||
};
|
||||
|
||||
@@ -79,12 +87,20 @@ exports.store = async (req, res, next) => {
|
||||
const wbot = getWbot();
|
||||
const io = getIO();
|
||||
|
||||
const { contactId } = req.params;
|
||||
const { ticketId } = req.params;
|
||||
const message = req.body;
|
||||
const media = req.file;
|
||||
let sentMessage;
|
||||
|
||||
const contact = await Contact.findByPk(contactId);
|
||||
const ticket = await Ticket.findByPk(ticketId, {
|
||||
include: [
|
||||
{
|
||||
model: Contact,
|
||||
attributes: ["number"],
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
if (media) {
|
||||
const newMedia = MessageMedia.fromFilePath(req.file.path);
|
||||
message.mediaUrl = req.file.filename.replace(/\s/g, "");
|
||||
@@ -94,17 +110,20 @@ exports.store = async (req, res, next) => {
|
||||
message.mediaType = "other";
|
||||
}
|
||||
|
||||
sentMessage = await wbot.sendMessage(`${contact.number}@c.us`, newMedia);
|
||||
sentMessage = await wbot.sendMessage(
|
||||
`${ticket.Contact.number}@c.us`,
|
||||
newMedia
|
||||
);
|
||||
} else {
|
||||
sentMessage = await wbot.sendMessage(
|
||||
`${contact.number}@c.us`,
|
||||
message.messageBody
|
||||
`${ticket.Contact.number}@c.us`,
|
||||
message.body
|
||||
);
|
||||
}
|
||||
|
||||
message.id = sentMessage.id.id;
|
||||
|
||||
const newMessage = await contact.createMessage(message);
|
||||
const newMessage = await ticket.createMessage(message);
|
||||
|
||||
const serialziedMessage = {
|
||||
...newMessage.dataValues,
|
||||
@@ -115,11 +134,12 @@ exports.store = async (req, res, next) => {
|
||||
}`,
|
||||
};
|
||||
|
||||
io.to(contactId).emit("appMessage", {
|
||||
io.to(ticketId).emit("appMessage", {
|
||||
action: "create",
|
||||
message: serialziedMessage,
|
||||
});
|
||||
await setMessagesAsRead(contactId);
|
||||
|
||||
return res.json({ message: "Mensagem enviada" });
|
||||
await setMessagesAsRead(ticketId);
|
||||
|
||||
return res.json({ message: "Mensagem enviada", newMessage, ticket });
|
||||
};
|
||||
|
||||
76
backend/src/controllers/TicketController.js
Normal file
76
backend/src/controllers/TicketController.js
Normal file
@@ -0,0 +1,76 @@
|
||||
const Sequelize = require("sequelize");
|
||||
|
||||
const Ticket = require("../models/Ticket");
|
||||
const Contact = require("../models/Contact");
|
||||
|
||||
exports.index = async (req, res) => {
|
||||
const tickets = await Ticket.findAll({
|
||||
include: [
|
||||
{
|
||||
model: Contact,
|
||||
attributes: ["name", "number", "profilePicUrl"],
|
||||
},
|
||||
],
|
||||
attributes: {
|
||||
include: [
|
||||
[
|
||||
Sequelize.literal(`(
|
||||
SELECT COUNT(*)
|
||||
FROM Messages AS message
|
||||
WHERE
|
||||
message.ticketId = Ticket.id
|
||||
AND
|
||||
message.read = 0
|
||||
|
||||
)`),
|
||||
"unreadMessages",
|
||||
],
|
||||
],
|
||||
},
|
||||
});
|
||||
|
||||
return res.json(tickets);
|
||||
};
|
||||
|
||||
exports.store = async (req, res) => {
|
||||
const ticket = await Ticket.create(req.body);
|
||||
|
||||
res.status(200).json(ticket);
|
||||
};
|
||||
|
||||
exports.update = async (req, res) => {
|
||||
const { ticketId } = req.params;
|
||||
|
||||
const ticket = await Ticket.findByPk(ticketId, {
|
||||
include: [
|
||||
{
|
||||
model: Contact,
|
||||
attributes: ["name", "number", "profilePicUrl"],
|
||||
},
|
||||
],
|
||||
attributes: {
|
||||
include: [
|
||||
[
|
||||
Sequelize.literal(`(
|
||||
SELECT COUNT(*)
|
||||
FROM Messages AS message
|
||||
WHERE
|
||||
message.ticketId = Ticket.id
|
||||
AND
|
||||
message.read = 0
|
||||
|
||||
)`),
|
||||
"unreadMessages",
|
||||
],
|
||||
],
|
||||
},
|
||||
});
|
||||
|
||||
if (!ticket) {
|
||||
return res.status(400).json({ error: "No ticket found with this ID" });
|
||||
}
|
||||
|
||||
await ticket.update(req.body);
|
||||
|
||||
res.status(200).json(ticket);
|
||||
};
|
||||
23
backend/src/controllers/WhatsAppSessionController.js
Normal file
23
backend/src/controllers/WhatsAppSessionController.js
Normal file
@@ -0,0 +1,23 @@
|
||||
const Whatsapp = require("../models/Whatsapp");
|
||||
// const { getIO } = require("../libs/socket");
|
||||
// const { getWbot, init } = require("../libs/wbot");
|
||||
|
||||
exports.show = async (req, res, next) => {
|
||||
const { sessionId } = req.params;
|
||||
const dbSession = await Whatsapp.findByPk(sessionId);
|
||||
|
||||
if (!dbSession) {
|
||||
return res.status(200).json({ message: "Session not found" });
|
||||
}
|
||||
|
||||
return res.status(200).json(dbSession);
|
||||
};
|
||||
|
||||
// exports.getContacts = async (req, res, next) => {
|
||||
// const io = getIO();
|
||||
// const wbot = getWbot();
|
||||
|
||||
// const phoneContacts = await wbot.getContacts();
|
||||
|
||||
// return res.status(200).json(phoneContacts);
|
||||
// };
|
||||
@@ -1,22 +0,0 @@
|
||||
const Whatsapp = require("../models/Whatsapp");
|
||||
const { getIO } = require("../libs/socket");
|
||||
const { getWbot, init } = require("../libs/wbot");
|
||||
|
||||
exports.getSession = async (req, res, next) => {
|
||||
const dbSession = await Whatsapp.findOne({ where: { id: 1 } });
|
||||
|
||||
if (!dbSession) {
|
||||
return res.status(200).json({ message: "Session not found" });
|
||||
}
|
||||
|
||||
return res.status(200).json(dbSession);
|
||||
};
|
||||
|
||||
exports.getContacts = async (req, res, next) => {
|
||||
const io = getIO();
|
||||
const wbot = getWbot();
|
||||
|
||||
const phoneContacts = await wbot.getContacts();
|
||||
|
||||
return res.status(200).json(phoneContacts);
|
||||
};
|
||||
Reference in New Issue
Block a user