feat: Update controller to use queueId assigned to a WhatsApp number for message sending in remote ticket creation
parent
88a71e5758
commit
7df322d1ab
|
@ -183,11 +183,17 @@ socketIo.on('connect_error', async function (err) {
|
|||
})
|
||||
//
|
||||
|
||||
const wwebVersion = '2.2402.5';
|
||||
|
||||
|
||||
//NOVA OPÇÃO MD
|
||||
client = new Client({
|
||||
authStrategy: new LocalAuth({ clientId: 'omnihit_sesssion' }),
|
||||
puppeteer: { args: ['--no-sandbox', '--disable-setuid-sandbox'], executablePath: process.env.CHROME_BIN || '/usr/bin/google-chrome-stable' },
|
||||
webVersionCache: {
|
||||
type: 'remote',
|
||||
remotePath: `https://raw.githubusercontent.com/wppconnect-team/wa-version/main/html/${wwebVersion}.html`,
|
||||
},
|
||||
})
|
||||
|
||||
client.initialize()
|
||||
|
|
|
@ -8,6 +8,9 @@ import UpdateQueueService from "../services/QueueService/UpdateQueueService";
|
|||
import Queue from "../models/Queue";
|
||||
import AppError from "../errors/AppError";
|
||||
import { del, get, set } from "../helpers/RedisClient";
|
||||
import { Op } from "sequelize";
|
||||
import ListWhatsAppsService from "../services/WhatsappService/ListWhatsAppsService";
|
||||
import Whatsapp from "../models/Whatsapp";
|
||||
|
||||
export const index = async (req: Request, res: Response): Promise<Response> => {
|
||||
const queues = await ListQueuesService();
|
||||
|
@ -15,6 +18,58 @@ export const index = async (req: Request, res: Response): Promise<Response> => {
|
|||
return res.status(200).json(queues);
|
||||
};
|
||||
|
||||
export const listQueues = async (
|
||||
req: Request,
|
||||
res: Response
|
||||
): Promise<Response> => {
|
||||
const whatsapps = await Whatsapp.findAll({
|
||||
where: {
|
||||
name: { [Op.ne]: "botqueue" },
|
||||
number: { [Op.ne]: "" },
|
||||
phoneNumberId: false
|
||||
},
|
||||
attributes: ["number"],
|
||||
include: [
|
||||
{
|
||||
model: Queue,
|
||||
as: "queues",
|
||||
attributes: ["id", "name"]
|
||||
}
|
||||
]
|
||||
});
|
||||
|
||||
const whats = whatsapps
|
||||
?.filter((w: any) => w?.queues?.length > 0)
|
||||
?.map((w: any) => {
|
||||
const { number, queues } = w;
|
||||
return {
|
||||
number,
|
||||
queues: queues?.map((q: any) => {
|
||||
const { id, name } = q;
|
||||
return { id, name };
|
||||
})
|
||||
};
|
||||
});
|
||||
|
||||
let _queues: any = [];
|
||||
|
||||
for (const w of whats) {
|
||||
const { queues } = w;
|
||||
|
||||
for (const q of queues) {
|
||||
const { id: queueId, name } = q;
|
||||
|
||||
const auxQueue = _queues.findIndex((q: any) => q.queueId == queueId);
|
||||
|
||||
if (auxQueue == -1) {
|
||||
_queues.push({ queueId, name });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return res.status(200).json(_queues);
|
||||
};
|
||||
|
||||
export const store = async (req: Request, res: Response): Promise<Response> => {
|
||||
const { name, color, greetingMessage } = req.body;
|
||||
|
||||
|
|
|
@ -119,10 +119,10 @@ export const remoteTicketCreation = async (
|
|||
req: Request,
|
||||
res: Response
|
||||
): Promise<Response> => {
|
||||
const { contact_from, contact_to, msg, contact_name }: any = req.body;
|
||||
const { queueId, contact_to, msg, contact_name }: any = req.body;
|
||||
|
||||
const validate = ["contact_from", "contact_to", "msg"];
|
||||
const validateOnlyNumber = ["contact_from", "contact_to"];
|
||||
const validate = ["queueId", "contact_to", "msg"];
|
||||
const validateOnlyNumber = ["queueId", "contact_to"];
|
||||
|
||||
for (let prop of validate) {
|
||||
if (!req.body[prop])
|
||||
|
@ -139,19 +139,15 @@ export const remoteTicketCreation = async (
|
|||
}
|
||||
}
|
||||
|
||||
const whatsapp = await Whatsapp.findOne({
|
||||
where: { number: contact_from, status: "CONNECTED" }
|
||||
const whatsapps = await ListWhatsAppsForQueueService(queueId, "CONNECTED");
|
||||
|
||||
if (!whatsapps || whatsapps?.length == 0) {
|
||||
return res.status(500).json({
|
||||
msg: `queueId ${queueId} does not have a WhatsApp number associated with it or the number's session is disconnected.`
|
||||
});
|
||||
}
|
||||
|
||||
if (whatsapp) {
|
||||
const { id: whatsappId, number, status } = whatsapp;
|
||||
|
||||
const queue: any = await WhatsappQueue.findOne({
|
||||
where: { whatsappId },
|
||||
attributes: ["queueId"]
|
||||
});
|
||||
|
||||
const { queueId } = queue;
|
||||
const { id: whatsappId } = whatsapps[0];
|
||||
|
||||
// const validNumber = await CheckIsValidContact(contact_to, true);
|
||||
const validNumber = contact_to;
|
||||
|
@ -234,14 +230,6 @@ export const remoteTicketCreation = async (
|
|||
return res
|
||||
.status(500)
|
||||
.json({ msg: `The number ${contact_to} does not exist on WhatsApp` });
|
||||
}
|
||||
|
||||
console.log(
|
||||
`REMOTE TICKET CREATION FROM ENDPOINT | STATUS: 500 | MSG: Whatsapp number ${contact_from} disconnected or it doesn't exist in omnihit`
|
||||
);
|
||||
return res.status(500).json({
|
||||
msg: `Whatsapp number ${contact_from} disconnected or it doesn't exist in omnihit`
|
||||
});
|
||||
};
|
||||
|
||||
export const store = async (req: Request, res: Response): Promise<Response> => {
|
||||
|
|
|
@ -22,7 +22,8 @@ const isAuth = (req: Request, res: Response, next: NextFunction): void => {
|
|||
const [, token] = authHeader.split(" ");
|
||||
|
||||
if (
|
||||
req.originalUrl == "/tickets/remote/create" &&
|
||||
(req.originalUrl == "/queue/remote/list" ||
|
||||
req.originalUrl == "/tickets/remote/create") &&
|
||||
token === process.env.TOKEN_REMOTE_TICKET_CREATION
|
||||
) {
|
||||
return next();
|
||||
|
|
|
@ -11,6 +11,8 @@ queueRoutes.post("/queue", isAuth, QueueController.store);
|
|||
|
||||
queueRoutes.post("/queue/customization", QueueController.customization);
|
||||
|
||||
queueRoutes.get("/queue/remote/list", isAuth, QueueController.listQueues);
|
||||
|
||||
queueRoutes.get("/queue/:queueId", isAuth, QueueController.show);
|
||||
|
||||
queueRoutes.put("/queue/:queueId", isAuth, QueueController.update);
|
||||
|
|
|
@ -6,17 +6,30 @@ const { QueryTypes } = require("sequelize");
|
|||
|
||||
const sequelize = new Sequelize(dbConfig);
|
||||
|
||||
const ListWhatsAppsForQueueService = async (queueId: number | string): Promise<any> => {
|
||||
const distinctWhatsapps = await sequelize.query(
|
||||
`SELECT w.id, w.number, w.status, wq.whatsappId, wq.queueId
|
||||
FROM Whatsapps w
|
||||
JOIN WhatsappQueues wq ON w.id = wq.whatsappId AND wq.queueId = ${queueId}
|
||||
GROUP BY w.number;`,
|
||||
const ListWhatsAppsForQueueService = async (
|
||||
queueId: number | string,
|
||||
status?: string
|
||||
): Promise<any> => {
|
||||
let distinctWhatsapps: any;
|
||||
|
||||
if (status) {
|
||||
distinctWhatsapps = await sequelize.query(
|
||||
`SELECT w.id, w.number, w.status, wq.whatsappId, wq.queueId FROM Whatsapps w
|
||||
JOIN WhatsappQueues wq ON w.id = wq.whatsappId AND wq.queueId = ${queueId} AND w.status = '${status}'
|
||||
AND phoneNumberId = false
|
||||
GROUP BY w.number;`,
|
||||
{ type: QueryTypes.SELECT }
|
||||
);
|
||||
} else {
|
||||
distinctWhatsapps = await sequelize.query(
|
||||
`SELECT w.id, w.number, w.status, wq.whatsappId, wq.queueId FROM Whatsapps w
|
||||
JOIN WhatsappQueues wq ON w.id = wq.whatsappId AND wq.queueId = ${queueId}
|
||||
GROUP BY w.number;`,
|
||||
{ type: QueryTypes.SELECT }
|
||||
);
|
||||
}
|
||||
|
||||
return distinctWhatsapps;
|
||||
};
|
||||
|
||||
export default ListWhatsAppsForQueueService;
|
||||
|
Loading…
Reference in New Issue