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