Consolidated the json import for a cleaner result

ref #22
This commit is contained in:
2020-12-17 17:25:17 +01:00
parent 71228fbf33
commit 0d8fbf1eca
3 changed files with 59 additions and 62 deletions

View File

@@ -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;
}
}