feat: add unlink user right to change profile property of user to default
							parent
							
								
									2d22a4b9f0
								
							
						
					
					
						commit
						fca4dd7036
					
				|  | @ -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; | ||||
		Loading…
	
		Reference in New Issue