feat: add unlink user right to change profile property of user to default

feat-scaling-ticket-remote-creation
Henrriky 2024-03-12 17:16:07 -03:00
parent 2d22a4b9f0
commit fca4dd7036
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 UnlinkUserRightServiceRequest {
userProfile: string;
userId: string | number;
}
const UnlinkUserRightService = async ({userProfile, userId}: UnlinkUserRightServiceRequest): Promise<void> => {
try {
const user = await ShowUserService(userId);
const schema = Yup.object().shape({
userId: Yup.string().required(),
userProfile: Yup.string().oneOf(['user'])
});
try {
await schema.validate({ userId, userProfile });
} catch (err: any) {
throw new AppError(err.message);
}
await user.update({
profile: userProfile || "user"
});
await user.reload();
} catch (error: any) {
console.error('===> Error on UnlinkUserRightService.ts file: \n', error)
throw new AppError(error.message);
}
};
export default UnlinkUserRightService;