correção da atualização do email

pull/21/head
adriano 2023-04-12 14:45:50 -03:00
parent 57b32a99e7
commit a2471fd08d
1 changed files with 20 additions and 8 deletions

View File

@ -1,7 +1,7 @@
import * as Yup from "yup"; import * as Yup from "yup";
import AppError from "../../errors/AppError"; import AppError from "../../errors/AppError";
import ShowUserService from "./ShowUserService"; import ShowUserService from "./ShowUserService";
import User from "../../models/User"; import User from "../../models/User";
interface UserData { interface UserData {
@ -31,20 +31,32 @@ const UpdateUserService = async ({
const user = await ShowUserService(userId); const user = await ShowUserService(userId);
const schema = Yup.object().shape({ const schema = Yup.object().shape({
name: Yup.string().min(2), name: Yup.string().min(2),
// email: Yup.string().min(2), // email: Yup.string().min(2),
profile: Yup.string(), profile: Yup.string(),
password: Yup.string(), password: Yup.string(),
email: Yup.string().trim().required().test( email: Yup.string().trim().required().test(
"Check-email", "Check-email",
"An user with this email already exists.", "An user with this email already exists.",
async value => { async value => {
if (!value) return false; if (!value) return false;
const emailExists = await User.findOne({
where: { email: value } const emailExists = await User.findOne({ where: { email: value }, raw: true, attributes: ['email', 'id'] });
});
return !emailExists; 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) { } catch (err: any) {
throw new AppError(err.message); throw new AppError(err.message);
} }
await user.update({ await user.update({
email, email,