From a33ce21f4430940774f82ce130ae0826cb2cb8ed Mon Sep 17 00:00:00 2001 From: adriano Date: Tue, 2 Apr 2024 15:33:34 -0300 Subject: [PATCH] feat: Update to support queueId or contact_from as parameter to create ticket remote --- backend/src/controllers/TicketController.ts | 55 +++++++++++++++++---- 1 file changed, 45 insertions(+), 10 deletions(-) diff --git a/backend/src/controllers/TicketController.ts b/backend/src/controllers/TicketController.ts index 549e460..0a60271 100644 --- a/backend/src/controllers/TicketController.ts +++ b/backend/src/controllers/TicketController.ts @@ -22,7 +22,7 @@ import format from "date-fns/format"; import ListTicketsServiceCache from "../services/TicketServices/ListTicketServiceCache"; import { searchTicketCache, loadTicketsCache } from "../helpers/TicketCache"; -import { Op } from "sequelize"; +import { Op, where } from "sequelize"; type IndexQuery = { searchParam: string; @@ -119,10 +119,18 @@ export const remoteTicketCreation = async ( req: Request, res: Response ): Promise => { - 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"]; - const validateOnlyNumber = ["queueId", "contact_to"]; + let whatsappId: any; + + 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) { 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) { - 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 (!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.` + }); + } + + 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 = contact_to;