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;