@@ -1,6 +1,13 @@
|
||||
import { IsNotEmpty, IsOptional, IsString } from 'class-validator';
|
||||
import { getConnectionManager } from 'typeorm';
|
||||
import { RunnerGroupNeededError } from '../../errors/RunnerErrors';
|
||||
import { RunnerOrganisationNotFoundError } from '../../errors/RunnerOrganisationErrors';
|
||||
import { RunnerGroup } from '../entities/RunnerGroup';
|
||||
import { RunnerOrganisation } from '../entities/RunnerOrganisation';
|
||||
import { RunnerTeam } from '../entities/RunnerTeam';
|
||||
import { CreateRunner } from './CreateRunner';
|
||||
|
||||
export abstract class ImportRunner {
|
||||
export class ImportRunner {
|
||||
|
||||
/**
|
||||
* The new runner's first name.
|
||||
@@ -24,5 +31,52 @@ export abstract class ImportRunner {
|
||||
@IsNotEmpty()
|
||||
lastname: string;
|
||||
|
||||
public abstract toCreateRunner(groupID?: number);
|
||||
/**
|
||||
* The new runner's class (if not provided otherwise).
|
||||
*/
|
||||
@IsString()
|
||||
@IsOptional()
|
||||
team?: string;
|
||||
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
public set class(value: string) {
|
||||
this.team = value;
|
||||
}
|
||||
|
||||
public async toCreateRunner(groupID: number): Promise<CreateRunner> {
|
||||
let newRunner: CreateRunner = new CreateRunner();
|
||||
|
||||
newRunner.firstname = this.firstname;
|
||||
newRunner.middlename = this.middlename;
|
||||
newRunner.lastname = this.lastname;
|
||||
newRunner.group = (await this.getGroup(groupID)).id;
|
||||
|
||||
return newRunner;
|
||||
}
|
||||
|
||||
public async getGroup(groupID: number): Promise<RunnerGroup> {
|
||||
if (this.team === undefined && groupID === undefined) {
|
||||
throw new RunnerGroupNeededError();
|
||||
}
|
||||
|
||||
let team = await getConnectionManager().get().getRepository(RunnerTeam).findOne({ id: groupID });
|
||||
if (team) { return team; }
|
||||
|
||||
let org = await getConnectionManager().get().getRepository(RunnerOrganisation).findOne({ id: groupID });
|
||||
if (!org) {
|
||||
throw new RunnerOrganisationNotFoundError();
|
||||
}
|
||||
if (this.team === undefined) { return org; }
|
||||
|
||||
team = await getConnectionManager().get().getRepository(RunnerTeam).findOne({ name: this.team, parentGroup: org });
|
||||
if (!team) {
|
||||
let newRunnerTeam: RunnerTeam = new RunnerTeam();
|
||||
newRunnerTeam.name = this.team;
|
||||
newRunnerTeam.parentGroup = org;
|
||||
team = await getConnectionManager().get().getRepository(RunnerTeam).save(newRunnerTeam);
|
||||
}
|
||||
|
||||
return team;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user