feat: checking for whatsapp session Id on ticket and message creation

This commit is contained in:
canove
2020-09-06 14:22:05 -03:00
parent 371477aa98
commit b793d28e60
7 changed files with 60 additions and 6 deletions

View File

@@ -1,6 +1,7 @@
const Message = require("../models/Message");
const Contact = require("../models/Contact");
const User = require("../models/User");
const Whatsapp = require("../models/Whatsapp");
const Ticket = require("../models/Ticket");
const { getIO } = require("../libs/socket");
@@ -11,7 +12,7 @@ const { MessageMedia } = require("whatsapp-web.js");
const setMessagesAsRead = async ticket => {
const io = getIO();
const wbot = getWbot();
const wbot = getWbot(ticket.whatsappId);
await Message.update(
{ read: true },
@@ -104,7 +105,6 @@ exports.index = async (req, res, next) => {
};
exports.store = async (req, res, next) => {
const wbot = getWbot();
const io = getIO();
const { ticketId } = req.params;
@@ -126,6 +126,22 @@ exports.store = async (req, res, next) => {
return res.status(404).json({ error: "No ticket found with this ID" });
}
if (!ticket.whatsappId) {
const defaultWhatsapp = await Whatsapp.findOne({
where: { default: true },
});
if (!defaultWhatsapp) {
return res
.status(404)
.json({ error: "No default WhatsApp found. Check Connection page." });
}
await ticket.setWhatsapp(defaultWhatsapp);
}
const wbot = getWbot(ticket.whatsappId);
try {
if (media) {
const newMedia = MessageMedia.fromFilePath(req.file.path);
@@ -178,7 +194,7 @@ exports.store = async (req, res, next) => {
await setMessagesAsRead(ticket);
return res.json({ newMessage, ticket });
return res.status(200).json({ newMessage, ticket });
}
return res