diff --git a/src/models/creation/CreateRunnerGroup.ts b/src/models/creation/CreateRunnerGroup.ts new file mode 100644 index 0000000..33cb5a9 --- /dev/null +++ b/src/models/creation/CreateRunnerGroup.ts @@ -0,0 +1,30 @@ +import { IsNotEmpty, IsObject, IsOptional, IsString } from 'class-validator'; +import { GroupContact } from '../entities/GroupContact'; + +export abstract class CreateRunnerGroup { + /** + * The group's name. + */ + @IsNotEmpty() + @IsString() + name: string; + + /** + * The group's contact. + * Optional + */ + @IsObject() + @IsOptional() + contact?: GroupContact; + + /** + * Deals with the contact for groups this. + */ + public async getContact(): Promise { + let newGroupContact: GroupContact; + + //TODO: + + return newGroupContact; + } +} \ No newline at end of file diff --git a/src/models/creation/CreateRunnerOrganisation.ts b/src/models/creation/CreateRunnerOrganisation.ts index ff4ec65..b90162d 100644 --- a/src/models/creation/CreateRunnerOrganisation.ts +++ b/src/models/creation/CreateRunnerOrganisation.ts @@ -1,13 +1,47 @@ -import { IsNotEmpty, IsString } from 'class-validator'; +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 { +export class CreateRunnerOrganisation extends CreateRunnerGroup { /** - * The Organisation's name. + * The new participant's address's id. + * Optional - please provide either addressId or address. */ - @IsString() - @IsNotEmpty() - name: string; + @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
{ + 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. @@ -16,6 +50,8 @@ export class CreateRunnerOrganisation { let newRunnerOrganisation: RunnerOrganisation = new RunnerOrganisation(); newRunnerOrganisation.name = this.name; + newRunnerOrganisation.contact = await this.getContact(); + newRunnerOrganisation.address = await this.getAddress(); return newRunnerOrganisation; }