diff --git a/server/src/modules/roles/service.ts b/server/src/modules/roles/service.ts index 981f0e6f57..85bc6f87d7 100644 --- a/server/src/modules/roles/service.ts +++ b/server/src/modules/roles/service.ts @@ -1,4 +1,4 @@ -import { Injectable } from '@nestjs/common'; +import { BadRequestException, Injectable } from '@nestjs/common'; import { EditUserRoleDto } from './dto'; import { RolesUtilService } from './util.service'; import { RolesRepository } from './repository'; @@ -6,6 +6,8 @@ import { IRolesService } from './interfaces/IService'; import { EntityManager } from 'typeorm'; import { dbTransactionWrap } from '@helpers/database.helper'; import { LicenseUserService } from '@modules/licensing/services/user.service'; +import { ERROR_HANDLER } from '@modules/group-permissions/constants/error'; +import { _ } from 'lodash'; @Injectable() export class RolesService implements IRolesService { @@ -16,7 +18,17 @@ export class RolesService implements IRolesService { ) {} async updateUserRole(organizationId: string, editRoleDto: EditUserRoleDto) { + const { userId, newRole } = editRoleDto; await dbTransactionWrap(async (manager: EntityManager) => { + const userRole = await this.roleRepository.getUserRole(userId, organizationId, manager); + if (_.isEmpty(userRole)) { + throw new BadRequestException(ERROR_HANDLER.ADD_GROUP_USER_NON_EXISTING_USER); + } + + if (userRole.name == newRole) { + throw new BadRequestException(ERROR_HANDLER.DEFAULT_GROUP_ADD_USER_ROLE_EXIST(newRole)); + } + editRoleDto.currentRole = userRole; await this.rolesUtilService.editDefaultGroupUserRole(organizationId, editRoleDto, manager); await this.licenseUserService.validateUser(manager); diff --git a/server/src/modules/roles/util.service.ts b/server/src/modules/roles/util.service.ts index 26733f07a1..481f1c641a 100644 --- a/server/src/modules/roles/util.service.ts +++ b/server/src/modules/roles/util.service.ts @@ -57,18 +57,8 @@ export class RolesUtilService implements IRolesUtilService { editRoleDto: EditUserRoleDto, manager?: EntityManager ): Promise { - const { newRole, userId, updatingUserId: updatedAdmin } = editRoleDto; + const { newRole, userId, updatingUserId: updatedAdmin, currentRole: userRole } = editRoleDto; return await dbTransactionWrap(async (manager: EntityManager) => { - const userRole = await this.roleRepository.getUserRole(userId, organizationId, manager); - if (_.isEmpty(userRole)) { - throw new BadRequestException(ERROR_HANDLER.ADD_GROUP_USER_NON_EXISTING_USER); - } - - if (userRole.name == newRole) { - throw new BadRequestException(ERROR_HANDLER.DEFAULT_GROUP_ADD_USER_ROLE_EXIST(newRole)); - } - editRoleDto.currentRole = userRole; - // Removing an admin if (userRole.name == USER_ROLE.ADMIN) { const groupUsers = await this.groupPermissionsRepository.getUsersInGroup(