mirror of
https://github.com/cheveguerra/whaticket-community.git
synced 2026-04-20 12:49:32 +00:00
added unread messages count to tickets
This commit is contained in:
@@ -9,7 +9,9 @@ import {
|
|||||||
ForeignKey,
|
ForeignKey,
|
||||||
BelongsTo,
|
BelongsTo,
|
||||||
HasMany,
|
HasMany,
|
||||||
AutoIncrement
|
AutoIncrement,
|
||||||
|
AfterFind,
|
||||||
|
BeforeUpdate
|
||||||
} from "sequelize-typescript";
|
} from "sequelize-typescript";
|
||||||
|
|
||||||
import Contact from "./Contact";
|
import Contact from "./Contact";
|
||||||
@@ -28,7 +30,7 @@ class Ticket extends Model<Ticket> {
|
|||||||
status: string;
|
status: string;
|
||||||
|
|
||||||
@Column(DataType.VIRTUAL)
|
@Column(DataType.VIRTUAL)
|
||||||
unreadMessages: string;
|
unreadMessages: number;
|
||||||
|
|
||||||
@Column
|
@Column
|
||||||
lastMessage: string;
|
lastMessage: string;
|
||||||
@@ -62,6 +64,26 @@ class Ticket extends Model<Ticket> {
|
|||||||
|
|
||||||
@HasMany(() => Message)
|
@HasMany(() => Message)
|
||||||
messages: Message[];
|
messages: Message[];
|
||||||
|
|
||||||
|
@AfterFind
|
||||||
|
static async countTicketsUnreadMessages(tickets: Ticket[]): Promise<void> {
|
||||||
|
if (tickets && tickets.length > 0) {
|
||||||
|
await Promise.all(
|
||||||
|
tickets.map(async ticket => {
|
||||||
|
ticket.unreadMessages = await Message.count({
|
||||||
|
where: { ticketId: ticket.id, read: false }
|
||||||
|
});
|
||||||
|
})
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@BeforeUpdate
|
||||||
|
static async countTicketUnreadMessags(ticket: Ticket): Promise<void> {
|
||||||
|
ticket.unreadMessages = await Message.count({
|
||||||
|
where: { ticketId: ticket.id, read: false }
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export default Ticket;
|
export default Ticket;
|
||||||
|
|||||||
@@ -1,16 +1,18 @@
|
|||||||
const path = require("path");
|
import path from "path";
|
||||||
const fs = require("fs");
|
import fs from "fs";
|
||||||
const { Op } = require("sequelize");
|
import { Op } from "sequelize";
|
||||||
const { subHours } = require("date-fns");
|
import { subHours } from "date-fns";
|
||||||
const Sentry = require("@sentry/node");
|
import Sentry from "@sentry/node";
|
||||||
|
|
||||||
const Contact = require("../models/Contact");
|
import { Client, Message } from "whatsapp-web.js";
|
||||||
const Ticket = require("../models/Ticket");
|
|
||||||
const Message = require("../models/Message");
|
|
||||||
const Whatsapp = require("../models/Whatsapp");
|
|
||||||
|
|
||||||
const { getIO } = require("../libs/socket");
|
import Contact from "../../models/Contact";
|
||||||
const { getWbot, initWbot } = require("../libs/wbot");
|
import Ticket from "../../models/Ticket";
|
||||||
|
import Message from "../../models/Message";
|
||||||
|
import Whatsapp from "../../models/Whatsapp";
|
||||||
|
|
||||||
|
import { getIO } from "../../libs/socket";
|
||||||
|
import { getWbot } from "../../libs/wbot";
|
||||||
|
|
||||||
const verifyContact = async (msgContact, profilePicUrl) => {
|
const verifyContact = async (msgContact, profilePicUrl) => {
|
||||||
let contact = await Contact.findOne({
|
let contact = await Contact.findOne({
|
||||||
@@ -134,53 +136,60 @@ const handleMessage = async (msg, ticket, contact) => {
|
|||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
const wbotMessageListener = whatsapp => {
|
const wbotMessageListener = (whatsapp: Whatsapp): void => {
|
||||||
const whatsappId = whatsapp.id;
|
const whatsappId = whatsapp.id;
|
||||||
const wbot = getWbot(whatsappId);
|
const wbot = getWbot(whatsappId);
|
||||||
const io = getIO();
|
const io = getIO();
|
||||||
|
|
||||||
wbot.on("message_create", async msg => {
|
if (!wbot) {
|
||||||
// console.log(msg);
|
console.log("Wbot not initialized");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
if (
|
wbot.on(
|
||||||
msg.from === "status@broadcast" ||
|
"message_create",
|
||||||
msg.type === "location" ||
|
async (msg): Promise<void> => {
|
||||||
msg.type === "call_log" ||
|
// console.log(msg);
|
||||||
msg.author != null // Ignore Group Messages
|
|
||||||
) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
if (
|
||||||
let msgContact;
|
msg.from === "status@broadcast" ||
|
||||||
|
msg.type === "location" ||
|
||||||
if (msg.fromMe) {
|
msg.author !== null // Ignore Group Messages
|
||||||
msgContact = await wbot.getContactById(msg.to);
|
) {
|
||||||
} else {
|
return;
|
||||||
msgContact = await msg.getContact();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const profilePicUrl = await msgContact.getProfilePicUrl();
|
try {
|
||||||
const contact = await verifyContact(msgContact, profilePicUrl);
|
let msgContact;
|
||||||
const ticket = await verifyTicket(contact, whatsappId);
|
|
||||||
|
|
||||||
//return if message was already created by messageController
|
if (msg.fromMe) {
|
||||||
if (msg.fromMe) {
|
msgContact = await wbot.getContactById(msg.to);
|
||||||
const alreadyExists = await Message.findOne({
|
} else {
|
||||||
where: { id: msg.id.id }
|
msgContact = await msg.getContact();
|
||||||
});
|
|
||||||
|
|
||||||
if (alreadyExists) {
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
await handleMessage(msg, ticket, contact);
|
const profilePicUrl = await msgContact.getProfilePicUrl();
|
||||||
} catch (err) {
|
const contact = await verifyContact(msgContact, profilePicUrl);
|
||||||
Sentry.captureException(err);
|
const ticket = await verifyTicket(contact, whatsappId);
|
||||||
console.log(err);
|
|
||||||
|
//return if message was already created by messageController
|
||||||
|
if (msg.fromMe) {
|
||||||
|
const alreadyExists = await Message.findOne({
|
||||||
|
where: { id: msg.id.id }
|
||||||
|
});
|
||||||
|
|
||||||
|
if (alreadyExists) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
await handleMessage(msg, ticket, contact);
|
||||||
|
} catch (err) {
|
||||||
|
Sentry.captureException(err);
|
||||||
|
console.log(err);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
});
|
);
|
||||||
|
|
||||||
wbot.on("message_ack", async (msg, ack) => {
|
wbot.on("message_ack", async (msg, ack) => {
|
||||||
try {
|
try {
|
||||||
@@ -203,4 +212,4 @@ const wbotMessageListener = whatsapp => {
|
|||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
module.exports = wbotMessageListener;
|
export default wbotMessageListener;
|
||||||
Reference in New Issue
Block a user