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";
|
|
|
|
|
2024-03-06 14:00:03 +00:00
|
|
|
import wbotByUserQueue from "../helpers/GetWbotByUserQueue";
|
2022-06-27 06:21:04 +00:00
|
|
|
|
|
|
|
import { WhatsIndex } from "./LoadBalanceWhatsSameQueue";
|
|
|
|
|
2023-09-08 19:50:51 +00:00
|
|
|
interface Request {
|
|
|
|
userId?: string | number;
|
|
|
|
queueId?: string | number;
|
2024-03-06 14:00:03 +00:00
|
|
|
ignoreNoWhatsappFound?: boolean
|
2023-09-08 19:50:51 +00:00
|
|
|
}
|
2024-03-06 14:00:03 +00:00
|
|
|
|
2023-09-08 19:50:51 +00:00
|
|
|
const GetDefaultWhatsApp = async ({
|
|
|
|
userId,
|
2024-03-06 14:00:03 +00:00
|
|
|
queueId,
|
|
|
|
ignoreNoWhatsappFound = false
|
|
|
|
}: Request): Promise<any> => {
|
2022-04-17 21:02:15 +00:00
|
|
|
let defaultWhatsapp = await Whatsapp.findOne({
|
2022-01-06 01:26:15 +00:00
|
|
|
where: { isDefault: true }
|
|
|
|
});
|
|
|
|
|
2024-03-07 22:38:22 +00:00
|
|
|
if (!defaultWhatsapp) {
|
2022-06-27 06:21:04 +00:00
|
|
|
if (userId) {
|
2023-09-16 14:45:44 +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(
|
2023-09-16 14:45:44 +00:00
|
|
|
(w: any) => w.phoneNumberId && w.phoneNumberId.trim().length > 0
|
2023-09-08 19:50:51 +00:00
|
|
|
);
|
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
|
|
|
}
|
2024-03-07 22:38:22 +00:00
|
|
|
} else {
|
2023-09-08 19:50:51 +00:00
|
|
|
defaultWhatsapp = await Whatsapp.findOne({
|
2024-03-06 14:00:03 +00:00
|
|
|
where: { status: "CONNECTED", isOfficial: false }
|
2023-09-08 19:50:51 +00:00
|
|
|
});
|
2022-04-23 15:18:02 +00:00
|
|
|
}
|
2024-03-07 22:38:22 +00:00
|
|
|
}
|
|
|
|
|
2024-03-06 14:00:03 +00:00
|
|
|
if (!defaultWhatsapp && !ignoreNoWhatsappFound) {
|
2022-01-06 01:26:15 +00:00
|
|
|
throw new AppError("ERR_NO_DEF_WAPP_FOUND");
|
|
|
|
}
|
|
|
|
|
2024-03-06 14:00:03 +00:00
|
|
|
return defaultWhatsapp;
|
2022-01-06 01:26:15 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export default GetDefaultWhatsApp;
|