2022-01-06 01:26:15 +00:00
|
|
|
import AppError from "../errors/AppError";
|
|
|
|
import Whatsapp from "../models/Whatsapp";
|
|
|
|
|
2023-09-08 19:50:51 +00:00
|
|
|
import WhatsappQueue from "../models/WhatsappQueue";
|
|
|
|
import UserQueue from "../models/UserQueue";
|
2022-04-17 21:02:15 +00:00
|
|
|
|
2022-04-23 15:18:02 +00:00
|
|
|
import { Op, where } from "sequelize";
|
|
|
|
|
2023-09-08 19:50:51 +00:00
|
|
|
import wbotByUserQueue from "../helpers/GetWbotByUserQueue";
|
2022-04-23 15:18:02 +00:00
|
|
|
|
2022-06-27 06:21:04 +00:00
|
|
|
// import WhatsQueueIndex from "./WhatsQueueIndex";
|
|
|
|
|
|
|
|
import { WhatsIndex } from "./LoadBalanceWhatsSameQueue";
|
|
|
|
|
2023-09-08 19:50:51 +00:00
|
|
|
interface Request {
|
|
|
|
userId?: string | number;
|
|
|
|
queueId?: string | number;
|
|
|
|
}
|
2022-06-27 06:21:04 +00:00
|
|
|
|
2023-09-08 19:50:51 +00:00
|
|
|
//const GetDefaultWhatsApp = async (userId?: string | number): Promise<Whatsapp> => {
|
|
|
|
|
|
|
|
const GetDefaultWhatsApp = async ({
|
|
|
|
userId,
|
|
|
|
queueId
|
|
|
|
}: Request): Promise<any> => {
|
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) {
|
2023-09-08 19:50:51 +00:00
|
|
|
let whatsapps = await wbotByUserQueue({ userId, queueId });
|
2022-04-17 21:02:15 +00:00
|
|
|
|
2023-09-08 19:50:51 +00:00
|
|
|
if (userId && queueId) {
|
|
|
|
if (whatsapps.length > 1) {
|
|
|
|
let whatsAppOfficial: any = whatsapps.find(
|
|
|
|
(w: any) => w.phoneNumberId != null
|
|
|
|
);
|
2022-04-17 21:02:15 +00:00
|
|
|
|
2023-09-08 19:50:51 +00:00
|
|
|
if (whatsAppOfficial) {
|
|
|
|
return whatsapps;
|
|
|
|
}
|
2022-06-27 06:21:04 +00:00
|
|
|
}
|
2023-09-08 19:50:51 +00:00
|
|
|
}
|
2022-04-18 18:21:28 +00:00
|
|
|
|
2023-09-08 19:50:51 +00:00
|
|
|
if (whatsapps.length > 0) {
|
|
|
|
if (whatsapps.length > 1) {
|
|
|
|
defaultWhatsapp = whatsapps[+WhatsIndex(whatsapps)];
|
|
|
|
} else {
|
|
|
|
defaultWhatsapp = whatsapps[0];
|
|
|
|
}
|
|
|
|
} // Quando o usuário não está em nenhuma fila
|
2022-06-27 06:21:04 +00:00
|
|
|
else {
|
2023-09-08 19:50:51 +00:00
|
|
|
defaultWhatsapp = await Whatsapp.findOne({
|
|
|
|
where: { status: "CONNECTED" }
|
|
|
|
});
|
2022-04-23 15:18:02 +00:00
|
|
|
}
|
2023-09-08 19:50:51 +00:00
|
|
|
} else {
|
|
|
|
defaultWhatsapp = await Whatsapp.findOne({
|
|
|
|
where: { status: "CONNECTED" }
|
|
|
|
});
|
2022-04-23 15:18:02 +00:00
|
|
|
}
|
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-01-06 01:26:15 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export default GetDefaultWhatsApp;
|