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

@ -4,6 +4,10 @@ import Whatsapp from "../models/Whatsapp";
import WhatsappQueue from "../models/WhatsappQueue"
import UserQueue from "../models/UserQueue"
import { Op, where } from "sequelize";
import wbotByUserQueue from '../helpers/GetWbotByUserQueue'
const GetDefaultWhatsApp = async (userId?: string | number ): Promise<Whatsapp> => {
// test del
@ -13,55 +17,26 @@ const GetDefaultWhatsApp = async (userId?: string | number ): Promise<Whatsapp>
if (!defaultWhatsapp) {
try{
if(userId){
const queue = await UserQueue.findOne(
{
where: { userId: userId },
raw:true,
attributes: ['queueId']
});
let whatsapps = await wbotByUserQueue(userId)
console.log('+++++++++++++++ queueId: ',queue?.queueId, ' | userId: ', userId)
if(whatsapps.length > 0){
if(queue?.queueId){
defaultWhatsapp = whatsapps[0]
console.log('Usuário está adicionado à fila!')
}// Quando o usuário não está em nenhuma fila
else{
defaultWhatsapp = await Whatsapp.findOne({ where: { status: 'CONNECTED' } });
}
const whatsapp = await WhatsappQueue.findOne(
{
where: { queueId: `${queue?.queueId }`},
raw:true,
attributes: ['whatsappId']
});
console.log('+++++++++++++++ whatsappId1: ',whatsapp?.whatsappId)
defaultWhatsapp = await Whatsapp.findOne({
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!')
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 Ticket from "../../models/Ticket";
import ShowWhatsAppService from "../WhatsappService/ShowWhatsAppService";
import wbotByUserQueue from '../../helpers/GetWbotByUserQueue'
interface Request {
body: string;
ticket: Ticket;
@ -23,6 +28,22 @@ const SendWhatsAppMessage = async ({
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);
try {