2022-01-06 01:26:15 +00:00
|
|
|
import * as Yup from "yup";
|
|
|
|
|
|
|
|
import AppError from "../../errors/AppError";
|
2023-04-12 17:45:50 +00:00
|
|
|
import ShowUserService from "./ShowUserService";
|
2023-04-11 18:00:37 +00:00
|
|
|
import User from "../../models/User";
|
2022-01-06 01:26:15 +00:00
|
|
|
|
|
|
|
interface UserData {
|
|
|
|
email?: string;
|
|
|
|
password?: string;
|
|
|
|
name?: string;
|
|
|
|
profile?: string;
|
|
|
|
queueIds?: number[];
|
|
|
|
}
|
|
|
|
|
|
|
|
interface Request {
|
|
|
|
userData: UserData;
|
|
|
|
userId: string | number;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface Response {
|
|
|
|
id: number;
|
|
|
|
name: string;
|
|
|
|
email: string;
|
|
|
|
profile: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
const UpdateUserService = async ({
|
|
|
|
userData,
|
|
|
|
userId
|
|
|
|
}: Request): Promise<Response | undefined> => {
|
|
|
|
const user = await ShowUserService(userId);
|
|
|
|
|
|
|
|
const schema = Yup.object().shape({
|
2023-04-12 17:45:50 +00:00
|
|
|
name: Yup.string().min(2),
|
2023-04-11 18:00:37 +00:00
|
|
|
// email: Yup.string().min(2),
|
2022-01-06 01:26:15 +00:00
|
|
|
profile: Yup.string(),
|
2023-04-11 18:00:37 +00:00
|
|
|
password: Yup.string(),
|
2023-04-12 17:45:50 +00:00
|
|
|
|
2023-04-11 18:00:37 +00:00
|
|
|
email: Yup.string().trim().required().test(
|
|
|
|
"Check-email",
|
|
|
|
"An user with this email already exists.",
|
|
|
|
async value => {
|
2023-04-12 17:45:50 +00:00
|
|
|
|
2023-04-11 18:00:37 +00:00
|
|
|
if (!value) return false;
|
2023-04-12 17:45:50 +00:00
|
|
|
|
|
|
|
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
|
2023-04-11 18:00:37 +00:00
|
|
|
}
|
|
|
|
),
|
|
|
|
|
2022-01-06 01:26:15 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
const { email, password, profile, name, queueIds = [] } = userData;
|
|
|
|
|
|
|
|
try {
|
|
|
|
await schema.validate({ email, password, profile, name });
|
2023-04-04 12:30:28 +00:00
|
|
|
} catch (err: any) {
|
2022-01-06 01:26:15 +00:00
|
|
|
throw new AppError(err.message);
|
|
|
|
}
|
2023-04-12 17:45:50 +00:00
|
|
|
|
2022-01-06 01:26:15 +00:00
|
|
|
|
|
|
|
await user.update({
|
|
|
|
email,
|
|
|
|
password,
|
|
|
|
profile,
|
|
|
|
name
|
|
|
|
});
|
|
|
|
|
|
|
|
await user.$set("queues", queueIds);
|
|
|
|
|
|
|
|
await user.reload();
|
|
|
|
|
|
|
|
const serializedUser = {
|
|
|
|
id: user.id,
|
|
|
|
name: user.name,
|
|
|
|
email: user.email,
|
|
|
|
profile: user.profile,
|
|
|
|
queues: user.queues
|
|
|
|
};
|
|
|
|
|
|
|
|
return serializedUser;
|
|
|
|
};
|
|
|
|
|
|
|
|
export default UpdateUserService;
|