feat: add createGroup function

Signed-off-by: Philip Molares <philip.molares@udo.edu>
This commit is contained in:
Philip Molares 2021-10-31 00:04:12 +02:00 committed by David Mehren
parent 9a8f4c0b8c
commit 5fd9750d68
No known key found for this signature in database
GPG key ID: 185982BA4C42B7C3
2 changed files with 58 additions and 2 deletions

View file

@ -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> => 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);

View file

@ -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<Group> {
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.