@@ -1,6 +1,6 @@
|
||||
import { IsInt, IsObject, IsOptional } from 'class-validator';
|
||||
import { IsObject } from 'class-validator';
|
||||
import { getConnectionManager } from 'typeorm';
|
||||
import { ParticipantOnlyOneAddressAllowedError } from '../../errors/ParticipantErrors';
|
||||
import { AddressNotFoundError, AddressWrongTypeError } from '../../errors/AddressErrors';
|
||||
import { Address } from '../entities/Address';
|
||||
import { RunnerOrganisation } from '../entities/RunnerOrganisation';
|
||||
import { CreateAddress } from './CreateAddress';
|
||||
@@ -8,39 +8,33 @@ import { CreateRunnerGroup } from './CreateRunnerGroup';
|
||||
|
||||
export class CreateRunnerOrganisation extends CreateRunnerGroup {
|
||||
/**
|
||||
* 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.
|
||||
* The new organisation's address.
|
||||
* Must be of type number (address id), createAddress (new address) or address (existing address)
|
||||
* Optional.
|
||||
*/
|
||||
@IsObject()
|
||||
@IsOptional()
|
||||
address?: CreateAddress;
|
||||
address?: number | CreateAddress | Address;
|
||||
|
||||
/**
|
||||
* 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) {
|
||||
if (this.address === undefined) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (this.addressId) {
|
||||
return await getConnectionManager().get().getRepository(Address).findOne({ id: this.addressId });
|
||||
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;
|
||||
}
|
||||
|
||||
return this.address.toAddress();
|
||||
throw new AddressWrongTypeError;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user