Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/authorization/authorization.module.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -89,6 +90,7 @@ import { UserCacheServiceInterface } from './service/usercache.service.interface
UserRepository,
UserGroupRepository,
EntityPermissionRepository,
LoggerService,
{
provide: EntityServiceInterface,
useClass: EntityService,
Expand Down
12 changes: 11 additions & 1 deletion src/authorization/exception/group.exception.ts
Original file line number Diff line number Diff line change
@@ -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) {
Expand All @@ -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.`);
}
}
24 changes: 22 additions & 2 deletions src/authorization/service/group.service.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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';
Expand All @@ -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 {
Expand All @@ -51,6 +58,7 @@ export class GroupService implements GroupServiceInterface {
@Inject(UserCacheServiceInterface)
private userCacheService: UserCacheServiceInterface,
private searchService: SearchService,
private logger: LoggerService,
) {}

/**
Expand Down Expand Up @@ -111,7 +119,19 @@ export class GroupService implements GroupServiceInterface {
* @returns
*/
async createGroup(group: NewGroupInput): Promise<Group> {
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;
}

/**
Expand Down
1 change: 1 addition & 0 deletions src/constants/db.error.constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const DUPLICATE_ERROR_CODE = '23505';
3 changes: 3 additions & 0 deletions test/authorization/service/group.service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -74,6 +75,7 @@ describe('test Group Service', () => {
const roleRepository = Substitute.for<Repository<Role>>();
const userCacheService = Substitute.for<UserCacheServiceInterface>();
const searchService = Substitute.for<SearchService>();
const loggerService = Substitute.for<LoggerService>();
const userQueryBuilder = Substitute.for<SelectQueryBuilder<User>>();
const permissionQueryBuilder = Substitute.for<
SelectQueryBuilder<Permission>
Expand Down Expand Up @@ -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,
Expand Down