feat: Update to support queueId or contact_from as parameter to create ticket remote

feat-scaling-ticket-remote-creation
adriano 2024-04-02 15:33:34 -03:00
parent 7df322d1ab
commit a33ce21f44
1 changed files with 45 additions and 10 deletions

View File

@ -22,7 +22,7 @@ import format from "date-fns/format";
import ListTicketsServiceCache from "../services/TicketServices/ListTicketServiceCache"; import ListTicketsServiceCache from "../services/TicketServices/ListTicketServiceCache";
import { searchTicketCache, loadTicketsCache } from "../helpers/TicketCache"; import { searchTicketCache, loadTicketsCache } from "../helpers/TicketCache";
import { Op } from "sequelize"; import { Op, where } from "sequelize";
type IndexQuery = { type IndexQuery = {
searchParam: string; searchParam: string;
@ -119,10 +119,18 @@ export const remoteTicketCreation = async (
req: Request, req: Request,
res: Response res: Response
): Promise<Response> => { ): Promise<Response> => {
const { queueId, contact_to, msg, contact_name }: any = req.body; let { queueId, contact_from, contact_to, msg, contact_name }: any = req.body;
const validate = ["queueId", "contact_to", "msg"]; let whatsappId: any;
const validateOnlyNumber = ["queueId", "contact_to"];
if (!queueId && !contact_from) {
return res
.status(400)
.json({ error: `Property 'queueId' or 'contact_from' is required.` });
}
const validate = ["contact_to", "msg"];
const validateOnlyNumber = ["queueId", "contact_to", "contact_from"];
for (let prop of validate) { for (let prop of validate) {
if (!req.body[prop]) if (!req.body[prop])
@ -139,15 +147,42 @@ export const remoteTicketCreation = async (
} }
} }
const whatsapps = await ListWhatsAppsForQueueService(queueId, "CONNECTED"); if (queueId) {
const whatsapps = await ListWhatsAppsForQueueService(queueId, "CONNECTED");
if (!whatsapps || whatsapps?.length == 0) { if (!whatsapps || whatsapps?.length == 0) {
return res.status(500).json({ return res.status(500).json({
msg: `queueId ${queueId} does not have a WhatsApp number associated with it or the number's session is disconnected.` msg: `queueId ${queueId} does not have a WhatsApp number associated with it or the number's session is disconnected.`
});
}
const { id } = whatsapps[0];
whatsappId = id;
} else if (contact_from) {
const whatsapp = await Whatsapp.findOne({
where: { number: contact_from, status: "CONNECTED" }
}); });
}
const { id: whatsappId } = whatsapps[0]; if (!whatsapp) {
return res.status(404).json({
msg: `Whatsapp number ${contact_from} not found or disconnected!`
});
}
const { id } = whatsapp;
const { queues } = await ShowWhatsAppService(id);
if (!queues || queues.length == 0) {
return res.status(500).json({
msg: `The WhatsApp number ${contact_from} is not associated with any queue! `
});
}
queueId = queues[0].id;
whatsappId = id;
}
// const validNumber = await CheckIsValidContact(contact_to, true); // const validNumber = await CheckIsValidContact(contact_to, true);
const validNumber = contact_to; const validNumber = contact_to;