From a2471fd08d985dfb45236ab3eb6561491a499f64 Mon Sep 17 00:00:00 2001 From: adriano Date: Wed, 12 Apr 2023 14:45:50 -0300 Subject: [PATCH] =?UTF-8?q?corre=C3=A7=C3=A3o=20da=20atualiza=C3=A7=C3=A3o?= =?UTF-8?q?=20do=20email?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../UserServices/UpdateUserService.ts | 28 +++++++++++++------ 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/backend/src/services/UserServices/UpdateUserService.ts b/backend/src/services/UserServices/UpdateUserService.ts index a1fedc6..63a1d5f 100644 --- a/backend/src/services/UserServices/UpdateUserService.ts +++ b/backend/src/services/UserServices/UpdateUserService.ts @@ -1,7 +1,7 @@ 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 { @@ -31,20 +31,32 @@ const UpdateUserService = async ({ const user = await ShowUserService(userId); const schema = Yup.object().shape({ - name: Yup.string().min(2), + name: Yup.string().min(2), // email: Yup.string().min(2), profile: 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 emailExists = await User.findOne({ where: { email: value }, raw: true, attributes: ['email', 'id'] }); + + console.log('emailExists.email: ', emailExists?.email) + console.log('User.email: ', user.email) + console.log('emailExists.id: ', emailExists?.id) + console.log('User.id: ', user.id) + + if (emailExists && user.id != emailExists?.id) { + + console.error('The email already exists in another user profile!') + return !emailExists; + } + + return true } ), @@ -57,7 +69,7 @@ const UpdateUserService = async ({ } catch (err: any) { throw new AppError(err.message); } - + await user.update({ email,