From fae02cf8e3ad317c97f61f47712ffc67535a18d6 Mon Sep 17 00:00:00 2001 From: canove Date: Sun, 6 Sep 2020 14:31:53 -0300 Subject: [PATCH] feat: checking default whatsapp on import and create contacts --- backend/src/controllers/ContactController.js | 13 ++++++++++++- .../controllers/ImportPhoneContactsController.js | 13 ++++++++++++- 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/backend/src/controllers/ContactController.js b/backend/src/controllers/ContactController.js index 2b0072f..268d5a3 100644 --- a/backend/src/controllers/ContactController.js +++ b/backend/src/controllers/ContactController.js @@ -2,6 +2,7 @@ const Sequelize = require("sequelize"); const { Op } = require("sequelize"); const Contact = require("../models/Contact"); +const Whatsapp = require("../models/Whatsapp"); const ContactCustomField = require("../models/ContactCustomField"); const { getIO } = require("../libs/socket"); @@ -39,7 +40,17 @@ exports.index = async (req, res) => { }; exports.store = async (req, res) => { - const wbot = getWbot(); + const defaultWhatsapp = await Whatsapp.findOne({ + where: { default: true }, + }); + + if (!defaultWhatsapp) { + return res + .status(404) + .json({ error: "No default WhatsApp found. Check Connection page." }); + } + + const wbot = getWbot(defaultWhatsapp); const io = getIO(); const newContact = req.body; diff --git a/backend/src/controllers/ImportPhoneContactsController.js b/backend/src/controllers/ImportPhoneContactsController.js index c9b79b0..b24ee0d 100644 --- a/backend/src/controllers/ImportPhoneContactsController.js +++ b/backend/src/controllers/ImportPhoneContactsController.js @@ -1,10 +1,21 @@ const Contact = require("../models/Contact"); +const Whatsapp = require("../models/Whatsapp"); const { getIO } = require("../libs/socket"); const { getWbot, initWbot } = require("../libs/wbot"); exports.store = async (req, res, next) => { + const defaultWhatsapp = await Whatsapp.findOne({ + where: { default: true }, + }); + + if (!defaultWhatsapp) { + return res + .status(404) + .json({ error: "No default WhatsApp found. Check Connection page." }); + } + const io = getIO(); - const wbot = getWbot(); + const wbot = getWbot(defaultWhatsapp); let phoneContacts;