import { IsEmail, IsInt, IsNotEmpty, IsOptional, IsPhoneNumber, IsString } from 'class-validator'; import { getConnectionManager } from 'typeorm'; import { config } from '../../../config'; import { AddressNotFoundError } from '../../../errors/AddressErrors'; import { Address } from '../../entities/Address'; import { GroupContact } from '../../entities/GroupContact'; /** * This classed is used to create a new Group entity from a json body (post request). */ export class CreateGroupContact { /** * The new contact's first name. */ @IsNotEmpty() @IsString() firstname: string; /** * The new contact's middle name. */ @IsOptional() @IsString() middlename?: string; /** * The new contact's last name. */ @IsNotEmpty() @IsString() lastname: string; /** * The new contact's address's id. */ @IsInt() @IsOptional() address?: number; /** * The contact's phone number. * This will be validated against the configured country phone numer syntax (default: international). */ @IsOptional() @IsPhoneNumber(config.phone_validation_countrycode) phone?: string; /** * The contact's email address. */ @IsOptional() @IsEmail() email?: string; /** * Gets the new contact's address by it's id. */ public async getAddress(): Promise
{ if (!this.address) { return null; } let address = await getConnectionManager().get().getRepository(Address).findOne({ id: this.address }); if (!address) { throw new AddressNotFoundError; } return address; } /** * Creates a new Address entity from this. */ public async toEntity(): Promise