mirror of
https://github.com/cheveguerra/whaticket-community.git
synced 2026-04-20 04:39:20 +00:00
Backend using new tickets logic
This commit is contained in:
@@ -1,4 +1,3 @@
|
|||||||
const { validationResult } = require("express-validator");
|
|
||||||
const jwt = require("jsonwebtoken");
|
const jwt = require("jsonwebtoken");
|
||||||
const authConfig = require("../config/auth");
|
const authConfig = require("../config/auth");
|
||||||
|
|
||||||
|
|||||||
@@ -4,7 +4,19 @@ const Ticket = require("../models/Ticket");
|
|||||||
const Contact = require("../models/Contact");
|
const Contact = require("../models/Contact");
|
||||||
|
|
||||||
exports.index = async (req, res) => {
|
exports.index = async (req, res) => {
|
||||||
|
const { status = "" } = req.query;
|
||||||
|
|
||||||
|
let whereCondition;
|
||||||
|
if (!status) {
|
||||||
|
whereCondition = ["pending", "open"];
|
||||||
|
} else {
|
||||||
|
whereCondition = [status];
|
||||||
|
}
|
||||||
|
|
||||||
const tickets = await Ticket.findAll({
|
const tickets = await Ticket.findAll({
|
||||||
|
where: {
|
||||||
|
status: { [Sequelize.Op.or]: whereCondition },
|
||||||
|
},
|
||||||
include: [
|
include: [
|
||||||
{
|
{
|
||||||
model: Contact,
|
model: Contact,
|
||||||
|
|||||||
@@ -1,109 +1,142 @@
|
|||||||
const Contact = require("../models/Contact");
|
|
||||||
const Message = require("../models/Message");
|
|
||||||
|
|
||||||
const path = require("path");
|
const path = require("path");
|
||||||
const fs = require("fs");
|
const fs = require("fs");
|
||||||
|
const { Op } = require("sequelize");
|
||||||
|
|
||||||
|
const Contact = require("../models/Contact");
|
||||||
|
const Ticket = require("../models/Ticket");
|
||||||
|
const Message = require("../models/Message");
|
||||||
|
|
||||||
const { getIO } = require("../libs/socket");
|
const { getIO } = require("../libs/socket");
|
||||||
const { getWbot, init } = require("../libs/wbot");
|
const { getWbot, init } = require("../libs/wbot");
|
||||||
|
|
||||||
|
const verifyContact = async (msgContact, profilePicUrl) => {
|
||||||
|
let contact = await Contact.findOne({
|
||||||
|
where: { number: msgContact.number },
|
||||||
|
});
|
||||||
|
|
||||||
|
if (contact) {
|
||||||
|
await contact.update({ profilePicUrl: profilePicUrl });
|
||||||
|
} else {
|
||||||
|
contact = await Contact.create({
|
||||||
|
name: msgContact.pushname || msgContact.number.toString(),
|
||||||
|
number: msgContact.number,
|
||||||
|
profilePicUrl: profilePicUrl,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
return contact;
|
||||||
|
};
|
||||||
|
|
||||||
|
const verifyTicket = async contact => {
|
||||||
|
let ticket = await Ticket.findOne({
|
||||||
|
where: {
|
||||||
|
status: {
|
||||||
|
[Op.or]: ["open", "pending"],
|
||||||
|
},
|
||||||
|
contactId: contact.id,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!ticket) {
|
||||||
|
ticket = await Ticket.create({
|
||||||
|
contactId: contact.id,
|
||||||
|
status: "pending",
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
return ticket;
|
||||||
|
};
|
||||||
|
|
||||||
|
const handlMedia = async (msg, ticket) => {
|
||||||
|
const media = await msg.downloadMedia();
|
||||||
|
let newMessage;
|
||||||
|
|
||||||
|
if (media) {
|
||||||
|
if (!media.filename) {
|
||||||
|
let ext = media.mimetype.split("/")[1].split(";")[0];
|
||||||
|
media.filename = `${new Date().getTime()}.${ext}`;
|
||||||
|
}
|
||||||
|
|
||||||
|
fs.writeFile(
|
||||||
|
path.join(__dirname, "..", "public", media.filename),
|
||||||
|
media.data,
|
||||||
|
"base64",
|
||||||
|
err => {
|
||||||
|
console.log(err);
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
newMessage = await ticket.createMessage({
|
||||||
|
id: msg.id.id,
|
||||||
|
body: msg.body || media.filename,
|
||||||
|
mediaUrl: media.filename,
|
||||||
|
mediaType: media.mimetype.split("/")[0],
|
||||||
|
});
|
||||||
|
await ticket.update({ lastMessage: msg.body || media.filename });
|
||||||
|
}
|
||||||
|
|
||||||
|
return newMessage;
|
||||||
|
};
|
||||||
|
|
||||||
const wbotMessageListener = () => {
|
const wbotMessageListener = () => {
|
||||||
const io = getIO();
|
const io = getIO();
|
||||||
const wbot = getWbot();
|
const wbot = getWbot();
|
||||||
|
|
||||||
wbot.on("message", async msg => {
|
wbot.on("message", async msg => {
|
||||||
console.log(msg);
|
// console.log(msg);
|
||||||
|
|
||||||
let newMessage;
|
let newMessage;
|
||||||
|
|
||||||
if (msg.from === "status@broadcast" || msg.type === "location") {
|
if (msg.from === "status@broadcast" || msg.type === "location") {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const msgContact = await msg.getContact();
|
const msgContact = await msg.getContact();
|
||||||
const profilePicUrl = await msgContact.getProfilePicUrl();
|
const profilePicUrl = await msgContact.getProfilePicUrl();
|
||||||
try {
|
|
||||||
let contact = await Contact.findOne({
|
const contact = await verifyContact(msgContact, profilePicUrl);
|
||||||
where: { number: msgContact.number },
|
|
||||||
|
const ticket = await verifyTicket(contact);
|
||||||
|
|
||||||
|
// if (msg.hasQuotedMsg) {
|
||||||
|
// const quotedMessage = await msg.getQuotedMessage();
|
||||||
|
// console.log("quoted", quotedMessage);
|
||||||
|
// }
|
||||||
|
|
||||||
|
if (msg.hasMedia) {
|
||||||
|
newMessage = await handlMedia(msg, ticket);
|
||||||
|
} else {
|
||||||
|
newMessage = await ticket.createMessage({
|
||||||
|
id: msg.id.id,
|
||||||
|
body: msg.body,
|
||||||
});
|
});
|
||||||
|
await ticket.update({ lastMessage: msg.body });
|
||||||
if (contact) {
|
|
||||||
await contact.update({ profilePicUrl: profilePicUrl });
|
|
||||||
} else {
|
|
||||||
try {
|
|
||||||
contact = await Contact.create({
|
|
||||||
name: msgContact.pushname || msgContact.number.toString(),
|
|
||||||
number: msgContact.number,
|
|
||||||
profilePicUrl: profilePicUrl,
|
|
||||||
});
|
|
||||||
} catch (err) {
|
|
||||||
console.log(err);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (msg.hasQuotedMsg) {
|
|
||||||
const quotedMessage = await msg.getQuotedMessage();
|
|
||||||
console.log("quoted", quotedMessage);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (msg.hasMedia) {
|
|
||||||
const media = await msg.downloadMedia();
|
|
||||||
|
|
||||||
if (media) {
|
|
||||||
if (!media.filename) {
|
|
||||||
let ext = media.mimetype.split("/")[1].split(";")[0];
|
|
||||||
media.filename = `${new Date().getTime()}.${ext}`;
|
|
||||||
}
|
|
||||||
|
|
||||||
fs.writeFile(
|
|
||||||
path.join(__dirname, "..", "public", media.filename),
|
|
||||||
media.data,
|
|
||||||
"base64",
|
|
||||||
err => {
|
|
||||||
console.log(err);
|
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|
||||||
newMessage = await contact.createMessage({
|
|
||||||
id: msg.id.id,
|
|
||||||
messageBody: msg.body || media.filename,
|
|
||||||
mediaUrl: media.filename,
|
|
||||||
mediaType: media.mimetype.split("/")[0],
|
|
||||||
});
|
|
||||||
await contact.update({ lastMessage: msg.body || media.filename });
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
newMessage = await contact.createMessage({
|
|
||||||
id: msg.id.id,
|
|
||||||
messageBody: msg.body,
|
|
||||||
});
|
|
||||||
await contact.update({ lastMessage: msg.body });
|
|
||||||
}
|
|
||||||
|
|
||||||
const serializedMessage = {
|
|
||||||
...newMessage.dataValues,
|
|
||||||
mediaUrl: `${
|
|
||||||
newMessage.mediaUrl
|
|
||||||
? `http://${process.env.HOST}:${process.env.PORT}/public/${newMessage.mediaUrl}`
|
|
||||||
: ""
|
|
||||||
}`,
|
|
||||||
};
|
|
||||||
|
|
||||||
const serializaedContact = {
|
|
||||||
...contact.dataValues,
|
|
||||||
unreadMessages: 1,
|
|
||||||
lastMessage: newMessage.messageBody,
|
|
||||||
};
|
|
||||||
|
|
||||||
io.to(contact.id).to("notification").emit("appMessage", {
|
|
||||||
action: "create",
|
|
||||||
message: serializedMessage,
|
|
||||||
contact: serializaedContact,
|
|
||||||
});
|
|
||||||
|
|
||||||
let chat = await msg.getChat();
|
|
||||||
chat.sendSeen();
|
|
||||||
} catch (err) {
|
|
||||||
console.log(err);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const serializedMessage = {
|
||||||
|
...newMessage.dataValues,
|
||||||
|
mediaUrl: `${
|
||||||
|
newMessage.mediaUrl
|
||||||
|
? `http://${process.env.HOST}:${process.env.PORT}/public/${newMessage.mediaUrl}`
|
||||||
|
: ""
|
||||||
|
}`,
|
||||||
|
};
|
||||||
|
|
||||||
|
const serializaedTicket = {
|
||||||
|
...ticket.dataValues,
|
||||||
|
unreadMessages: 1,
|
||||||
|
lastMessage: newMessage.body,
|
||||||
|
};
|
||||||
|
|
||||||
|
io.to(ticket.id).to("notification").emit("appMessage", {
|
||||||
|
action: "create",
|
||||||
|
message: serializedMessage,
|
||||||
|
ticket: serializaedTicket,
|
||||||
|
});
|
||||||
|
|
||||||
|
let chat = await msg.getChat();
|
||||||
|
chat.sendSeen();
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.log(err);
|
console.log(err);
|
||||||
}
|
}
|
||||||
@@ -124,7 +157,7 @@ const wbotMessageListener = () => {
|
|||||||
}
|
}
|
||||||
await messageToUpdate.update({ ack: ack });
|
await messageToUpdate.update({ ack: ack });
|
||||||
|
|
||||||
io.to(messageToUpdate.contactId).emit("appMessage", {
|
io.to(messageToUpdate.ticketId).emit("appMessage", {
|
||||||
action: "update",
|
action: "update",
|
||||||
message: messageToUpdate,
|
message: messageToUpdate,
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user