import { IsInt, IsNotEmpty, IsObject } 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 UpdateRunnerTeam extends CreateRunnerGroup { /** * The updated team's id. */ @IsInt() id: number; /** * The team's parent group (organisation). */ @IsObject() @IsNotEmpty() parentGroup: RunnerOrganisation; public async getParent(): Promise { if (this.parentGroup === undefined) { throw new RunnerTeamNeedsParentError(); } if (!isNaN(this.parentGroup.id)) { let parentGroup = await getConnectionManager().get().getRepository(RunnerOrganisation).findOne({ id: this.parentGroup.id }); 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.id = this.id; newRunnerTeam.name = this.name; newRunnerTeam.parentGroup = await this.getParent(); newRunnerTeam.contact = await this.getContact() return newRunnerTeam; } }