added unread messages count to tickets

This commit is contained in:
canove
2020-09-21 11:48:53 -03:00
parent 9bd4c30d3c
commit dd9b61c2b9
2 changed files with 80 additions and 49 deletions

View File

@@ -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;

View File

@@ -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,19 +136,25 @@ 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("Wbot not initialized");
return;
}
wbot.on(
"message_create",
async (msg): Promise<void> => {
// console.log(msg); // console.log(msg);
if ( if (
msg.from === "status@broadcast" || msg.from === "status@broadcast" ||
msg.type === "location" || msg.type === "location" ||
msg.type === "call_log" || msg.author !== null // Ignore Group Messages
msg.author != null // Ignore Group Messages
) { ) {
return; return;
} }
@@ -180,7 +188,8 @@ const wbotMessageListener = whatsapp => {
Sentry.captureException(err); Sentry.captureException(err);
console.log(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;