import { IsInt, IsNotEmpty } from 'class-validator'; import { getConnectionManager } from 'typeorm'; import { RunnerOrganisationNotFoundError, RunnerOrganisationWrongTypeError } from '../../errors/RunnerOrganisationErrors'; import { RunnerTeamNeedsParentError } from '../../errors/RunnerTeamErrors'; import { RunnerOrganisation } from '../entities/RunnerOrganisation'; import { RunnerTeam } from '../entities/RunnerTeam'; import { CreateRunnerGroup } from './CreateRunnerGroup'; export class CreateRunnerTeam extends CreateRunnerGroup { /** * The team's parent group (organisation). */ @IsInt() @IsNotEmpty() parentGroup: number; public async getParent(): Promise { if (this.parentGroup === undefined) { throw new RunnerTeamNeedsParentError(); } if (!isNaN(this.parentGroup)) { let parentGroup = await getConnectionManager().get().getRepository(RunnerOrganisation).findOne({ id: this.parentGroup }); if (!parentGroup) { throw new RunnerOrganisationNotFoundError();; } return parentGroup; } throw new RunnerOrganisationWrongTypeError; } /** * Creates a RunnerTeam entity from this. */ public async toRunnerTeam(): Promise { let newRunnerTeam: RunnerTeam = new RunnerTeam(); newRunnerTeam.name = this.name; try { newRunnerTeam.parentGroup = await this.getParent(); } catch (error) { throw error; } newRunnerTeam.contact = await this.getContact() return newRunnerTeam; } }