import { IsEmail, IsInt, IsNotEmpty, IsOptional, IsPhoneNumber, IsString } from 'class-validator'; import { getConnectionManager } from 'typeorm'; import { config } from '../../config'; import { AddressNotFoundError, AddressWrongTypeError } from '../../errors/AddressErrors'; import { Address } from '../entities/Address'; /** * This classed is used to create a new Participant entity from a json body (post request). */ export abstract class CreateParticipant { /** * The new participant's first name. */ @IsString() @IsNotEmpty() firstname: string; /** * The new participant's middle name. */ @IsString() @IsOptional() middlename?: string; /** * The new participant's last name. */ @IsString() @IsNotEmpty() lastname: string; /** * The new participant's phone number. * This will be validated against the configured country phone numer syntax (default: international). */ @IsString() @IsOptional() @IsPhoneNumber(config.phone_validation_countrycode) phone?: string; /** * The new participant's e-mail address. */ @IsString() @IsOptional() @IsEmail() email?: string; /** * The new participant's address. * Must be of type number (address id). */ @IsInt() @IsOptional() address?: number; /** * Gets the new participant's address by it's address. */ public async getAddress(): Promise
{ if (this.address === undefined || this.address === null) { 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; } }