2022-01-06 01:26:15 +00:00
|
|
|
import * as Yup from "yup";
|
|
|
|
|
|
|
|
import AppError from "../../errors/AppError";
|
|
|
|
import ShowUserService from "./ShowUserService";
|
|
|
|
|
|
|
|
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({
|
|
|
|
name: Yup.string().min(2),
|
2022-05-25 20:19:38 +00:00
|
|
|
|
|
|
|
email: Yup.string().min(2),
|
|
|
|
|
|
|
|
// email: Yup.string().email(),
|
|
|
|
|
2022-01-06 01:26:15 +00:00
|
|
|
profile: Yup.string(),
|
|
|
|
password: Yup.string()
|
|
|
|
});
|
|
|
|
|
|
|
|
const { email, password, profile, name, queueIds = [] } = userData;
|
|
|
|
|
|
|
|
try {
|
|
|
|
await schema.validate({ email, password, profile, name });
|
|
|
|
} catch (err) {
|
|
|
|
throw new AppError(err.message);
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|