From 5fd9750d68ca6577b7432c6c8ea9638502c02633 Mon Sep 17 00:00:00 2001 From: Philip Molares Date: Sun, 31 Oct 2021 00:04:12 +0200 Subject: [PATCH] feat: add createGroup function Signed-off-by: Philip Molares --- src/groups/groups.service.spec.ts | 29 ++++++++++++++++++++++++++++- src/groups/groups.service.ts | 31 ++++++++++++++++++++++++++++++- 2 files changed, 58 insertions(+), 2 deletions(-) diff --git a/src/groups/groups.service.spec.ts b/src/groups/groups.service.spec.ts index 5292d189b..62c0559c7 100644 --- a/src/groups/groups.service.spec.ts +++ b/src/groups/groups.service.spec.ts @@ -9,7 +9,7 @@ import { getRepositoryToken } from '@nestjs/typeorm'; import { Repository } from 'typeorm'; import appConfigMock from '../config/mock/app.config.mock'; -import { NotInDBError } from '../errors/errors'; +import { AlreadyInDBError, NotInDBError } from '../errors/errors'; import { LoggerModule } from '../logger/logger.module'; import { Group } from './group.entity'; import { GroupsService } from './groups.service'; @@ -46,6 +46,33 @@ describe('GroupsService', () => { expect(service).toBeDefined(); }); + describe('createGroup', () => { + const groupName = 'testGroup'; + const displayname = 'Group Test'; + beforeEach(() => { + jest + .spyOn(groupRepo, 'save') + .mockImplementationOnce(async (group: Group): Promise => group); + }); + it('successfully creates a group', async () => { + const user = await service.createGroup(groupName, displayname); + expect(user.name).toEqual(groupName); + expect(user.displayName).toEqual(displayname); + }); + it('fails if group name is already taken', async () => { + // add additional mock implementation for failure + jest.spyOn(groupRepo, 'save').mockImplementationOnce(() => { + throw new Error(); + }); + // create first group with group name + await service.createGroup(groupName, displayname); + // attempt to create second group with group name + await expect(service.createGroup(groupName, displayname)).rejects.toThrow( + AlreadyInDBError, + ); + }); + }); + describe('getGroupByName', () => { it('works', async () => { jest.spyOn(groupRepo, 'findOne').mockResolvedValueOnce(group); diff --git a/src/groups/groups.service.ts b/src/groups/groups.service.ts index a5d35bae8..9e762fd8b 100644 --- a/src/groups/groups.service.ts +++ b/src/groups/groups.service.ts @@ -7,7 +7,7 @@ import { Injectable } from '@nestjs/common'; import { InjectRepository } from '@nestjs/typeorm'; import { Repository } from 'typeorm'; -import { NotInDBError } from '../errors/errors'; +import { AlreadyInDBError, NotInDBError } from '../errors/errors'; import { ConsoleLoggerService } from '../logger/console-logger.service'; import { GroupInfoDto } from './group-info.dto'; import { Group } from './group.entity'; @@ -21,6 +21,35 @@ export class GroupsService { this.logger.setContext(GroupsService.name); } + /** + * @async + * Create a new group with a given name and displayName + * @param name - the group name the new group shall have + * @param displayName - the display name the new group shall have + * @param special - if the group is special or not + * @return {Group} the group + * @throws {AlreadyInDBError} the group name is already taken. + */ + async createGroup( + name: string, + displayName: string, + special = false, + ): Promise { + const group = Group.create(name, displayName); + group.special = special; + try { + return await this.groupRepository.save(group); + } catch { + this.logger.debug( + `A group with the name '${name}' already exists.`, + 'createGroup', + ); + throw new AlreadyInDBError( + `A group with the name '${name}' already exists.`, + ); + } + } + /** * @async * Get a group by their name.