feat: add check user right service
parent
26e90c6ea9
commit
097737a3b8
|
@ -0,0 +1,33 @@
|
|||
import * as Yup from "yup";
|
||||
import AppError from "../../errors/AppError";
|
||||
import ShowUserService from "./ShowUserService";
|
||||
|
||||
interface CheckUserRightServiceRequest {
|
||||
userProfileToCompare: string;
|
||||
userId: string | number;
|
||||
}
|
||||
|
||||
type CheckUserRightServiceResponse = boolean;
|
||||
|
||||
const CheckUserRightService = async ({userProfileToCompare, userId}: CheckUserRightServiceRequest): Promise<CheckUserRightServiceResponse> => {
|
||||
try {
|
||||
const user = await ShowUserService(userId);
|
||||
const schema = Yup.object().shape({
|
||||
userId: Yup.string().required(),
|
||||
userProfile: Yup.string().oneOf(['admin', 'user', 'supervisor', 'master']).required()
|
||||
});
|
||||
try {
|
||||
await schema.validate({ userId, userProfile: userProfileToCompare });
|
||||
} catch (err: any) {
|
||||
throw new AppError(err.message);
|
||||
}
|
||||
|
||||
return (user.profile == userProfileToCompare) ? true : false
|
||||
|
||||
} catch (error: any) {
|
||||
console.error('===> Error on CheckUserRightService.ts file: \n', error)
|
||||
throw new AppError(error.message);
|
||||
}
|
||||
};
|
||||
|
||||
export default CheckUserRightService;
|
Loading…
Reference in New Issue