user-role

This commit is contained in:
Rudra deep Biswas 2025-05-13 17:08:09 +05:30
parent 592447dc83
commit 6e6b721fca
2 changed files with 14 additions and 12 deletions

View file

@ -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);

View file

@ -57,18 +57,8 @@ export class RolesUtilService implements IRolesUtilService {
editRoleDto: EditUserRoleDto,
manager?: EntityManager
): Promise<void> {
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(