import 'reflect-metadata' ; import { Post , Body , JsonController , Res , Authorized , Put , Param , QueryParam , Get , Delete , Req } from 'routing-controllers' ; import { DeleteRoleRequest as DeleteRoleRequest } from './requests/DeleteRoleRequest' ; import { CreateRole as CreateRoleRequest } from './requests/CreateRoleRequest' ; import { UserGroupService } from '../services/UserGroupService' ; import { UserService } from '../services/UserService' ; import { UserGroup } from '../models/UserGroup' ; @ JsonController ( '/role' ) export class RoleController { constructor ( private userGroupService , private userService ) { } api apiGroup apiParam apiParam apiHeader apiParamExample apiSuccessExample apiSampleRequest apiErrorExample @ Post ( '/create-role' ) @ Authorized ( ) public async createRole ( @ Body ( { validate : true } ) createRoleParam , @ Res ( ) response ) { console . log ( createRoleParam ) ; const role = await this . userGroupService . findOne ( { where : { name : createRoleParam . name , } , } ) ; console . log ( role ) ; if ( role ) { const errorResponse = { status : 0 , message : 'this role already exist' , } ; return response . status ( 400 ) . send ( errorResponse ) ; } const newRoleParams = new UserGroup ( ) ; newRoleParams . name = createRoleParam . name ; newRoleParams . isActive = createRoleParam . status ; const userGroupSaveResponse = await this . userGroupService . create ( newRoleParams ) ; if ( userGroupSaveResponse ) { const successResponse = { status : 1 , message : 'Role saved successfully' , data : userGroupSaveResponse , } ; return response . status ( 200 ) . send ( successResponse ) ; } else { const errorResponse = { status : 0 , message : 'unable to save Role' , } ; return response . status ( 400 ) . send ( errorResponse ) ; } } api apiGroup apiParam apiParam apiHeader apiParamExample apiSuccessExample apiSampleRequest apiErrorExample @ Put ( '/update-role/:id' ) @ Authorized ( ) public async updateRole ( @ Param ( 'id' ) id , @ Body ( { validate : true } ) createRoleParam , @ Res ( ) response ) { console . log ( createRoleParam ) ; const role = await this . userGroupService . findOne ( { where : { groupId : id , } , } ) ; console . log ( role ) ; if ( ! role ) { const errorResponse = { status : 0 , message : 'Invalid roleId' , } ; return response . status ( 400 ) . send ( errorResponse ) ; } const newRoleParams = new UserGroup ( ) ; newRoleParams . name = createRoleParam . name ; newRoleParams . isActive = createRoleParam . status ; const userGroupSaveResponse = await this . userGroupService . update ( id , newRoleParams ) ; if ( userGroupSaveResponse ) { const successResponse = { status : 1 , message : 'Role updated successfully' , data : userGroupSaveResponse , } ; return response . status ( 200 ) . send ( successResponse ) ; } else { const errorResponse = { status : 0 , message : 'unable to update Role' , } ; return response . status ( 400 ) . send ( errorResponse ) ; } } api apiGroup apiParam apiParam apiParam apiParam apiParam apiHeader apiSuccessExample apiSampleRequest apiErrorExample @ Get ( '/rolelist' ) @ Authorized ( ) public async roleList ( @ QueryParam ( 'limit' ) limit , @ QueryParam ( 'offset' ) offset , @ QueryParam ( 'keyword' ) keyword , @ QueryParam ( 'status' ) status , @ QueryParam ( 'count' ) count , @ Res ( ) response ) { console . log ( keyword ) ; const select = [ 'groupId' , 'name' , 'isActive' ] ; const whereConditions = [ { name : 'name' , op : 'like' , value : keyword , } , { name : 'isActive' , op : 'like' , value : status , } , ] ; const roleList = await this . userGroupService . list ( limit , offset , select , whereConditions , count ) ; const successResponse = { status : 1 , message : 'Successfully get all role List' , data : roleList , } ; return response . status ( 200 ) . send ( successResponse ) ; } api apiGroup apiHeader apiParam apiParamExample apiSuccessExample apiSampleRequest apiErrorExample @ Delete ( '/delete-role/:id' ) @ Authorized ( ) public async deleteRole ( @ Body ( { validate : true } ) role , @ Res ( ) response , @ Req ( ) request ) { const roleId = await this . userGroupService . findOne ( { where : { groupId : role . groupId , } , } ) ; if ( ! roleId ) { const errorResponse = { status : 0 , message : 'Invalid roleId' , } ; return response . status ( 400 ) . send ( errorResponse ) ; } const defaultRoleId = await this . userGroupService . findOne ( { where : { groupId : role . groupId , name : 'Admin' , } , } ) ; if ( defaultRoleId ) { const errorResponse = { status : 0 , message : 'You cannot delete default role' , } ; return response . status ( 400 ) . send ( errorResponse ) ; } const finduser = await this . userService . findOne ( { where : { userGroupId : role . groupId , } , } ) ; if ( finduser ) { const errorResponse = { status : 0 , message : 'Users are mapped for this role, So you cannot delete this role' , } ; return response . status ( 400 ) . send ( errorResponse ) ; } const deleteRole = await this . userGroupService . delete ( role . groupId ) ; console . log ( 'role' + deleteRole ) ; if ( deleteRole ) { const successResponse = { status : 1 , message : 'Successfully deleted role' , } ; return response . status ( 200 ) . send ( successResponse ) ; } else { const errorResponse = { status : 0 , message : 'unable to delete role' , } ; return response . status ( 400 ) . send ( errorResponse ) ; } } }