From 10af1ba34148c992e94fa580e1c0210bfaea5112 Mon Sep 17 00:00:00 2001 From: Nicolai Ort Date: Thu, 21 Jan 2021 16:40:47 +0100 Subject: [PATCH] Implemented a runner selfservice registration creation action ref #112 --- .../actions/create/CreateSelfServiceRunner.ts | 54 +++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 src/models/actions/create/CreateSelfServiceRunner.ts diff --git a/src/models/actions/create/CreateSelfServiceRunner.ts b/src/models/actions/create/CreateSelfServiceRunner.ts new file mode 100644 index 0000000..2c3bc04 --- /dev/null +++ b/src/models/actions/create/CreateSelfServiceRunner.ts @@ -0,0 +1,54 @@ +import { IsInt, IsOptional } from 'class-validator'; +import { getConnection } from 'typeorm'; +import { RunnerTeamNotFoundError } from '../../../errors/RunnerTeamErrors'; +import { Address } from '../../entities/Address'; +import { Runner } from '../../entities/Runner'; +import { RunnerGroup } from '../../entities/RunnerGroup'; +import { RunnerTeam } from '../../entities/RunnerTeam'; +import { CreateParticipant } from './CreateParticipant'; + +/** + * This classed is used to create a new Runner entity from a json body (post request). + */ +export class CreateSelfServiceRunner extends CreateParticipant { + + /** + * The new runner's team's id. + * The team has to be a part of the runner's org. + * The team property may get ignored. + * If no team get's provided the runner's group will be their org. + */ + @IsInt() + @IsOptional() + team?: number; + + /** + * Creates a new Runner entity from this. + */ + public async toEntity(group: RunnerGroup): Promise { + let newRunner: Runner = new Runner(); + + newRunner.firstname = this.firstname; + newRunner.middlename = this.middlename; + newRunner.lastname = this.lastname; + newRunner.phone = this.phone; + newRunner.email = this.email; + newRunner.group = await this.getGroup(group); + newRunner.address = this.address; + Address.validate(newRunner.address); + + return newRunner; + } + + /** + * Gets the new runner's group by it's id. + */ + public async getGroup(group: RunnerGroup): Promise { + if (!this.team) { + return group; + } + const team = await getConnection().getRepository(RunnerTeam).findOne({ id: this.team }, { relations: ["parentGroup"] }); + if (team.parentGroup.id != group.id || !team) { throw new RunnerTeamNotFoundError(); } + return team; + } +} \ No newline at end of file