mirror of
https://github.com/cheveguerra/whaticket-community.git
synced 2026-04-20 20:59:16 +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: ["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"]],
|
order: [["updatedAt", "DESC"]],
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -89,22 +73,6 @@ exports.update = async (req, res) => {
|
|||||||
attributes: ["name", "number", "profilePicUrl"],
|
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) {
|
if (!ticket) {
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
const Sequelize = require("sequelize");
|
const Sequelize = require("sequelize");
|
||||||
|
const Message = require("./Message");
|
||||||
|
|
||||||
class Ticket extends Sequelize.Model {
|
class Ticket extends Sequelize.Model {
|
||||||
static init(sequelize) {
|
static init(sequelize) {
|
||||||
@@ -6,6 +7,7 @@ class Ticket extends Sequelize.Model {
|
|||||||
{
|
{
|
||||||
status: { type: Sequelize.STRING, defaultValue: "pending" },
|
status: { type: Sequelize.STRING, defaultValue: "pending" },
|
||||||
userId: { type: Sequelize.INTEGER, defaultValue: null },
|
userId: { type: Sequelize.INTEGER, defaultValue: null },
|
||||||
|
unreadMessages: { type: Sequelize.VIRTUAL },
|
||||||
lastMessage: { type: Sequelize.STRING },
|
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;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -48,7 +48,7 @@ const verifyTicket = async contact => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
if (ticket) {
|
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 = {
|
const serializaedTicket = {
|
||||||
...ticket.dataValues,
|
...ticket.dataValues,
|
||||||
unreadMessages: 1,
|
// unreadMessages: 1,
|
||||||
lastMessage: newMessage.body,
|
// lastMessage: newMessage.body,
|
||||||
contact: contact,
|
contact: contact,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -251,6 +251,7 @@ const TicketsList = () => {
|
|||||||
socket.emit("joinNotification");
|
socket.emit("joinNotification");
|
||||||
|
|
||||||
socket.on("ticket", data => {
|
socket.on("ticket", data => {
|
||||||
|
console.log("data", data);
|
||||||
if (data.action === "updateUnread") {
|
if (data.action === "updateUnread") {
|
||||||
resetUnreadMessages(data);
|
resetUnreadMessages(data);
|
||||||
}
|
}
|
||||||
@@ -268,7 +269,7 @@ const TicketsList = () => {
|
|||||||
|
|
||||||
socket.on("appMessage", data => {
|
socket.on("appMessage", data => {
|
||||||
if (data.action === "create") {
|
if (data.action === "create") {
|
||||||
updateUnreadMessagesCount(data);
|
updateTickets(data);
|
||||||
if (
|
if (
|
||||||
(ticketId &&
|
(ticketId &&
|
||||||
data.message.ticketId === +ticketId &&
|
data.message.ticketId === +ticketId &&
|
||||||
@@ -285,26 +286,8 @@ const TicketsList = () => {
|
|||||||
};
|
};
|
||||||
}, [ticketId, userId, history]);
|
}, [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 }) => {
|
const updateTickets = ({ ticket }) => {
|
||||||
|
console.log("recebido", ticket);
|
||||||
setTickets(prevState => {
|
setTickets(prevState => {
|
||||||
const ticketIndex = prevState.findIndex(t => t.id === ticket.id);
|
const ticketIndex = prevState.findIndex(t => t.id === ticket.id);
|
||||||
|
|
||||||
@@ -313,11 +296,14 @@ const TicketsList = () => {
|
|||||||
} else {
|
} else {
|
||||||
let aux = [...prevState];
|
let aux = [...prevState];
|
||||||
aux[ticketIndex] = ticket;
|
aux[ticketIndex] = ticket;
|
||||||
|
aux.unshift(aux.splice(ticketIndex, 1)[0]);
|
||||||
return aux;
|
return aux;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
console.log(tickets);
|
||||||
|
|
||||||
const deleteTicket = ({ ticketId }) => {
|
const deleteTicket = ({ ticketId }) => {
|
||||||
setTickets(prevState => {
|
setTickets(prevState => {
|
||||||
const ticketIndex = prevState.findIndex(ticket => ticket.id === ticketId);
|
const ticketIndex = prevState.findIndex(ticket => ticket.id === ticketId);
|
||||||
|
|||||||
Reference in New Issue
Block a user