diff --git a/backend/src/controllers/ContactController.ts b/backend/src/controllers/ContactController.ts index 15721e3..7ab9363 100644 --- a/backend/src/controllers/ContactController.ts +++ b/backend/src/controllers/ContactController.ts @@ -93,7 +93,7 @@ export const store = async (req: Request, res: Response): Promise => { 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 => { 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; +}