backend/src/models/creation/CreateParticipant.ts

83 lines
2.0 KiB
TypeScript

import { IsEmail, IsInt, IsNotEmpty, IsObject, IsOptional, IsPhoneNumber, IsString } from 'class-validator';
import { getConnectionManager } from 'typeorm';
import { ParticipantOnlyOneAddressAllowedError } from '../../errors/ParticipantErrors';
import { Address } from '../entities/Address';
import { CreateAddress } from './CreateAddress';
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's id.
* Optional - please provide either addressId or address.
*/
@IsInt()
@IsOptional()
addressId?: number;
/**
* The new participant's address.
* Optional - please provide either addressId or address.
*/
@IsObject()
@IsOptional()
address?: CreateAddress;
/**
* Creates a Participant entity from this.
*/
public async getAddress(): Promise<Address> {
let address: Address;
if (this.addressId !== undefined && this.address !== undefined) {
throw new ParticipantOnlyOneAddressAllowedError
}
if (this.addressId === undefined && this.address === undefined) {
return null;
}
if (this.addressId) {
return await getConnectionManager().get().getRepository(Address).findOne({ id: this.addressId });
}
return this.address.toAddress();
}
}