34 lines
939 B
TypeScript
34 lines
939 B
TypeScript
|
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;
|