feat: Improve code for transferring users to another agent

feat-scaling-ticket-remote-creation
adriano 2024-02-23 14:36:52 -03:00
parent 05dd0e60c0
commit 1d3a6178ba
2 changed files with 44 additions and 17 deletions

View File

@ -120,12 +120,21 @@ export const remoteTicketCreation = async (
const { contact_from, contact_to, msg, contact_name }: any = req.body;
const validate = ["contact_from", "contact_to", "msg"];
const validateOnlyNumber = ["contact_from", "contact_to"];
for (let prop of validate) {
if (!req.body[prop])
return res
.status(400)
.json({ error: `Property '${prop}' is undefined.` });
if(validateOnlyNumber.includes(prop)){
if(!(/^\d+$/.test(req.body[prop]))){
return res
.status(400)
.json({ error: `The property '${prop}' must be a number` });
}
}
}
const whatsapp = await Whatsapp.findOne({

View File

@ -497,7 +497,12 @@ const queuesOutBot = async (wbot: Session, botId: string | number) => {
return { queues, greetingMessage };
};
const transferTicket = async (queueName: any, wbot: any, ticket: Ticket) => {
const transferTicket = async (
queueName: any,
wbot: any,
ticket: Ticket,
sendGreetingMessage?: boolean
) => {
const botInfo = await BotIsOnQueue("botqueue");
console.log("kkkkkkkkkkkkkkkkkkkkk queueName: ", queueName);
@ -519,16 +524,24 @@ const transferTicket = async (queueName: any, wbot: any, ticket: Ticket) => {
queue = queues[queueName];
}
if (queue) await botTransferTicket(queue, ticket);
if (queue) await botTransferTicket(queue, ticket, sendGreetingMessage);
};
const botTransferTicket = async (queues: Queue, ticket: Ticket) => {
const botTransferTicket = async (
queues: Queue,
ticket: Ticket,
sendGreetingMessage?: boolean
) => {
await ticket.update({ userId: null });
await UpdateTicketService({
ticketData: { status: "pending", queueId: queues.id },
ticketId: ticket.id
});
if (sendGreetingMessage && queues?.greetingMessage?.length > 0) {
botSendMessage(ticket, queues.greetingMessage);
}
};
const botTransferTicketToUser = async (
@ -759,12 +772,28 @@ const handleMessage = async (
await verifyQueue(wbot, msg, ticket, contact);
}
const botInfo = await BotIsOnQueue("botqueue");
// Transfer to agent
if (!msg.fromMe) {
const filteredUsers = await findByContain("user:*", "name", msg?.body);
// O bot interage com o cliente e encaminha o atendimento para fila de atendende quando o usuário escolhe a opção falar com atendente
if (
!msg.fromMe &&
((ticket.status == "open" &&
botInfo &&
ticket.userId == +botInfo.userIdBot) ||
ticket.status == "pending" ||
ticket.status == "queueChoice")
) {
const filteredUsers = await findByContain("user:*", "name", msg?.body);
console.log("######## filteredUsers: ", filteredUsers);
if (filteredUsers && filteredUsers.length > 0) {
if (botInfo.isOnQueue) {
transferTicket(filteredUsers[0].name, wbot, ticket, true);
return;
}
const whatsappQueues = await ListWhatsappQueuesByUserQueue(
+filteredUsers[0].id
);
@ -791,12 +820,6 @@ const handleMessage = async (
}
//
// O bot interage com o cliente e encaminha o atendimento para fila de atendende quando o usuário escolhe a opção falar com atendente
//Habilitar esse caso queira usar o bot
const botInfo = await BotIsOnQueue("botqueue");
// const botInfo = { isOnQueue: false, botQueueId: 0, userIdBot: 0 };
if (
botInfo.isOnQueue &&
!msg.fromMe &&
@ -844,11 +867,6 @@ const handleMessage = async (
menuMsg?.transferToQueue &&
menuMsg.transferToQueue.trim().length > 0
) {
console.log(
"YYYYYYYYYYYYYYYYYYYY menuMsg.transferToQueue: ",
menuMsg.transferToQueue
);
transferTicket(menuMsg.transferToQueue.trim(), wbot, ticket);
}
}