Skip to content

Commit d8f8cbc

Browse files
BUG: Duplicate Error Group Message(#75)
* Added Custom Error for Duplicate Group Name * Added Constants --------- Co-authored-by: Navnit <navnit@keyvalue.systems>
1 parent 081549f commit d8f8cbc

File tree

5 files changed

+39
-3
lines changed

5 files changed

+39
-3
lines changed

src/authorization/authorization.module.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { Module } from '@nestjs/common';
22
import { ConfigService } from '@nestjs/config';
33
import { TypeOrmModule } from '@nestjs/typeorm';
4+
import { LoggerService } from 'src/logger/logger.service';
45
import { AuthenticationHelper } from '../authentication/authentication.helper';
56
import { RedisCacheModule } from '../cache/redis-cache/redis-cache.module';
67
import { RedisCacheService } from '../cache/redis-cache/redis-cache.service';
@@ -89,6 +90,7 @@ import { UserCacheServiceInterface } from './service/usercache.service.interface
8990
UserRepository,
9091
UserGroupRepository,
9192
EntityPermissionRepository,
93+
LoggerService,
9294
{
9395
provide: EntityServiceInterface,
9496
useClass: EntityService,

src/authorization/exception/group.exception.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
import { NotFoundException, PreconditionFailedException } from '@nestjs/common';
1+
import {
2+
BadRequestException,
3+
NotFoundException,
4+
PreconditionFailedException,
5+
} from '@nestjs/common';
26

37
export class GroupNotFoundException extends NotFoundException {
48
constructor(groupId: string) {
@@ -10,3 +14,9 @@ export class GroupDeleteNotAllowedException extends PreconditionFailedException
1014
super(`Group cannot be deleted as it is already in use`);
1115
}
1216
}
17+
18+
export class GroupExistsException extends BadRequestException {
19+
constructor(name: string) {
20+
super(`Group with name ${name} already exists. Cannot create this group.`);
21+
}
22+
}

src/authorization/service/group.service.ts

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
import { Inject, Injectable } from '@nestjs/common';
1+
import {
2+
Inject,
3+
Injectable,
4+
InternalServerErrorException,
5+
} from '@nestjs/common';
26
import { DataSource } from 'typeorm';
37
import { SearchEntity } from '../../constants/search.entity.enum';
48
import {
@@ -19,6 +23,7 @@ import UserGroup from '../entity/userGroup.entity';
1923
import {
2024
GroupDeleteNotAllowedException,
2125
GroupNotFoundException,
26+
GroupExistsException,
2227
} from '../exception/group.exception';
2328
import { PermissionNotFoundException } from '../exception/permission.exception';
2429
import { RoleNotFoundException } from '../exception/role.exception';
@@ -34,6 +39,8 @@ import { GroupServiceInterface } from './group.service.interface';
3439
import { GroupCacheServiceInterface } from './groupcache.service.interface';
3540
import SearchService from './search.service';
3641
import { UserCacheServiceInterface } from './usercache.service.interface';
42+
import { DUPLICATE_ERROR_CODE } from '../../constants/db.error.constants';
43+
import { LoggerService } from '../../logger/logger.service';
3744

3845
@Injectable()
3946
export class GroupService implements GroupServiceInterface {
@@ -51,6 +58,7 @@ export class GroupService implements GroupServiceInterface {
5158
@Inject(UserCacheServiceInterface)
5259
private userCacheService: UserCacheServiceInterface,
5360
private searchService: SearchService,
61+
private logger: LoggerService,
5462
) {}
5563

5664
/**
@@ -111,7 +119,19 @@ export class GroupService implements GroupServiceInterface {
111119
* @returns
112120
*/
113121
async createGroup(group: NewGroupInput): Promise<Group> {
114-
return this.groupRepository.save(group);
122+
let newGroup;
123+
try {
124+
newGroup = await this.groupRepository.save(group);
125+
} catch (err) {
126+
if (err.code === DUPLICATE_ERROR_CODE) {
127+
err = new GroupExistsException(group.name);
128+
} else {
129+
this.logger.error(err);
130+
err = new InternalServerErrorException('Something Went Wrong');
131+
}
132+
throw err;
133+
}
134+
return newGroup;
115135
}
116136

117137
/**
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const DUPLICATE_ERROR_CODE = '23505';

test/authorization/service/group.service.test.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { Substitute } from '@fluffy-spoon/substitute';
22
import { ConfigService } from '@nestjs/config';
33
import { Test } from '@nestjs/testing';
44
import { getRepositoryToken } from '@nestjs/typeorm';
5+
import { LoggerService } from '../../../src/logger/logger.service';
56
import { DataSource, Repository, SelectQueryBuilder } from 'typeorm';
67
import { AuthenticationHelper } from '../../../src/authentication/authentication.helper';
78
import Group from '../../../src/authorization/entity/group.entity';
@@ -74,6 +75,7 @@ describe('test Group Service', () => {
7475
const roleRepository = Substitute.for<Repository<Role>>();
7576
const userCacheService = Substitute.for<UserCacheServiceInterface>();
7677
const searchService = Substitute.for<SearchService>();
78+
const loggerService = Substitute.for<LoggerService>();
7779
const userQueryBuilder = Substitute.for<SelectQueryBuilder<User>>();
7880
const permissionQueryBuilder = Substitute.for<
7981
SelectQueryBuilder<Permission>
@@ -153,6 +155,7 @@ describe('test Group Service', () => {
153155
{ provide: GroupCacheServiceInterface, useValue: groupCacheService },
154156
{ provide: RedisCacheService, useValue: redisCacheService },
155157
{ provide: SearchService, useValue: searchService },
158+
{ provide: LoggerService, useValue: loggerService },
156159
{
157160
provide: DataSource,
158161
useValue: mockDataSource,

0 commit comments

Comments
 (0)