import { IsEmail, IsInt, IsNotEmpty, IsOptional, IsPhoneNumber, IsPositive, IsString } from 'class-validator'; import { Runner } from '../models/Runner'; import { getConnectionManager, Repository } from 'typeorm'; import { group } from 'console'; import { RunnerOnlyOneGroupAllowedError, RunnerGroupNeededError, RunnerGroupNotFoundError } from '../errors/RunnerErrors'; import { RunnerOrganisation } from './RunnerOrganisation'; import { RunnerTeam } from './RunnerTeam'; import { RunnerGroup } from './RunnerGroup'; import { Address } from 'cluster'; export class CreateRunner { @IsString() firstname: string; @IsString() middlename?: string; @IsString() lastname: string; @IsString() phone?: string; @IsString() email?: string; @IsInt() teamId?: number @IsInt() orgId?: number public async toRunner(): Promise { let newRunner: Runner = new Runner(); if (this.teamId !== undefined && this.orgId !== undefined) { throw new RunnerOnlyOneGroupAllowedError(); } if (this.teamId === undefined && this.orgId === undefined) { throw new RunnerGroupNeededError(); } if (this.teamId) { newRunner.group = await getConnectionManager().get().getRepository(RunnerTeam).findOne({ id: this.teamId }); } if (this.orgId) { newRunner.group = await getConnectionManager().get().getRepository(RunnerOrganisation).findOne({ id: this.orgId }); } if (!newRunner.group) { throw new RunnerGroupNotFoundError(); } newRunner.firstname = this.firstname; newRunner.middlename = this.middlename; newRunner.lastname = this.lastname; newRunner.phone = this.phone; newRunner.email = this.email; console.log(newRunner) return newRunner; } }