feat: Update to support queueId or contact_from as parameter to create ticket remote
parent
7df322d1ab
commit
a33ce21f44
|
@ -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<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"];
|
||||
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,6 +147,7 @@ export const remoteTicketCreation = async (
|
|||
}
|
||||
}
|
||||
|
||||
if (queueId) {
|
||||
const whatsapps = await ListWhatsAppsForQueueService(queueId, "CONNECTED");
|
||||
|
||||
if (!whatsapps || whatsapps?.length == 0) {
|
||||
|
@ -147,7 +156,33 @@ export const remoteTicketCreation = async (
|
|||
});
|
||||
}
|
||||
|
||||
const { id: whatsappId } = whatsapps[0];
|
||||
const { id } = whatsapps[0];
|
||||
|
||||
whatsappId = id;
|
||||
} else if (contact_from) {
|
||||
const whatsapp = await Whatsapp.findOne({
|
||||
where: { number: contact_from, status: "CONNECTED" }
|
||||
});
|
||||
|
||||
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;
|
||||
|
|
Loading…
Reference in New Issue