diff --git a/backend/src/services/UserServices/CreateUserService.ts b/backend/src/services/UserServices/CreateUserService.ts index fd31b80..22a3c1a 100644 --- a/backend/src/services/UserServices/CreateUserService.ts +++ b/backend/src/services/UserServices/CreateUserService.ts @@ -29,7 +29,7 @@ const CreateUserService = async ({ const schema = Yup.object().shape({ name: Yup.string().required().min(2), - email: Yup.string().required().test( + email: Yup.string().required().trim().test( "Check-email", "An user with this email already exists.", async value => { @@ -58,7 +58,7 @@ const CreateUserService = async ({ try { await schema.validate({ email, password, name }); - } catch (err) { + } catch (err:any) { throw new AppError(err.message); } diff --git a/backend/src/services/UserServices/UpdateUserService.ts b/backend/src/services/UserServices/UpdateUserService.ts index 5292e23..a1fedc6 100644 --- a/backend/src/services/UserServices/UpdateUserService.ts +++ b/backend/src/services/UserServices/UpdateUserService.ts @@ -1,7 +1,8 @@ import * as Yup from "yup"; import AppError from "../../errors/AppError"; -import ShowUserService from "./ShowUserService"; +import ShowUserService from "./ShowUserService"; +import User from "../../models/User"; interface UserData { email?: string; @@ -30,14 +31,23 @@ const UpdateUserService = async ({ const user = await ShowUserService(userId); const schema = Yup.object().shape({ - name: Yup.string().min(2), - - email: Yup.string().min(2), - - // email: Yup.string().email(), - + name: Yup.string().min(2), + // email: Yup.string().min(2), profile: Yup.string(), - password: Yup.string() + password: Yup.string(), + + email: Yup.string().trim().required().test( + "Check-email", + "An user with this email already exists.", + async value => { + if (!value) return false; + const emailExists = await User.findOne({ + where: { email: value } + }); + return !emailExists; + } + ), + }); const { email, password, profile, name, queueIds = [] } = userData; @@ -47,6 +57,7 @@ const UpdateUserService = async ({ } catch (err: any) { throw new AppError(err.message); } + await user.update({ email,