Reverted to id based relation setter

ref #13
This commit is contained in:
2020-12-05 17:04:22 +01:00
parent 4352910d54
commit 65b2399eaa
10 changed files with 34 additions and 79 deletions

View File

@@ -1,10 +1,10 @@
import { IsInt, IsOptional } from 'class-validator';
import { IsInt } from 'class-validator';
import { getConnectionManager } from 'typeorm';
import { RunnerGroupNeededError, RunnerGroupNotFoundError, RunnerOnlyOneGroupAllowedError } from '../../errors/RunnerErrors';
import { RunnerGroupNotFoundError } from '../../errors/RunnerErrors';
import { RunnerOrganisationWrongTypeError } from '../../errors/RunnerOrganisationErrors';
import { RunnerTeamNeedsParentError } from '../../errors/RunnerTeamErrors';
import { Runner } from '../entities/Runner';
import { RunnerGroup } from '../entities/RunnerGroup';
import { RunnerOrganisation } from '../entities/RunnerOrganisation';
import { RunnerTeam } from '../entities/RunnerTeam';
import { CreateParticipant } from './CreateParticipant';
export class CreateRunner extends CreateParticipant {
@@ -14,16 +14,7 @@ export class CreateRunner extends CreateParticipant {
* Either provide this or his organisation's id.
*/
@IsInt()
@IsOptional()
teamId?: number;
/**
* The new runner's organisation's id.
* Either provide this or his teams's id.
*/
@IsInt()
@IsOptional()
orgId?: number;
group: number;
/**
* Creates a Runner entity from this.
@@ -46,23 +37,15 @@ export class CreateRunner extends CreateParticipant {
* Manages all the different ways a group can be provided.
*/
public async getGroup(): Promise<RunnerGroup> {
let group: RunnerGroup;
if (this.teamId !== undefined && this.orgId !== undefined) {
throw new RunnerOnlyOneGroupAllowedError();
if (this.group === undefined) {
throw new RunnerTeamNeedsParentError();
}
if (this.teamId === undefined && this.orgId === undefined) {
throw new RunnerGroupNeededError();
if (!isNaN(this.group)) {
let group = await getConnectionManager().get().getRepository(RunnerGroup).findOne({ id: this.group });
if (!group) { throw new RunnerGroupNotFoundError; }
return group;
}
if (this.teamId) {
group = await getConnectionManager().get().getRepository(RunnerTeam).findOne({ id: this.teamId });
}
if (this.orgId) {
group = await getConnectionManager().get().getRepository(RunnerOrganisation).findOne({ id: this.orgId });
}
if (!group) {
throw new RunnerGroupNotFoundError();
}
return group;
throw new RunnerOrganisationWrongTypeError;
}
}