Backend using new tickets logic

This commit is contained in:
canove
2020-07-17 15:20:53 -03:00
parent 9687a1ce13
commit 9da6c3ceb2
3 changed files with 132 additions and 88 deletions

View File

@@ -1,4 +1,3 @@
const { validationResult } = require("express-validator");
const jwt = require("jsonwebtoken"); const jwt = require("jsonwebtoken");
const authConfig = require("../config/auth"); const authConfig = require("../config/auth");

View File

@@ -4,7 +4,19 @@ const Ticket = require("../models/Ticket");
const Contact = require("../models/Contact"); const Contact = require("../models/Contact");
exports.index = async (req, res) => { exports.index = async (req, res) => {
const { status = "" } = req.query;
let whereCondition;
if (!status) {
whereCondition = ["pending", "open"];
} else {
whereCondition = [status];
}
const tickets = await Ticket.findAll({ const tickets = await Ticket.findAll({
where: {
status: { [Sequelize.Op.or]: whereCondition },
},
include: [ include: [
{ {
model: Contact, model: Contact,

View File

@@ -1,26 +1,15 @@
const Contact = require("../models/Contact");
const Message = require("../models/Message");
const path = require("path"); const path = require("path");
const fs = require("fs"); const fs = require("fs");
const { Op } = require("sequelize");
const Contact = require("../models/Contact");
const Ticket = require("../models/Ticket");
const Message = require("../models/Message");
const { getIO } = require("../libs/socket"); const { getIO } = require("../libs/socket");
const { getWbot, init } = require("../libs/wbot"); const { getWbot, init } = require("../libs/wbot");
const wbotMessageListener = () => { const verifyContact = async (msgContact, profilePicUrl) => {
const io = getIO();
const wbot = getWbot();
wbot.on("message", async msg => {
console.log(msg);
let newMessage;
if (msg.from === "status@broadcast" || msg.type === "location") {
return;
}
try {
const msgContact = await msg.getContact();
const profilePicUrl = await msgContact.getProfilePicUrl();
try {
let contact = await Contact.findOne({ let contact = await Contact.findOne({
where: { number: msgContact.number }, where: { number: msgContact.number },
}); });
@@ -28,24 +17,39 @@ const wbotMessageListener = () => {
if (contact) { if (contact) {
await contact.update({ profilePicUrl: profilePicUrl }); await contact.update({ profilePicUrl: profilePicUrl });
} else { } else {
try {
contact = await Contact.create({ contact = await Contact.create({
name: msgContact.pushname || msgContact.number.toString(), name: msgContact.pushname || msgContact.number.toString(),
number: msgContact.number, number: msgContact.number,
profilePicUrl: profilePicUrl, profilePicUrl: profilePicUrl,
}); });
} catch (err) {
console.log(err);
}
} }
if (msg.hasQuotedMsg) { return contact;
const quotedMessage = await msg.getQuotedMessage(); };
console.log("quoted", quotedMessage);
const verifyTicket = async contact => {
let ticket = await Ticket.findOne({
where: {
status: {
[Op.or]: ["open", "pending"],
},
contactId: contact.id,
},
});
if (!ticket) {
ticket = await Ticket.create({
contactId: contact.id,
status: "pending",
});
} }
if (msg.hasMedia) { return ticket;
};
const handlMedia = async (msg, ticket) => {
const media = await msg.downloadMedia(); const media = await msg.downloadMedia();
let newMessage;
if (media) { if (media) {
if (!media.filename) { if (!media.filename) {
@@ -62,20 +66,52 @@ const wbotMessageListener = () => {
} }
); );
newMessage = await contact.createMessage({ newMessage = await ticket.createMessage({
id: msg.id.id, id: msg.id.id,
messageBody: msg.body || media.filename, body: msg.body || media.filename,
mediaUrl: media.filename, mediaUrl: media.filename,
mediaType: media.mimetype.split("/")[0], mediaType: media.mimetype.split("/")[0],
}); });
await contact.update({ lastMessage: msg.body || media.filename }); await ticket.update({ lastMessage: msg.body || media.filename });
} }
return newMessage;
};
const wbotMessageListener = () => {
const io = getIO();
const wbot = getWbot();
wbot.on("message", async msg => {
// console.log(msg);
let newMessage;
if (msg.from === "status@broadcast" || msg.type === "location") {
return;
}
try {
const msgContact = await msg.getContact();
const profilePicUrl = await msgContact.getProfilePicUrl();
const contact = await verifyContact(msgContact, profilePicUrl);
const ticket = await verifyTicket(contact);
// if (msg.hasQuotedMsg) {
// const quotedMessage = await msg.getQuotedMessage();
// console.log("quoted", quotedMessage);
// }
if (msg.hasMedia) {
newMessage = await handlMedia(msg, ticket);
} else { } else {
newMessage = await contact.createMessage({ newMessage = await ticket.createMessage({
id: msg.id.id, id: msg.id.id,
messageBody: msg.body, body: msg.body,
}); });
await contact.update({ lastMessage: msg.body }); await ticket.update({ lastMessage: msg.body });
} }
const serializedMessage = { const serializedMessage = {
@@ -87,16 +123,16 @@ const wbotMessageListener = () => {
}`, }`,
}; };
const serializaedContact = { const serializaedTicket = {
...contact.dataValues, ...ticket.dataValues,
unreadMessages: 1, unreadMessages: 1,
lastMessage: newMessage.messageBody, lastMessage: newMessage.body,
}; };
io.to(contact.id).to("notification").emit("appMessage", { io.to(ticket.id).to("notification").emit("appMessage", {
action: "create", action: "create",
message: serializedMessage, message: serializedMessage,
contact: serializaedContact, ticket: serializaedTicket,
}); });
let chat = await msg.getChat(); let chat = await msg.getChat();
@@ -104,9 +140,6 @@ const wbotMessageListener = () => {
} catch (err) { } catch (err) {
console.log(err); console.log(err);
} }
} catch (err) {
console.log(err);
}
}); });
wbot.on("message_ack", async (msg, ack) => { wbot.on("message_ack", async (msg, ack) => {
@@ -124,7 +157,7 @@ const wbotMessageListener = () => {
} }
await messageToUpdate.update({ ack: ack }); await messageToUpdate.update({ ack: ack });
io.to(messageToUpdate.contactId).emit("appMessage", { io.to(messageToUpdate.ticketId).emit("appMessage", {
action: "update", action: "update",
message: messageToUpdate, message: messageToUpdate,
}); });