2022-01-06 01:26:15 +00:00
|
|
|
import AppError from "../errors/AppError";
|
|
|
|
import Whatsapp from "../models/Whatsapp";
|
|
|
|
|
2022-04-17 21:02:15 +00:00
|
|
|
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({
|
2022-01-06 01:26:15 +00:00
|
|
|
where: { isDefault: true }
|
|
|
|
});
|
|
|
|
|
2022-04-17 21:02:15 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-01-06 01:26:15 +00:00
|
|
|
if (!defaultWhatsapp) {
|
|
|
|
throw new AppError("ERR_NO_DEF_WAPP_FOUND");
|
|
|
|
}
|
|
|
|
|
|
|
|
return defaultWhatsapp;
|
2022-04-17 21:02:15 +00:00
|
|
|
//
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// const defaultWhatsapp = await Whatsapp.findOne({
|
|
|
|
// where: { isDefault: true }
|
|
|
|
// });
|
|
|
|
|
|
|
|
// if (!defaultWhatsapp) {
|
|
|
|
// throw new AppError("ERR_NO_DEF_WAPP_FOUND");
|
|
|
|
// }
|
|
|
|
|
|
|
|
// return defaultWhatsapp;
|
|
|
|
|
|
|
|
|
2022-01-06 01:26:15 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export default GetDefaultWhatsApp;
|