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'; /** * This classed is used to create a new RunnerTeam entity from a json body (post request). */ export class CreateRunnerTeam extends CreateRunnerGroup { /** * The new team's parent group (organisation). */ @IsInt() @IsNotEmpty() parentGroup: number; /** * Gets the new team's parent org based on it's id. */ public async getParent(): Promise { if (this.parentGroup === undefined || this.parentGroup === null) { 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 new RunnerTeam entity from this. */ public async toEntity(): Promise { let newRunnerTeam: RunnerTeam = new RunnerTeam(); newRunnerTeam.name = this.name; newRunnerTeam.parentGroup = await this.getParent(); newRunnerTeam.contact = await this.getContact() return newRunnerTeam; } }