feat: Optimize database by reopening bot tickets instead of creating new ones

pull/22/head
adriano 2024-02-07 17:52:07 -03:00
parent 853f537361
commit 7bab4bf6b9
1 changed files with 51 additions and 6 deletions

View File

@ -89,6 +89,8 @@ import SettingTicket from "../../models/SettingTicket";
import mostRepeatedPhrase from "../../helpers/MostRepeatedPhrase"; import mostRepeatedPhrase from "../../helpers/MostRepeatedPhrase";
import ListWhatsAppsNumber from "../WhatsappService/ListWhatsAppsNumber"; import ListWhatsAppsNumber from "../WhatsappService/ListWhatsAppsNumber";
import { createObject, findObject, get } from "../../helpers/RedisClient"; import { createObject, findObject, get } from "../../helpers/RedisClient";
import FindOrCreateTicketServiceBot from "../TicketServices/FindOrCreateTicketServiceBot"
import ShowTicketService from "../TicketServices/ShowTicketService"
var lst: any[] = getWhatsappIds(); var lst: any[] = getWhatsappIds();
@ -663,12 +665,39 @@ const handleMessage = async (
) )
return; return;
const ticket = await FindOrCreateTicketService( let ticket;
contact,
wbot.id!, const _botInfo = await BotIsOnQueue("botqueue");
unreadMessages
// groupContact if (_botInfo.isOnQueue) {
); let ticket_obj: any = await FindOrCreateTicketServiceBot(
contact,
wbot.id!,
unreadMessages
// groupContact
);
ticket = ticket_obj.ticket;
if (ticket_obj.created) {
let queue = await ShowQueueService(_botInfo.botQueueId);
await UpdateTicketService({
ticketData: { queueId: queue.id },
ticketId: ticket.id
});
ticket = await ShowTicketService(ticket.id);
}
} else {
ticket = await FindOrCreateTicketService(
contact,
wbot.id!,
unreadMessages
// groupContact
);
}
if (getSettingValue("oneContactChatWithManyWhats")?.value == "disabled") { if (getSettingValue("oneContactChatWithManyWhats")?.value == "disabled") {
// Para responder para o cliente pelo mesmo whatsapp que ele enviou a mensagen // Para responder para o cliente pelo mesmo whatsapp que ele enviou a mensagen
@ -739,6 +768,22 @@ const handleMessage = async (
} else { } else {
console.log("MSG body: ", msg.body); console.log("MSG body: ", msg.body);
if (msg.type != "chat") {
botSendMessage(
ticket,
`Desculpe, nao compreendi!\nEnvie apenas texto quando estiver interagindo com o bot!\n _Digite *0* para voltar ao menu principal._`
);
return;
}
if (msg.type == "chat" && String(msg.body).length > 120) {
botSendMessage(
ticket,
`Desculpe, nao compreendi!\nTexto acima de 120 caracteres!\n _Digite *0* para voltar ao menu principal._`
);
return;
}
const menuMsg: any = await menu(msg.body, wbot.id, contact.id); const menuMsg: any = await menu(msg.body, wbot.id, contact.id);
console.log("menuMsg: ", menuMsg); console.log("menuMsg: ", menuMsg);