import { IsBoolean, IsInt, IsOptional } from 'class-validator'; import { getConnection } from 'typeorm'; import { RunnerNotFoundError } from '../../errors/RunnerErrors'; import { Runner } from '../entities/Runner'; import { RunnerCard } from '../entities/RunnerCard'; /** * This classed is used to create a new RunnerCard entity from a json body (post request). */ export class CreateRunnerCard { /** * The card's associated runner. */ @IsInt() @IsOptional() runner?: number; /** * Is the new card enabled (for fraud reasons)? * Default: true */ @IsBoolean() enabled: boolean = true; /** * Creates a new RunnerCard entity from this. */ public async toEntity(): Promise { let newCard: RunnerCard = new RunnerCard(); newCard.enabled = this.enabled; newCard.runner = await this.getRunner(); return newCard; } public async getRunner(): Promise { if (!this.runner) { return null; } const runner = await getConnection().getRepository(Runner).findOne({ id: this.runner }); if (!runner) { throw new RunnerNotFoundError(); } return runner; } }