feat: add check user right service

feat-scaling-ticket-remote-creation
Henrriky 2024-03-12 17:11:26 -03:00
parent 26e90c6ea9
commit 097737a3b8
1 changed files with 33 additions and 0 deletions

View File

@ -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;