projeto-hit/backend/src/services/WbotServices/CheckIsValidContact.ts

37 lines
1.1 KiB
TypeScript
Raw Normal View History

import AppError from "../../errors/AppError";
2023-04-17 14:03:50 +00:00
import endPointQuery from "../../helpers/old_EndPointQuery";
import GetDefaultWhatsApp from "../../helpers/GetDefaultWhatsApp";
import { getWbot } from "../../libs/wbot";
const CheckIsValidContact = async (number: string): Promise<any> => {
const defaultWhatsapp = await GetDefaultWhatsApp();
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)
try {
// const isValidNumber = await wbot.isRegisteredUser(`${number}@c.us`);
if (!isValidNumber || isValidNumber && !isValidNumber.data.isValid) {
throw new AppError("invalidNumber");
}
} catch (err: any) {
if (err.message === "invalidNumber") {
throw new AppError("ERR_WAPP_INVALID_CONTACT");
}
throw new AppError("ERR_WAPP_CHECK_CONTACT");
}
if (isValidNumber && isValidNumber.data.isValid)
return isValidNumber.data.number
};
export default CheckIsValidContact;