1- import { Inject , Injectable } from '@nestjs/common' ;
1+ import {
2+ Inject ,
3+ Injectable ,
4+ InternalServerErrorException ,
5+ } from '@nestjs/common' ;
26import { DataSource } from 'typeorm' ;
37import { SearchEntity } from '../../constants/search.entity.enum' ;
48import {
@@ -19,6 +23,7 @@ import UserGroup from '../entity/userGroup.entity';
1923import {
2024 GroupDeleteNotAllowedException ,
2125 GroupNotFoundException ,
26+ GroupExistsException ,
2227} from '../exception/group.exception' ;
2328import { PermissionNotFoundException } from '../exception/permission.exception' ;
2429import { RoleNotFoundException } from '../exception/role.exception' ;
@@ -34,6 +39,8 @@ import { GroupServiceInterface } from './group.service.interface';
3439import { GroupCacheServiceInterface } from './groupcache.service.interface' ;
3540import SearchService from './search.service' ;
3641import { 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 ( )
3946export 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 /**
0 commit comments