diff --git a/src/authorization/authorization.module.ts b/src/authorization/authorization.module.ts index b6cc2c7..99f89df 100644 --- a/src/authorization/authorization.module.ts +++ b/src/authorization/authorization.module.ts @@ -1,6 +1,7 @@ import { Module } from '@nestjs/common'; import { ConfigService } from '@nestjs/config'; import { TypeOrmModule } from '@nestjs/typeorm'; +import { LoggerService } from 'src/logger/logger.service'; import { AuthenticationHelper } from '../authentication/authentication.helper'; import { RedisCacheModule } from '../cache/redis-cache/redis-cache.module'; import { RedisCacheService } from '../cache/redis-cache/redis-cache.service'; @@ -89,6 +90,7 @@ import { UserCacheServiceInterface } from './service/usercache.service.interface UserRepository, UserGroupRepository, EntityPermissionRepository, + LoggerService, { provide: EntityServiceInterface, useClass: EntityService, diff --git a/src/authorization/exception/group.exception.ts b/src/authorization/exception/group.exception.ts index 555cb87..72e23e4 100644 --- a/src/authorization/exception/group.exception.ts +++ b/src/authorization/exception/group.exception.ts @@ -1,4 +1,8 @@ -import { NotFoundException, PreconditionFailedException } from '@nestjs/common'; +import { + BadRequestException, + NotFoundException, + PreconditionFailedException, +} from '@nestjs/common'; export class GroupNotFoundException extends NotFoundException { constructor(groupId: string) { @@ -10,3 +14,9 @@ export class GroupDeleteNotAllowedException extends PreconditionFailedException super(`Group cannot be deleted as it is already in use`); } } + +export class GroupExistsException extends BadRequestException { + constructor(name: string) { + super(`Group with name ${name} already exists. Cannot create this group.`); + } +} diff --git a/src/authorization/service/group.service.ts b/src/authorization/service/group.service.ts index a2ccad5..bc28c19 100644 --- a/src/authorization/service/group.service.ts +++ b/src/authorization/service/group.service.ts @@ -1,4 +1,8 @@ -import { Inject, Injectable } from '@nestjs/common'; +import { + Inject, + Injectable, + InternalServerErrorException, +} from '@nestjs/common'; import { DataSource } from 'typeorm'; import { SearchEntity } from '../../constants/search.entity.enum'; import { @@ -19,6 +23,7 @@ import UserGroup from '../entity/userGroup.entity'; import { GroupDeleteNotAllowedException, GroupNotFoundException, + GroupExistsException, } from '../exception/group.exception'; import { PermissionNotFoundException } from '../exception/permission.exception'; import { RoleNotFoundException } from '../exception/role.exception'; @@ -34,6 +39,8 @@ import { GroupServiceInterface } from './group.service.interface'; import { GroupCacheServiceInterface } from './groupcache.service.interface'; import SearchService from './search.service'; import { UserCacheServiceInterface } from './usercache.service.interface'; +import { DUPLICATE_ERROR_CODE } from '../../constants/db.error.constants'; +import { LoggerService } from '../../logger/logger.service'; @Injectable() export class GroupService implements GroupServiceInterface { @@ -51,6 +58,7 @@ export class GroupService implements GroupServiceInterface { @Inject(UserCacheServiceInterface) private userCacheService: UserCacheServiceInterface, private searchService: SearchService, + private logger: LoggerService, ) {} /** @@ -111,7 +119,19 @@ export class GroupService implements GroupServiceInterface { * @returns */ async createGroup(group: NewGroupInput): Promise { - return this.groupRepository.save(group); + let newGroup; + try { + newGroup = await this.groupRepository.save(group); + } catch (err) { + if (err.code === DUPLICATE_ERROR_CODE) { + err = new GroupExistsException(group.name); + } else { + this.logger.error(err); + err = new InternalServerErrorException('Something Went Wrong'); + } + throw err; + } + return newGroup; } /** diff --git a/src/constants/db.error.constants.ts b/src/constants/db.error.constants.ts new file mode 100644 index 0000000..ab4c33d --- /dev/null +++ b/src/constants/db.error.constants.ts @@ -0,0 +1 @@ +export const DUPLICATE_ERROR_CODE = '23505'; diff --git a/test/authorization/service/group.service.test.ts b/test/authorization/service/group.service.test.ts index de95a80..09d0dfb 100644 --- a/test/authorization/service/group.service.test.ts +++ b/test/authorization/service/group.service.test.ts @@ -2,6 +2,7 @@ import { Substitute } from '@fluffy-spoon/substitute'; import { ConfigService } from '@nestjs/config'; import { Test } from '@nestjs/testing'; import { getRepositoryToken } from '@nestjs/typeorm'; +import { LoggerService } from '../../../src/logger/logger.service'; import { DataSource, Repository, SelectQueryBuilder } from 'typeorm'; import { AuthenticationHelper } from '../../../src/authentication/authentication.helper'; import Group from '../../../src/authorization/entity/group.entity'; @@ -74,6 +75,7 @@ describe('test Group Service', () => { const roleRepository = Substitute.for>(); const userCacheService = Substitute.for(); const searchService = Substitute.for(); + const loggerService = Substitute.for(); const userQueryBuilder = Substitute.for>(); const permissionQueryBuilder = Substitute.for< SelectQueryBuilder @@ -153,6 +155,7 @@ describe('test Group Service', () => { { provide: GroupCacheServiceInterface, useValue: groupCacheService }, { provide: RedisCacheService, useValue: redisCacheService }, { provide: SearchService, useValue: searchService }, + { provide: LoggerService, useValue: loggerService }, { provide: DataSource, useValue: mockDataSource,