From 56202ec3094444ec60170953aee5829e2a4ffba1 Mon Sep 17 00:00:00 2001 From: Nicolai Ort Date: Fri, 4 Dec 2020 22:40:14 +0100 Subject: [PATCH] Create models now feature the createparticipant abstract ref #13 --- src/errors/ParticipantErrors.ts | 18 +++++ src/models/creation/CreateAddress.ts | 64 ++++++++++++++++++ src/models/creation/CreateParticipant.ts | 83 ++++++++++++++++++++++++ src/models/creation/CreateRunner.ts | 76 +++++++--------------- 4 files changed, 189 insertions(+), 52 deletions(-) create mode 100644 src/errors/ParticipantErrors.ts create mode 100644 src/models/creation/CreateAddress.ts create mode 100644 src/models/creation/CreateParticipant.ts diff --git a/src/errors/ParticipantErrors.ts b/src/errors/ParticipantErrors.ts new file mode 100644 index 0000000..de672d8 --- /dev/null +++ b/src/errors/ParticipantErrors.ts @@ -0,0 +1,18 @@ +import { IsString } from 'class-validator'; +import { NotAcceptableError, NotFoundError } from 'routing-controllers'; + +export class ParticipantOnlyOneAddressAllowedError extends NotAcceptableError { + @IsString() + name = "ParticipantOnlyOneAddressAllowedError" + + @IsString() + message = "Participant's can only have one address! \n You provided an id and address object.." +} + +export class ParticipantAddressNotFoundError extends NotFoundError { + @IsString() + name = "ParticipantAddressNotFoundError" + + @IsString() + message = "The address you provided couldn't be located in the system. \n Please check your request." +} \ No newline at end of file diff --git a/src/models/creation/CreateAddress.ts b/src/models/creation/CreateAddress.ts new file mode 100644 index 0000000..ef78410 --- /dev/null +++ b/src/models/creation/CreateAddress.ts @@ -0,0 +1,64 @@ +import { IsNotEmpty, IsOptional, IsPostalCode, IsString } from 'class-validator'; +import { Address } from '../entities/Address'; + +export class CreateAddress { + /** + * The address's description. + */ + @IsString() + @IsOptional() + description?: string; + + /** + * The address's first line. + * Containing the street and house number. + */ + @IsString() + @IsNotEmpty() + address1: string; + + /** + * The address's second line. + * Containing optional information. + */ + @IsString() + @IsOptional() + address2?: string; + + /** + * The address's postal code. + */ + @IsString() + @IsNotEmpty() + @IsPostalCode("DE") + postalcode: string; + + /** + * The address's city. + */ + @IsString() + @IsNotEmpty() + city: string; + + /** + * The address's country. + */ + @IsString() + @IsNotEmpty() + country: string; + + /** + * Creates a Address object based on this. + */ + public toAddress(): Address { + let newAddress: Address = new Address(); + + newAddress.address1 = this.address1; + newAddress.address2 = this.address2; + newAddress.postalcode = this.postalcode; + newAddress.city = this.city; + newAddress.country = this.country; + + return newAddress; + } +} \ No newline at end of file diff --git a/src/models/creation/CreateParticipant.ts b/src/models/creation/CreateParticipant.ts new file mode 100644 index 0000000..8436186 --- /dev/null +++ b/src/models/creation/CreateParticipant.ts @@ -0,0 +1,83 @@ +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
{ + 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(); + } +} \ No newline at end of file diff --git a/src/models/creation/CreateRunner.ts b/src/models/creation/CreateRunner.ts index 9913ee8..b34ec9d 100644 --- a/src/models/creation/CreateRunner.ts +++ b/src/models/creation/CreateRunner.ts @@ -1,50 +1,13 @@ -import { IsEmail, IsInt, IsNotEmpty, IsOptional, IsPhoneNumber, IsString } from 'class-validator'; +import { IsInt, IsOptional } from 'class-validator'; import { getConnectionManager } from 'typeorm'; import { RunnerGroupNeededError, RunnerGroupNotFoundError, RunnerOnlyOneGroupAllowedError } from '../../errors/RunnerErrors'; import { Runner } from '../entities/Runner'; +import { RunnerGroup } from '../entities/RunnerGroup'; import { RunnerOrganisation } from '../entities/RunnerOrganisation'; import { RunnerTeam } from '../entities/RunnerTeam'; +import { CreateParticipant } from './CreateParticipant'; -export class CreateRunner { - /** - * The new runner's first name. - */ - @IsString() - @IsNotEmpty() - firstname: string; - - /** - * The new runner's middle name. - * Optional. - */ - @IsString() - @IsNotEmpty() - middlename?: string; - - /** - * The new runner's last name. - */ - @IsString() - @IsNotEmpty() - lastname: string; - - /** - * The new runner's phone number. - * Optional. - */ - @IsString() - @IsOptional() - @IsPhoneNumber("ZZ") - phone?: string; - - /** - * The new runner's e-mail address. - * Optional. - */ - @IsString() - @IsOptional() - @IsEmail() - email?: string; +export class CreateRunner extends CreateParticipant { /** * The new runner's team's id. @@ -68,6 +31,22 @@ export class CreateRunner { public async toRunner(): Promise { let newRunner: Runner = new Runner(); + newRunner.firstname = this.firstname; + newRunner.middlename = this.middlename; + newRunner.lastname = this.lastname; + newRunner.phone = this.phone; + newRunner.email = this.email; + newRunner.group = await this.getGroup(); + newRunner.address = await this.getAddress(); + + return newRunner; + } + + /** + * Manages all the different ways a group can be provided. + */ + public async getGroup(): Promise { + let group: RunnerGroup; if (this.teamId !== undefined && this.orgId !== undefined) { throw new RunnerOnlyOneGroupAllowedError(); } @@ -76,21 +55,14 @@ export class CreateRunner { } if (this.teamId) { - newRunner.group = await getConnectionManager().get().getRepository(RunnerTeam).findOne({ id: this.teamId }); + group = await getConnectionManager().get().getRepository(RunnerTeam).findOne({ id: this.teamId }); } if (this.orgId) { - newRunner.group = await getConnectionManager().get().getRepository(RunnerOrganisation).findOne({ id: this.orgId }); + group = await getConnectionManager().get().getRepository(RunnerOrganisation).findOne({ id: this.orgId }); } - if (!newRunner.group) { + if (!group) { throw new RunnerGroupNotFoundError(); } - - newRunner.firstname = this.firstname; - newRunner.middlename = this.middlename; - newRunner.lastname = this.lastname; - newRunner.phone = this.phone; - newRunner.email = this.email; - - return newRunner; + return group; } } \ No newline at end of file