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"
|
|
|
|
|
2022-04-23 15:18:02 +00:00
|
|
|
import { Op, where } from "sequelize";
|
|
|
|
|
|
|
|
import wbotByUserQueue from '../helpers/GetWbotByUserQueue'
|
|
|
|
|
2022-06-27 06:21:04 +00:00
|
|
|
// import WhatsQueueIndex from "./WhatsQueueIndex";
|
|
|
|
|
|
|
|
import { WhatsIndex } from "./LoadBalanceWhatsSameQueue";
|
|
|
|
|
|
|
|
const GetDefaultWhatsApp = async (userId?: string | number): Promise<Whatsapp> => {
|
|
|
|
|
2022-04-17 21:02:15 +00:00
|
|
|
// test del
|
|
|
|
let defaultWhatsapp = await Whatsapp.findOne({
|
2022-01-06 01:26:15 +00:00
|
|
|
where: { isDefault: true }
|
|
|
|
});
|
|
|
|
|
2022-06-27 06:21:04 +00:00
|
|
|
if (!defaultWhatsapp) {
|
|
|
|
|
|
|
|
|
|
|
|
if (userId) {
|
2022-04-17 21:02:15 +00:00
|
|
|
|
2022-06-27 06:21:04 +00:00
|
|
|
let whatsapps = await wbotByUserQueue(userId)
|
2022-04-17 21:02:15 +00:00
|
|
|
|
2022-06-27 06:21:04 +00:00
|
|
|
if (whatsapps.length > 0) {
|
|
|
|
|
|
|
|
if (whatsapps.length > 1) {
|
|
|
|
|
|
|
|
defaultWhatsapp = whatsapps[+WhatsIndex(whatsapps)]
|
|
|
|
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
defaultWhatsapp = whatsapps[0]
|
|
|
|
}
|
2022-04-18 18:21:28 +00:00
|
|
|
|
2022-04-23 15:18:02 +00:00
|
|
|
}// Quando o usuário não está em nenhuma fila
|
2022-06-27 06:21:04 +00:00
|
|
|
else {
|
|
|
|
defaultWhatsapp = await Whatsapp.findOne({ where: { status: 'CONNECTED' } });
|
2022-04-23 15:18:02 +00:00
|
|
|
}
|
2022-06-27 06:21:04 +00:00
|
|
|
|
|
|
|
|
2022-04-23 15:18:02 +00:00
|
|
|
}
|
2022-06-27 06:21:04 +00:00
|
|
|
else {
|
2022-04-23 15:18:02 +00:00
|
|
|
|
2022-06-27 06:21:04 +00:00
|
|
|
defaultWhatsapp = await Whatsapp.findOne({ where: { status: 'CONNECTED' } });
|
|
|
|
|
|
|
|
}
|
2022-04-17 21:02:15 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
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
|
|
|
//
|
|
|
|
|
|
|
|
|
|
|
|
|
2022-06-27 06:21:04 +00:00
|
|
|
// const defaultWhatsapp = await Whatsapp.findOne({
|
2022-04-17 21:02:15 +00:00
|
|
|
// 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;
|