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'; import { GroupContact } from '../entities/GroupContact'; export class CreateGroupContact { /** * The contact's first name. */ @IsNotEmpty() @IsString() firstname: string; /** * The contact's middle name. * Optional */ @IsOptional() @IsString() middlename?: string; /** * The contact's last name. */ @IsNotEmpty() @IsString() lastname: string; /** * The contact's address. * Optional */ @IsInt() @IsOptional() address?: number; /** * The contact's phone number. * Optional */ @IsOptional() @IsPhoneNumber(config.phone_validation_countrycode) phone?: string; /** * The contact's email address. * Optional */ @IsOptional() @IsEmail() email?: string; /** * 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; } /** * Creates a Address object based on this. */ public async toGroupContact(): Promise