2024-03-06 14:00:03 +00:00
|
|
|
import axios from "axios";
|
2022-01-06 01:26:15 +00:00
|
|
|
import AppError from "../../errors/AppError";
|
2023-02-07 15:47:40 +00:00
|
|
|
import endPointQuery from "../../helpers/EndPointQuery";
|
2022-01-06 01:26:15 +00:00
|
|
|
import GetDefaultWhatsApp from "../../helpers/GetDefaultWhatsApp";
|
|
|
|
import { getWbot } from "../../libs/wbot";
|
|
|
|
|
2024-03-06 14:00:03 +00:00
|
|
|
const CheckIsValidContact = async (
|
|
|
|
number: string,
|
|
|
|
ignoreThrow?: boolean
|
|
|
|
): Promise<any> => {
|
|
|
|
const defaultWhatsapp = await GetDefaultWhatsApp({
|
|
|
|
ignoreNoWhatsappFound: true
|
|
|
|
});
|
2023-02-07 15:47:40 +00:00
|
|
|
|
2024-03-06 14:00:03 +00:00
|
|
|
let isValidNumber;
|
2022-01-06 01:26:15 +00:00
|
|
|
|
2024-03-06 14:00:03 +00:00
|
|
|
if (defaultWhatsapp) {
|
|
|
|
const wbot_url = await getWbot(defaultWhatsapp.id);
|
2023-02-07 15:47:40 +00:00
|
|
|
|
2024-03-06 14:00:03 +00:00
|
|
|
let { data } = await endPointQuery(`${wbot_url}/api/validate`, {
|
|
|
|
mobile: `${number}`
|
|
|
|
});
|
2023-02-07 15:47:40 +00:00
|
|
|
|
2024-03-06 14:00:03 +00:00
|
|
|
if (data?.isValid) {
|
|
|
|
isValidNumber = data;
|
|
|
|
}
|
|
|
|
}
|
2022-01-06 01:26:15 +00:00
|
|
|
|
|
|
|
try {
|
2024-03-06 14:00:03 +00:00
|
|
|
if (!isValidNumber) {
|
|
|
|
|
|
|
|
const { data } = await axios.post(
|
|
|
|
`${process.env.WHATS_NUMBER_VALIDATOR_URL}/api/validate`,
|
|
|
|
{ mobile: number },
|
|
|
|
{
|
|
|
|
headers: {
|
|
|
|
"Content-Type": "application/json"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
isValidNumber = data;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ignoreThrow) return isValidNumber?.number;
|
2023-02-07 15:47:40 +00:00
|
|
|
|
2024-03-06 14:00:03 +00:00
|
|
|
if (!isValidNumber || (isValidNumber && !isValidNumber.isValid)) {
|
2022-01-06 01:26:15 +00:00
|
|
|
throw new AppError("invalidNumber");
|
|
|
|
}
|
2024-03-06 14:00:03 +00:00
|
|
|
|
|
|
|
if (isValidNumber && isValidNumber?.isValid) return isValidNumber.number;
|
2023-02-07 15:47:40 +00:00
|
|
|
} catch (err: any) {
|
2022-01-06 01:26:15 +00:00
|
|
|
if (err.message === "invalidNumber") {
|
|
|
|
throw new AppError("ERR_WAPP_INVALID_CONTACT");
|
|
|
|
}
|
2023-02-07 15:47:40 +00:00
|
|
|
|
2022-01-06 01:26:15 +00:00
|
|
|
throw new AppError("ERR_WAPP_CHECK_CONTACT");
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
export default CheckIsValidContact;
|