feat: finished multiple whatsapp support

This commit is contained in:
canove
2020-09-06 15:12:25 -03:00
parent fae02cf8e3
commit 8e8658425f

View File

@@ -7,6 +7,7 @@ const Sentry = require("@sentry/node");
const Contact = require("../models/Contact"); const Contact = require("../models/Contact");
const Ticket = require("../models/Ticket"); const Ticket = require("../models/Ticket");
const Message = require("../models/Message"); const Message = require("../models/Message");
const Whatsapp = require("../models/Whatsapp");
const { getIO } = require("../libs/socket"); const { getIO } = require("../libs/socket");
const { getWbot, initWbot } = require("../libs/wbot"); const { getWbot, initWbot } = require("../libs/wbot");
@@ -29,7 +30,7 @@ const verifyContact = async (msgContact, profilePicUrl) => {
return contact; return contact;
}; };
const verifyTicket = async contact => { const verifyTicket = async (contact, whatsappId) => {
let ticket = await Ticket.findOne({ let ticket = await Ticket.findOne({
where: { where: {
status: { status: {
@@ -57,6 +58,7 @@ const verifyTicket = async contact => {
ticket = await Ticket.create({ ticket = await Ticket.create({
contactId: contact.id, contactId: contact.id,
status: "pending", status: "pending",
whatsappId,
}); });
} }
@@ -133,11 +135,12 @@ const handleMessage = async (msg, ticket, contact) => {
}; };
const wbotMessageListener = whatsapp => { const wbotMessageListener = whatsapp => {
const wbot = getWbot(whatsapp.id); const whatsappId = whatsapp.id;
const wbot = getWbot(whatsappId);
const io = getIO(); const io = getIO();
wbot.on("message_create", async msg => { wbot.on("message_create", async msg => {
console.log(msg); // console.log(msg);
if ( if (
msg.from === "status@broadcast" || msg.from === "status@broadcast" ||
@@ -159,7 +162,7 @@ const wbotMessageListener = whatsapp => {
const profilePicUrl = await msgContact.getProfilePicUrl(); const profilePicUrl = await msgContact.getProfilePicUrl();
const contact = await verifyContact(msgContact, profilePicUrl); const contact = await verifyContact(msgContact, profilePicUrl);
const ticket = await verifyTicket(contact); const ticket = await verifyTicket(contact, whatsappId);
//return if message was already created by messageController //return if message was already created by messageController
if (msg.fromMe) { if (msg.fromMe) {