Adaptação para que o envio de mensagem seja pela conexão a partir fila em que o usuário esteja adicionado

pull/1/head
adriano 2022-04-23 12:18:02 -03:00
parent 8edcd968be
commit ab498d5be1
4 changed files with 92 additions and 46 deletions

View File

@ -35,7 +35,7 @@ interface TicketData {
} }
import ListStatusChatEndService from "../services/StatusChatEndService/ListStatusChatEndService"; import ListStatusChatEndService from "../services/StatusChatEndService/ListStatusChatEndService";
export const index = async (req: Request, res: Response): Promise<Response> => { export const index = async (req: Request, res: Response): Promise<Response> => {
const { const {

View File

@ -4,6 +4,10 @@ import Whatsapp from "../models/Whatsapp";
import WhatsappQueue from "../models/WhatsappQueue" import WhatsappQueue from "../models/WhatsappQueue"
import UserQueue from "../models/UserQueue" import UserQueue from "../models/UserQueue"
import { Op, where } from "sequelize";
import wbotByUserQueue from '../helpers/GetWbotByUserQueue'
const GetDefaultWhatsApp = async (userId?: string | number ): Promise<Whatsapp> => { const GetDefaultWhatsApp = async (userId?: string | number ): Promise<Whatsapp> => {
// test del // test del
@ -11,57 +15,28 @@ const GetDefaultWhatsApp = async (userId?: string | number ): Promise<Whatsapp>
where: { isDefault: true } where: { isDefault: true }
}); });
if (!defaultWhatsapp) { if (!defaultWhatsapp) {
try{ if(userId){
if(userId){
const queue = await UserQueue.findOne(
{
where: { userId: userId },
raw:true,
attributes: ['queueId']
});
console.log('+++++++++++++++ queueId: ',queue?.queueId, ' | userId: ', userId)
if(queue?.queueId){
console.log('Usuário está adicionado à fila!')
const whatsapp = await WhatsappQueue.findOne(
{
where: { queueId: `${queue?.queueId }`},
raw:true,
attributes: ['whatsappId']
});
console.log('+++++++++++++++ whatsappId1: ',whatsapp?.whatsappId)
defaultWhatsapp = await Whatsapp.findOne({ let whatsapps = await wbotByUserQueue(userId)
where: { id: `${whatsapp?.whatsappId}` }
});
}
else{
console.log('Usuário não está adicionando a nenhuma fila e vai enviar a mensagem a partir da primeira conexao disponível!') if(whatsapps.length > 0){
defaultWhatsapp = await Whatsapp.findOne({
where: { status: 'CONNECTED' } defaultWhatsapp = whatsapps[0]
});
} }// Quando o usuário não está em nenhuma fila
}
else{ else{
defaultWhatsapp = await Whatsapp.findOne({ where: { status: 'CONNECTED' } });
defaultWhatsapp = await Whatsapp.findOne({ }
where: { status: 'CONNECTED' }
});
} }
else{
defaultWhatsapp = await Whatsapp.findOne({ where: { status: 'CONNECTED' } });
}catch(err){ }
console.log('There was an error on select a whatsapp id by user queue: ', err)
}
} }

View File

@ -0,0 +1,50 @@
import UserQueue from "../models/UserQueue";
import WhatsappQueue from "../models/WhatsappQueue";
import Whatsapp from "../models/Whatsapp";
import { Op, where } from "sequelize";
const wbotByUserQueue = async (userId: string | number, status: string = 'CONNECTED') => {
let defaultWhatsapp: Whatsapp[] = []
try{
const queue = await UserQueue.findOne(
{
where: { userId: userId },
raw:true,
attributes: ['queueId']
});
if(queue?.queueId){
// Pega todas conexões de whatsaap que estão adicionadas à uma fila
const whatsappQueues = await WhatsappQueue.findAll(
{
where: { queueId: `${queue?.queueId }`},
raw:true,
attributes: ['whatsappId']
});
defaultWhatsapp = await Whatsapp.findAll({
where: {
id: {[Op.in]: whatsappQueues.map((w) => { return w.whatsappId })},
status: status
}
});
}
}catch(err){
console.log('There was an error on select a whatsapp id by user queue: ', err)
}
return defaultWhatsapp;
}
export default wbotByUserQueue;

View File

@ -6,6 +6,11 @@ import SerializeWbotMsgId from "../../helpers/SerializeWbotMsgId";
import Message from "../../models/Message"; import Message from "../../models/Message";
import Ticket from "../../models/Ticket"; import Ticket from "../../models/Ticket";
import ShowWhatsAppService from "../WhatsappService/ShowWhatsAppService";
import wbotByUserQueue from '../../helpers/GetWbotByUserQueue'
interface Request { interface Request {
body: string; body: string;
ticket: Ticket; ticket: Ticket;
@ -23,6 +28,22 @@ const SendWhatsAppMessage = async ({
quotedMsgSerializedId = SerializeWbotMsgId(ticket, quotedMsg); quotedMsgSerializedId = SerializeWbotMsgId(ticket, quotedMsg);
} }
// test del
const whatsapp = await ShowWhatsAppService(ticket.whatsappId);
if(whatsapp.status!='CONNECTED'){
let whatsapps = await wbotByUserQueue(ticket.userId)
if(whatsapps.length > 0){
await ticket.update({ whatsappId: whatsapps[0].id });
}
}
//
const wbot = await GetTicketWbot(ticket); const wbot = await GetTicketWbot(ticket);
try { try {