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";
|
|
|
|
|
2023-02-07 15:47:40 +00:00
|
|
|
const CheckIsValidContact = async (number: string): Promise<any> => {
|
|
|
|
|
2022-01-06 01:26:15 +00:00
|
|
|
const defaultWhatsapp = await GetDefaultWhatsApp();
|
|
|
|
|
2023-02-07 15:47:40 +00:00
|
|
|
const wbot_url = await getWbot(defaultWhatsapp.id);
|
|
|
|
|
|
|
|
const isValidNumber = await endPointQuery(`${wbot_url}/api/validate`, { mobile: `${number}`, })
|
|
|
|
|
2023-02-23 15:13:51 +00:00
|
|
|
// console.log('isValidNumber.data.number: ', isValidNumber.data.number)
|
2022-01-06 01:26:15 +00:00
|
|
|
|
|
|
|
try {
|
2023-02-07 15:47:40 +00:00
|
|
|
|
|
|
|
// const isValidNumber = await wbot.isRegisteredUser(`${number}@c.us`);
|
|
|
|
|
|
|
|
if (!isValidNumber || isValidNumber && !isValidNumber.data.isValid) {
|
2022-01-06 01:26:15 +00:00
|
|
|
throw new AppError("invalidNumber");
|
|
|
|
}
|
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");
|
|
|
|
}
|
2023-02-07 15:47:40 +00:00
|
|
|
|
|
|
|
if (isValidNumber && isValidNumber.data.isValid)
|
|
|
|
return isValidNumber.data.number
|
|
|
|
|
2022-01-06 01:26:15 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export default CheckIsValidContact;
|