feat: add link user right to modify profile property of user
parent
097737a3b8
commit
2d22a4b9f0
|
@ -0,0 +1,33 @@
|
|||
import * as Yup from "yup";
|
||||
import AppError from "../../errors/AppError";
|
||||
import ShowUserService from "./ShowUserService";
|
||||
|
||||
interface LinkUserRightServiceRequest {
|
||||
userProfile: string;
|
||||
userId: string | number;
|
||||
}
|
||||
const LinkUserRightService = async ({userProfile, userId}: LinkUserRightServiceRequest): Promise<void> => {
|
||||
try {
|
||||
const user = await ShowUserService(userId);
|
||||
const schema = Yup.object().shape({
|
||||
userId: Yup.string().required(),
|
||||
userProfile: Yup.string().oneOf(['admin', 'user', 'supervisor']).required()
|
||||
});
|
||||
try {
|
||||
await schema.validate({ userId, userProfile });
|
||||
} catch (err: any) {
|
||||
throw new AppError(err.message);
|
||||
}
|
||||
|
||||
await user.update({
|
||||
profile: userProfile
|
||||
});
|
||||
|
||||
await user.reload();
|
||||
} catch (error: any) {
|
||||
console.error('===> Error on LinkUserRightService.ts file: \n', error)
|
||||
throw new AppError(error.message);
|
||||
}
|
||||
};
|
||||
|
||||
export default LinkUserRightService;
|
Loading…
Reference in New Issue