projeto-hit/backend/src/helpers/SetTicketMessagesAsRead.ts

39 lines
930 B
TypeScript
Raw Normal View History

import { getIO } from "../libs/socket";
import Message from "../models/Message";
import Ticket from "../models/Ticket";
import { logger } from "../utils/logger";
import GetTicketWbot from "./GetTicketWbot";
const SetTicketMessagesAsRead = async (ticket: Ticket): Promise<void> => {
await Message.update(
{ read: true },
{
where: {
ticketId: ticket.id,
read: false
}
}
);
await ticket.update({ unreadMessages: 0 });
try {
const wbot = await GetTicketWbot(ticket);
2022-07-12 12:37:34 +00:00
await wbot.sendSeen(`${ticket.contact.number}@${ticket.isGroup ? "g" : "c"}.us`);
} catch (err) {
logger.warn(
`Could not mark messages as read. Maybe whatsapp session disconnected? Err: ${err}`
);
}
const io = getIO();
io.to(ticket.status).to("notification").emit("ticket", {
action: "updateUnread",
ticketId: ticket.id
});
};
export default SetTicketMessagesAsRead;