backend/src/models/creation/CreateRunnerOrganisation.ts

58 lines
1.8 KiB
TypeScript

import { IsInt, IsObject, IsOptional } from 'class-validator';
import { getConnectionManager } from 'typeorm';
import { ParticipantOnlyOneAddressAllowedError } from '../../errors/ParticipantErrors';
import { Address } from '../entities/Address';
import { RunnerOrganisation } from '../entities/RunnerOrganisation';
import { CreateAddress } from './CreateAddress';
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.
*/
@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();
}
/**
* Creates a RunnerOrganisation entity from this.
*/
public async toRunnerOrganisation(): Promise<RunnerOrganisation> {
let newRunnerOrganisation: RunnerOrganisation = new RunnerOrganisation();
newRunnerOrganisation.name = this.name;
newRunnerOrganisation.contact = await this.getContact();
newRunnerOrganisation.address = await this.getAddress();
return newRunnerOrganisation;
}
}