feat: Automatically add '55' to the beginning of phone numbers in ContactController

pull/22/head
adriano 2024-01-29 11:34:56 -03:00
parent bc134c827c
commit 4edddd2dbb
1 changed files with 20 additions and 4 deletions

View File

@ -93,7 +93,7 @@ export const store = async (req: Request, res: Response): Promise<Response> => {
number: Yup.string()
.required()
.matches(/^\d+$/, "Invalid number format. Only numbers is allowed.")
.matches(/^55\d+$/, "The number must start with 55.")
// .matches(/^55\d+$/, "The number must start with 55.")
});
try {
@ -102,6 +102,8 @@ export const store = async (req: Request, res: Response): Promise<Response> => {
throw new AppError(err.message);
}
newContact.number = addStartPhoneNumber(newContact.number);
const validNumber = await CheckIsValidContact(newContact.number);
// const validNumber: any = await CheckContactNumber(newContact.number)
@ -157,9 +159,11 @@ export const update = async (
const schema = Yup.object().shape({
name: Yup.string(),
number: Yup.string()
.matches(/^\d+$/, "Invalid number format. Only numbers is allowed.")
.matches(/^55\d+$/, "The number must start with 55.")
number: Yup.string().matches(
/^\d+$/,
"Invalid number format. Only numbers is allowed."
)
// .matches(/^55\d+$/, "The number must start with 55.")
});
try {
@ -168,6 +172,8 @@ export const update = async (
throw new AppError(err.message);
}
contactData.number = addStartPhoneNumber(contactData.number);
await CheckIsValidContact(contactData.number);
const { contactId } = req.params;
@ -233,3 +239,13 @@ export const contacsBulkInsertOnQueue = async (
return res.status(200).json({ message: "ok" });
};
function addStartPhoneNumber(phoneNumber: string) {
const regex = /^55/;
if (!regex.test(phoneNumber)) {
phoneNumber = "55" + phoneNumber;
}
return phoneNumber;
}