projeto-hit/backend/src/helpers/GetWbotByUserQueue.ts

51 lines
1.4 KiB
TypeScript
Raw Normal View History

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;