import { IsInt, IsNotEmpty, IsOptional, IsString } from 'class-validator'; import { getConnectionManager } from 'typeorm'; import { GroupContactNotFoundError, GroupContactWrongTypeError } from '../../../errors/GroupContactErrors'; import { GroupContact } from '../../entities/GroupContact'; /** * This classed is used to create a new RunnerGroup entity from a json body (post request). */ export abstract class CreateRunnerGroup { /** * The new group's name. */ @IsNotEmpty() @IsString() name: string; /** * The new group's contact. * Optional */ @IsInt() @IsOptional() contact?: number; /** * Gets the new group's contact by it's id. */ public async getContact(): Promise { if (this.contact === undefined || this.contact === null) { return null; } if (!isNaN(this.contact)) { let contact = await getConnectionManager().get().getRepository(GroupContact).findOne({ id: this.contact }); if (!contact) { throw new GroupContactNotFoundError; } return contact; } throw new GroupContactWrongTypeError; } }