2022-07-14 23:00:35 +00:00
|
|
|
import path from "path";
|
2022-01-06 01:26:15 +00:00
|
|
|
import { getIO } from "../libs/socket";
|
|
|
|
import Message from "../models/Message";
|
|
|
|
import Ticket from "../models/Ticket";
|
2022-07-14 23:00:35 +00:00
|
|
|
import ShowWhatsAppService from "../services/WhatsappService/ShowWhatsAppService";
|
2022-01-06 01:26:15 +00:00
|
|
|
import { logger } from "../utils/logger";
|
|
|
|
import GetTicketWbot from "./GetTicketWbot";
|
2022-07-14 23:00:35 +00:00
|
|
|
import fs from 'fs';
|
|
|
|
import { restartWhatsSession } from "./RestartWhatsSession";
|
|
|
|
import { Session } from "@sentry/types";
|
|
|
|
import { splitDateTime } from "./SplitDateTime";
|
|
|
|
|
|
|
|
import { format } from "date-fns";
|
|
|
|
import ptBR from 'date-fns/locale/pt-BR';
|
2022-11-16 13:23:14 +00:00
|
|
|
import ListWhatsAppsNumber from "../services/WhatsappService/ListWhatsAppsNumber";
|
2022-01-06 01:26:15 +00:00
|
|
|
|
|
|
|
const SetTicketMessagesAsRead = async (ticket: Ticket): Promise<void> => {
|
|
|
|
await Message.update(
|
|
|
|
{ read: true },
|
|
|
|
{
|
|
|
|
where: {
|
|
|
|
ticketId: ticket.id,
|
|
|
|
read: false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
await ticket.update({ unreadMessages: 0 });
|
|
|
|
|
2022-11-16 13:23:14 +00:00
|
|
|
try {
|
2022-07-14 23:00:35 +00:00
|
|
|
|
2022-11-16 13:23:14 +00:00
|
|
|
const wbot = await GetTicketWbot(ticket);
|
2022-07-14 23:00:35 +00:00
|
|
|
|
|
|
|
// test del
|
|
|
|
// throw new Error('Throw makes it go boom!')
|
|
|
|
//
|
2022-11-16 13:23:14 +00:00
|
|
|
|
2022-07-12 12:37:34 +00:00
|
|
|
await wbot.sendSeen(`${ticket.contact.number}@${ticket.isGroup ? "g" : "c"}.us`);
|
|
|
|
|
2022-01-06 01:26:15 +00:00
|
|
|
} catch (err) {
|
2022-07-14 23:00:35 +00:00
|
|
|
|
2022-11-16 13:23:14 +00:00
|
|
|
logger.warn(`Could not mark messages as read. Maybe whatsapp session disconnected? Err: ${err}`);
|
|
|
|
|
|
|
|
//Solução para contornar erro de sessão
|
|
|
|
if ((`${err}`).includes("Evaluation failed: r") && ticket.whatsappId) {
|
|
|
|
|
2022-12-14 12:29:42 +00:00
|
|
|
const sourcePath = path.join(__dirname, `../../.wwebjs_auth/sessions/log`)
|
2022-07-14 23:00:35 +00:00
|
|
|
|
|
|
|
const dateToday = splitDateTime(new Date(format(new Date(), 'yyyy-MM-dd HH:mm:ss', { locale: ptBR })))
|
|
|
|
|
2022-11-16 13:23:14 +00:00
|
|
|
const whatsapp = await ShowWhatsAppService(ticket.whatsappId);
|
|
|
|
|
|
|
|
if (whatsapp && whatsapp.status == 'CONNECTED') {
|
2022-07-14 23:00:35 +00:00
|
|
|
|
|
|
|
let timestamp = Math.floor(Date.now() / 1000)
|
2022-11-16 13:23:14 +00:00
|
|
|
|
|
|
|
fs.writeFile(`${sourcePath}/${timestamp}_SetTicketMessagesAsRead.txt`, `Whatsapp id: ${whatsapp.id} \nDate: ${dateToday.fullDate} ${dateToday.fullTime} \nFile: SetTicketMessagesAsRead.ts \nError: ${err}`, (error) => { });
|
2022-07-14 23:00:35 +00:00
|
|
|
|
2022-12-14 12:29:42 +00:00
|
|
|
// await restartWhatsSession(whatsapp)
|
2022-11-16 13:23:14 +00:00
|
|
|
|
2022-07-14 23:00:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2022-01-06 01:26:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const io = getIO();
|
|
|
|
io.to(ticket.status).to("notification").emit("ticket", {
|
|
|
|
action: "updateUnread",
|
|
|
|
ticketId: ticket.id
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
export default SetTicketMessagesAsRead;
|