import { IsEmail, IsNotEmpty, IsOptional, IsPhoneNumber, IsString } from 'class-validator'; import { getConnectionManager } from 'typeorm'; import { AddressNotFoundError, AddressWrongTypeError } from '../../errors/AddressErrors'; import { Address } from '../entities/Address'; import { GroupContact } from '../entities/GroupContact'; import { CreateAddress } from './CreateAddress'; 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 */ @IsOptional() address?: Address | CreateAddress | number; /** * The contact's phone number. * Optional */ @IsOptional() @IsPhoneNumber("DE") 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 (this.address! instanceof Address) { return this.address; } if (this.address! instanceof CreateAddress) { return this.address.toAddress(); } 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 { let contact: GroupContact = new GroupContact(); contact.firstname = this.firstname; contact.middlename = this.middlename; contact.lastname = this.lastname; contact.email = this.email; contact.phone = this.phone; contact.address = await this.getAddress(); return null; } }