mirror of
https://github.com/cheveguerra/whaticket-community.git
synced 2026-04-18 03:39:29 +00:00
improvement: moved unreadMessages logic to model
This commit is contained in:
@@ -39,22 +39,6 @@ exports.index = async (req, res) => {
|
||||
attributes: ["name", "number", "profilePicUrl"],
|
||||
},
|
||||
],
|
||||
attributes: {
|
||||
include: [
|
||||
[
|
||||
Sequelize.literal(`(
|
||||
SELECT COUNT(*)
|
||||
FROM Messages AS message
|
||||
WHERE
|
||||
message.ticketId = Ticket.id
|
||||
AND
|
||||
message.read = 0
|
||||
|
||||
)`),
|
||||
"unreadMessages",
|
||||
],
|
||||
],
|
||||
},
|
||||
order: [["updatedAt", "DESC"]],
|
||||
});
|
||||
|
||||
@@ -89,22 +73,6 @@ exports.update = async (req, res) => {
|
||||
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) {
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
const Sequelize = require("sequelize");
|
||||
const Message = require("./Message");
|
||||
|
||||
class Ticket extends Sequelize.Model {
|
||||
static init(sequelize) {
|
||||
@@ -6,6 +7,7 @@ class Ticket extends Sequelize.Model {
|
||||
{
|
||||
status: { type: Sequelize.STRING, defaultValue: "pending" },
|
||||
userId: { type: Sequelize.INTEGER, defaultValue: null },
|
||||
unreadMessages: { type: Sequelize.VIRTUAL },
|
||||
lastMessage: { type: Sequelize.STRING },
|
||||
},
|
||||
{
|
||||
@@ -13,6 +15,12 @@ class Ticket extends Sequelize.Model {
|
||||
}
|
||||
);
|
||||
|
||||
this.addHook("afterUpdate", async ticket => {
|
||||
ticket.unreadMessages = await Message.count({
|
||||
where: { ticketId: ticket.id, read: false },
|
||||
});
|
||||
});
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
@@ -48,7 +48,7 @@ const verifyTicket = async contact => {
|
||||
});
|
||||
|
||||
if (ticket) {
|
||||
await ticket.update({ status: "open" });
|
||||
await ticket.update({ status: "pending", userId: null });
|
||||
}
|
||||
}
|
||||
|
||||
@@ -120,8 +120,8 @@ const handleMessage = async (msg, ticket, contact) => {
|
||||
|
||||
const serializaedTicket = {
|
||||
...ticket.dataValues,
|
||||
unreadMessages: 1,
|
||||
lastMessage: newMessage.body,
|
||||
// unreadMessages: 1,
|
||||
// lastMessage: newMessage.body,
|
||||
contact: contact,
|
||||
};
|
||||
|
||||
|
||||
@@ -251,6 +251,7 @@ const TicketsList = () => {
|
||||
socket.emit("joinNotification");
|
||||
|
||||
socket.on("ticket", data => {
|
||||
console.log("data", data);
|
||||
if (data.action === "updateUnread") {
|
||||
resetUnreadMessages(data);
|
||||
}
|
||||
@@ -268,7 +269,7 @@ const TicketsList = () => {
|
||||
|
||||
socket.on("appMessage", data => {
|
||||
if (data.action === "create") {
|
||||
updateUnreadMessagesCount(data);
|
||||
updateTickets(data);
|
||||
if (
|
||||
(ticketId &&
|
||||
data.message.ticketId === +ticketId &&
|
||||
@@ -285,26 +286,8 @@ const TicketsList = () => {
|
||||
};
|
||||
}, [ticketId, userId, history]);
|
||||
|
||||
const updateUnreadMessagesCount = ({ message, ticket }) => {
|
||||
setTickets(prevState => {
|
||||
const ticketIndex = prevState.findIndex(t => t.id === message.ticketId);
|
||||
|
||||
if (ticketIndex !== -1) {
|
||||
let aux = [...prevState];
|
||||
if (!message.fromMe) {
|
||||
aux[ticketIndex].unreadMessages++;
|
||||
}
|
||||
aux[ticketIndex].lastMessage = message.body;
|
||||
aux[ticketIndex].status = ticket.status;
|
||||
aux.unshift(aux.splice(ticketIndex, 1)[0]);
|
||||
return aux;
|
||||
} else {
|
||||
return [ticket, ...prevState];
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
const updateTickets = ({ ticket }) => {
|
||||
console.log("recebido", ticket);
|
||||
setTickets(prevState => {
|
||||
const ticketIndex = prevState.findIndex(t => t.id === ticket.id);
|
||||
|
||||
@@ -313,11 +296,14 @@ const TicketsList = () => {
|
||||
} else {
|
||||
let aux = [...prevState];
|
||||
aux[ticketIndex] = ticket;
|
||||
aux.unshift(aux.splice(ticketIndex, 1)[0]);
|
||||
return aux;
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
console.log(tickets);
|
||||
|
||||
const deleteTicket = ({ ticketId }) => {
|
||||
setTickets(prevState => {
|
||||
const ticketIndex = prevState.findIndex(ticket => ticket.id === ticketId);
|
||||
|
||||
Reference in New Issue
Block a user