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

78 lines
1.7 KiB
TypeScript
Raw Normal View History

import AppError from "../errors/AppError";
import Whatsapp from "../models/Whatsapp";
import WhatsappQueue from "../models/WhatsappQueue"
import UserQueue from "../models/UserQueue"
const GetDefaultWhatsApp = async (userId?: string | number ): Promise<Whatsapp> => {
// test del
let defaultWhatsapp = await Whatsapp.findOne({
where: { isDefault: true }
});
if (!defaultWhatsapp) {
try{
if(userId){
const queue = await UserQueue.findOne(
{
where: { userId: userId },
raw:true,
attributes: ['queueId']
});
console.log('+++++++++++++++ queueId: ',queue?.queueId, ' | userId: ', userId)
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{
defaultWhatsapp = await Whatsapp.findOne({
where: { status: 'CONNECTED' }
});
}
}catch(err){
console.log('There was an error on select a whatsapp id by user queue: ', err)
}
}
if (!defaultWhatsapp) {
throw new AppError("ERR_NO_DEF_WAPP_FOUND");
}
return defaultWhatsapp;
//
// const defaultWhatsapp = await Whatsapp.findOne({
// where: { isDefault: true }
// });
// if (!defaultWhatsapp) {
// throw new AppError("ERR_NO_DEF_WAPP_FOUND");
// }
// return defaultWhatsapp;
};
export default GetDefaultWhatsApp;