mirror of
https://github.com/cheveguerra/whaticket-community.git
synced 2026-04-19 04:09:26 +00:00
migrated receive message logic to typescript
This commit is contained in:
22
backend/src/helpers/GetTicketWbot.ts
Normal file
22
backend/src/helpers/GetTicketWbot.ts
Normal file
@@ -0,0 +1,22 @@
|
||||
import { Client as Session } from "whatsapp-web.js";
|
||||
import { getWbot } from "../libs/wbot";
|
||||
import AppError from "../errors/AppError";
|
||||
import GetDefaultWhatsApp from "./GetDefaultWhatsApp";
|
||||
import Ticket from "../models/Ticket";
|
||||
|
||||
const GetTicketWbot = async (ticket: Ticket): Promise<Session> => {
|
||||
if (!ticket.whatsappId) {
|
||||
const defaultWhatsapp = await GetDefaultWhatsApp();
|
||||
|
||||
if (!defaultWhatsapp) {
|
||||
throw new AppError("No default WhatsApp found. Check Connection page.");
|
||||
}
|
||||
await ticket.$set("whatsapp", defaultWhatsapp);
|
||||
}
|
||||
|
||||
const wbot = getWbot(ticket.whatsappId);
|
||||
|
||||
return wbot;
|
||||
};
|
||||
|
||||
export default GetTicketWbot;
|
||||
34
backend/src/helpers/SetTicketMessagesAsRead.ts
Normal file
34
backend/src/helpers/SetTicketMessagesAsRead.ts
Normal file
@@ -0,0 +1,34 @@
|
||||
import { getIO } from "../libs/socket";
|
||||
import { getWbot } from "../libs/wbot";
|
||||
import Message from "../models/Message";
|
||||
import Ticket from "../models/Ticket";
|
||||
|
||||
const SetTicketMessagesAsRead = async (ticket: Ticket): Promise<void> => {
|
||||
const wbot = getWbot(ticket.whatsappId);
|
||||
|
||||
await Message.update(
|
||||
{ read: true },
|
||||
{
|
||||
where: {
|
||||
ticketId: ticket.id,
|
||||
read: false
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
try {
|
||||
await wbot.sendSeen(`${ticket.contact.number}@c.us`);
|
||||
} catch (err) {
|
||||
console.log(
|
||||
"Could not mark messages as read. Maybe whatsapp session disconnected?"
|
||||
);
|
||||
}
|
||||
|
||||
const io = getIO();
|
||||
io.to("notification").emit("ticket", {
|
||||
action: "updateUnread",
|
||||
ticketId: ticket.id
|
||||
});
|
||||
};
|
||||
|
||||
export default SetTicketMessagesAsRead;
|
||||
Reference in New Issue
Block a user