From 5073ccadf7fe55ef452998a1feb57a7fd76a3fc3 Mon Sep 17 00:00:00 2001 From: adriano Date: Tue, 11 Apr 2023 15:00:37 -0300 Subject: [PATCH] =?UTF-8?q?Corre=C3=A7=C3=A3o=20do=20bug=20tcp=5Fwrap=20qu?= =?UTF-8?q?e=20ocorriga=20quando=20um=20usuario=20era=20atualizado=20com?= =?UTF-8?q?=20um=20email=20que=20ja=20esta=20cadastrado=20no=20banco?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../UserServices/CreateUserService.ts | 4 +-- .../UserServices/UpdateUserService.ts | 27 +++++++++++++------ 2 files changed, 21 insertions(+), 10 deletions(-) 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,