import { IsInt, IsObject } from 'class-validator'; import { getConnectionManager } from 'typeorm'; import { RunnerGroupNotFoundError } from '../../errors/RunnerGroupErrors'; import { RunnerOrganisationWrongTypeError } from '../../errors/RunnerOrganisationErrors'; import { RunnerTeamNeedsParentError } from '../../errors/RunnerTeamErrors'; import { Runner } from '../entities/Runner'; import { RunnerGroup } from '../entities/RunnerGroup'; import { CreateParticipant } from './CreateParticipant'; export class UpdateRunner extends CreateParticipant { /** * The updated runner's id. */ @IsInt() id: number; /** * The updated runner's new team/org. */ @IsObject() group: RunnerGroup; /** * Creates a Runner entity from this. */ public async updateRunner(runner: Runner): Promise { runner.firstname = this.firstname; runner.middlename = this.middlename; runner.lastname = this.lastname; runner.phone = this.phone; runner.email = this.email; runner.group = await this.getGroup(); runner.address = await this.getAddress(); return runner; } /** * Manages all the different ways a group can be provided. */ public async getGroup(): Promise { if (this.group === undefined || this.group === null) { throw new RunnerTeamNeedsParentError(); } if (!isNaN(this.group.id)) { let group = await getConnectionManager().get().getRepository(RunnerGroup).findOne({ id: this.group.id }); if (!group) { throw new RunnerGroupNotFoundError; } return group; } throw new RunnerOrganisationWrongTypeError; } }