Added import-action classes

ref #22
This commit is contained in:
Nicolai Ort 2020-12-17 16:21:02 +01:00
parent 30952aa14f
commit a8ec0142b0
2 changed files with 78 additions and 0 deletions

View File

@ -0,0 +1,26 @@
import { IsNotEmpty, IsOptional, IsString } from 'class-validator';
export class ImportRunner {
/**
* The new runner's first name.
*/
@IsString()
@IsNotEmpty()
firstname: string;
/**
* The new runner's middle name.
* Optional.
*/
@IsString()
@IsOptional()
middlename?: string;
/**
* The new runner's last name.
*/
@IsString()
@IsNotEmpty()
lastname: string;
}

View File

@ -0,0 +1,52 @@
import { IsNotEmpty, IsString } from 'class-validator';
import { getConnectionManager } from 'typeorm';
import { RunnerOrganisationNotFoundError } from '../../errors/RunnerOrganisationErrors';
import { RunnerGroup } from '../entities/RunnerGroup';
import { RunnerOrganisation } from '../entities/RunnerOrganisation';
import { RunnerTeam } from '../entities/RunnerTeam';
import { CreateRunner } from './CreateRunner';
import { ImportRunner } from './ImportRunner';
/**
* A special class for importing runners from tabels provided by schools.
*/
export class ImportSchoolRunner extends ImportRunner {
/**
* The pupils's class.
*/
@IsString()
@IsNotEmpty()
class: string;
public async toCreateRunner(orgID: 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(orgID)).id;
return newRunner;
}
public async getGroup(orgID: number): Promise<RunnerGroup> {
if (this.class === undefined) {
throw new Error("TODO:Error for runner missing teams");
}
let org = await getConnectionManager().get().getRepository(RunnerOrganisation).findOne({ id: orgID });
if (!org) {
throw new RunnerOrganisationNotFoundError();
}
let team = await getConnectionManager().get().getRepository(RunnerTeam).findOne({ name: this.class, parentGroup: org });
if (!team) {
let newRunnerTeam: RunnerTeam = new RunnerTeam();
newRunnerTeam.name = this.class;
newRunnerTeam.parentGroup = org;
team = await getConnectionManager().get().getRepository(RunnerTeam).create(newRunnerTeam);
}
return team;
}
}