backend/src/models/actions/CreateParticipant.ts
Nicolai Ort 9df86953cf Merge branch 'dev' into feature/17-automated_tests
# Conflicts:
#	src/app.ts
#	src/controllers/RunnerController.ts
#	src/controllers/RunnerOrganisationController.ts
#	src/controllers/RunnerTeamController.ts
#	src/models/actions/CreateParticipant.ts
2020-12-10 17:45:23 +01:00

72 lines
1.8 KiB
TypeScript

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';
export abstract class CreateParticipant {
/**
* The new participant's first name.
*/
@IsString()
@IsNotEmpty()
firstname: string;
/**
* The new participant's middle name.
* Optional.
*/
@IsString()
@IsOptional()
middlename?: string;
/**
* The new participant's last name.
*/
@IsString()
@IsNotEmpty()
lastname: string;
/**
* The new participant's phone number.
* Optional.
*/
@IsString()
@IsOptional()
@IsPhoneNumber(config.phone_validation_countrycode)
phone?: string;
/**
* The new participant's e-mail address.
* Optional.
*/
@IsString()
@IsOptional()
@IsEmail()
email?: string;
/**
* The new participant's address.
* Must be of type number (address id), createAddress (new address) or address (existing address)
* Optional.
*/
@IsInt()
@IsOptional()
address?: number;
/**
* Get's this participant's address from this.address.
*/
public async getAddress(): Promise<Address> {
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;
}
}