mirror of
https://github.com/cheveguerra/whaticket-community.git
synced 2026-04-21 13:19:21 +00:00
feat: checking for whatsapp session Id on ticket and message creation
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
const Message = require("../models/Message");
|
const Message = require("../models/Message");
|
||||||
const Contact = require("../models/Contact");
|
const Contact = require("../models/Contact");
|
||||||
const User = require("../models/User");
|
const User = require("../models/User");
|
||||||
|
const Whatsapp = require("../models/Whatsapp");
|
||||||
|
|
||||||
const Ticket = require("../models/Ticket");
|
const Ticket = require("../models/Ticket");
|
||||||
const { getIO } = require("../libs/socket");
|
const { getIO } = require("../libs/socket");
|
||||||
@@ -11,7 +12,7 @@ const { MessageMedia } = require("whatsapp-web.js");
|
|||||||
|
|
||||||
const setMessagesAsRead = async ticket => {
|
const setMessagesAsRead = async ticket => {
|
||||||
const io = getIO();
|
const io = getIO();
|
||||||
const wbot = getWbot();
|
const wbot = getWbot(ticket.whatsappId);
|
||||||
|
|
||||||
await Message.update(
|
await Message.update(
|
||||||
{ read: true },
|
{ read: true },
|
||||||
@@ -104,7 +105,6 @@ exports.index = async (req, res, next) => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
exports.store = async (req, res, next) => {
|
exports.store = async (req, res, next) => {
|
||||||
const wbot = getWbot();
|
|
||||||
const io = getIO();
|
const io = getIO();
|
||||||
|
|
||||||
const { ticketId } = req.params;
|
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" });
|
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 {
|
try {
|
||||||
if (media) {
|
if (media) {
|
||||||
const newMedia = MessageMedia.fromFilePath(req.file.path);
|
const newMedia = MessageMedia.fromFilePath(req.file.path);
|
||||||
@@ -178,7 +194,7 @@ exports.store = async (req, res, next) => {
|
|||||||
|
|
||||||
await setMessagesAsRead(ticket);
|
await setMessagesAsRead(ticket);
|
||||||
|
|
||||||
return res.json({ newMessage, ticket });
|
return res.status(200).json({ newMessage, ticket });
|
||||||
}
|
}
|
||||||
|
|
||||||
return res
|
return res
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ const { startOfDay, endOfDay, parseISO } = require("date-fns");
|
|||||||
const Ticket = require("../models/Ticket");
|
const Ticket = require("../models/Ticket");
|
||||||
const Contact = require("../models/Contact");
|
const Contact = require("../models/Contact");
|
||||||
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");
|
||||||
|
|
||||||
@@ -110,7 +111,18 @@ exports.index = async (req, res) => {
|
|||||||
|
|
||||||
exports.store = async (req, res) => {
|
exports.store = async (req, res) => {
|
||||||
const io = getIO();
|
const io = getIO();
|
||||||
const ticket = await Ticket.create(req.body);
|
|
||||||
|
const defaultWhatsapp = await Whatsapp.findOne({
|
||||||
|
where: { default: true },
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!defaultWhatsapp) {
|
||||||
|
return res
|
||||||
|
.status(404)
|
||||||
|
.json({ error: "No default WhatsApp found. Check Connection page." });
|
||||||
|
}
|
||||||
|
|
||||||
|
const ticket = await defaultWhatsapp.createTicket(req.body);
|
||||||
|
|
||||||
const contact = await ticket.getContact();
|
const contact = await ticket.getContact();
|
||||||
|
|
||||||
|
|||||||
@@ -20,7 +20,6 @@ exports.store = async (req, res) => {
|
|||||||
"Check-default",
|
"Check-default",
|
||||||
"Only one default whatsapp is permited",
|
"Only one default whatsapp is permited",
|
||||||
async value => {
|
async value => {
|
||||||
// console.log("cai no if", value);
|
|
||||||
if (value === true) {
|
if (value === true) {
|
||||||
const whatsappFound = await Whatsapp.findOne({
|
const whatsappFound = await Whatsapp.findOne({
|
||||||
where: { default: true },
|
where: { default: true },
|
||||||
@@ -88,6 +87,8 @@ exports.update = async (req, res) => {
|
|||||||
});
|
});
|
||||||
if (whatsappFound) {
|
if (whatsappFound) {
|
||||||
return !(whatsappFound.id !== +whatsappId);
|
return !(whatsappFound.id !== +whatsappId);
|
||||||
|
} else {
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
} else return true;
|
} else return true;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,16 @@
|
|||||||
|
"use strict";
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
up: (queryInterface, Sequelize) => {
|
||||||
|
return queryInterface.addColumn("Tickets", "whatsappId", {
|
||||||
|
type: Sequelize.INTEGER,
|
||||||
|
references: { model: "Whatsapps", key: "id" },
|
||||||
|
onUpdate: "CASCADE",
|
||||||
|
onDelete: "SET NULL",
|
||||||
|
});
|
||||||
|
},
|
||||||
|
|
||||||
|
down: queryInterface => {
|
||||||
|
return queryInterface.removeColumn("Tickets", "whatsappId");
|
||||||
|
},
|
||||||
|
};
|
||||||
@@ -90,7 +90,8 @@ module.exports = {
|
|||||||
const sessionIndex = sessions.findIndex(s => s.id === whatsappId);
|
const sessionIndex = sessions.findIndex(s => s.id === whatsappId);
|
||||||
|
|
||||||
if (sessionIndex === -1) {
|
if (sessionIndex === -1) {
|
||||||
throw new Error("This Wbot session is not initialized");
|
console.log("This Wbot session is not initialized");
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
return sessions[sessionIndex];
|
return sessions[sessionIndex];
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -39,6 +39,10 @@ class Ticket extends Sequelize.Model {
|
|||||||
static associate(models) {
|
static associate(models) {
|
||||||
this.belongsTo(models.Contact, { foreignKey: "contactId", as: "contact" });
|
this.belongsTo(models.Contact, { foreignKey: "contactId", as: "contact" });
|
||||||
this.belongsTo(models.User, { foreignKey: "userId", as: "user" });
|
this.belongsTo(models.User, { foreignKey: "userId", as: "user" });
|
||||||
|
this.belongsTo(models.Whatsapp, {
|
||||||
|
foreignKey: "whatsappId",
|
||||||
|
as: "whatsapp",
|
||||||
|
});
|
||||||
this.hasMany(models.Message, { foreignKey: "ticketId", as: "messages" });
|
this.hasMany(models.Message, { foreignKey: "ticketId", as: "messages" });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -23,6 +23,10 @@ class Whatsapp extends Sequelize.Model {
|
|||||||
|
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static associate(models) {
|
||||||
|
this.hasMany(models.Ticket, { foreignKey: "whatsappId", as: "tickets" });
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = Whatsapp;
|
module.exports = Whatsapp;
|
||||||
|
|||||||
Reference in New Issue
Block a user