import { IsEmail, IsInt, IsNotEmpty, IsOptional, IsPhoneNumber, IsString } from 'class-validator'; import { getConnectionManager } from 'typeorm'; import { AddressNotFoundError, AddressWrongTypeError } from '../../errors/AddressErrors'; import { Address } from '../entities/Address'; export abstract class CreateParticipant { /** * The new participant's first name. */ @IsString() @IsNotEmpty() firstname: string; /** * The new participant's middle name. * Optional. */ @IsString() @IsNotEmpty() middlename?: string; /** * The new participant's last name. */ @IsString() @IsNotEmpty() lastname: string; /** * The new participant's phone number. * Optional. */ @IsString() @IsOptional() @IsPhoneNumber("ZZ") phone?: string; /** * The new participant's e-mail address. * Optional. */ @IsString() @IsOptional() @IsEmail() email?: string; /** * The new participant's address. * Must be of type number (address id), createAddress (new address) or address (existing address) * Optional. */ @IsInt() @IsOptional() address?: number; /** * Get's this participant's address from this.address. */ public async getAddress(): Promise
{ if (this.address === undefined) { return null; } if (!isNaN(this.address)) { let address = await getConnectionManager().get().getRepository(Address).findOne({ id: this.address }); if (!address) { throw new AddressNotFoundError; } return address; } throw new AddressWrongTypeError; } }