From a8ec0142b06eb367e496d395a74105cd7df77d06 Mon Sep 17 00:00:00 2001 From: Nicolai Ort Date: Thu, 17 Dec 2020 16:21:02 +0100 Subject: [PATCH] Added import-action classes ref #22 --- src/models/actions/ImportRunner.ts | 26 ++++++++++++ src/models/actions/ImportSchoolRunner.ts | 52 ++++++++++++++++++++++++ 2 files changed, 78 insertions(+) create mode 100644 src/models/actions/ImportRunner.ts create mode 100644 src/models/actions/ImportSchoolRunner.ts diff --git a/src/models/actions/ImportRunner.ts b/src/models/actions/ImportRunner.ts new file mode 100644 index 0000000..2adb5cc --- /dev/null +++ b/src/models/actions/ImportRunner.ts @@ -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; +} \ No newline at end of file diff --git a/src/models/actions/ImportSchoolRunner.ts b/src/models/actions/ImportSchoolRunner.ts new file mode 100644 index 0000000..59320b7 --- /dev/null +++ b/src/models/actions/ImportSchoolRunner.ts @@ -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 { + 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 { + 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; + } +} \ No newline at end of file