From 975d30e411c580d8c6a7e6c87f7d0187b97070f1 Mon Sep 17 00:00:00 2001 From: Nicolai Ort Date: Sat, 5 Dec 2020 12:39:11 +0100 Subject: [PATCH] Smoothed out the participant creation process regarting addresses ref #13 --- src/errors/ParticipantErrors.ts | 6 ++-- src/models/creation/CreateParticipant.ts | 40 ++++++++++-------------- 2 files changed, 20 insertions(+), 26 deletions(-) diff --git a/src/errors/ParticipantErrors.ts b/src/errors/ParticipantErrors.ts index de672d8..97c5412 100644 --- a/src/errors/ParticipantErrors.ts +++ b/src/errors/ParticipantErrors.ts @@ -1,12 +1,12 @@ import { IsString } from 'class-validator'; import { NotAcceptableError, NotFoundError } from 'routing-controllers'; -export class ParticipantOnlyOneAddressAllowedError extends NotAcceptableError { +export class ParticipantAddressWrongTypeError extends NotAcceptableError { @IsString() - name = "ParticipantOnlyOneAddressAllowedError" + name = "ParticipantAddressWrongTypeError" @IsString() - message = "Participant's can only have one address! \n You provided an id and address object.." + message = "The participant's address must be either a existing address, new address or a existing adress's id. \n You provided a object of another type." } export class ParticipantAddressNotFoundError extends NotFoundError { diff --git a/src/models/creation/CreateParticipant.ts b/src/models/creation/CreateParticipant.ts index 8436186..c70cf56 100644 --- a/src/models/creation/CreateParticipant.ts +++ b/src/models/creation/CreateParticipant.ts @@ -1,6 +1,6 @@ -import { IsEmail, IsInt, IsNotEmpty, IsObject, IsOptional, IsPhoneNumber, IsString } from 'class-validator'; +import { IsEmail, IsNotEmpty, IsObject, IsOptional, IsPhoneNumber, IsString } from 'class-validator'; import { getConnectionManager } from 'typeorm'; -import { ParticipantOnlyOneAddressAllowedError } from '../../errors/ParticipantErrors'; +import { ParticipantAddressNotFoundError, ParticipantAddressWrongTypeError } from '../../errors/ParticipantErrors'; import { Address } from '../entities/Address'; import { CreateAddress } from './CreateAddress'; @@ -45,39 +45,33 @@ export abstract class CreateParticipant { @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. + * 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
{ - 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 ParticipantAddressNotFoundError; } + return address; } - return this.address.toAddress(); + throw new ParticipantAddressWrongTypeError; } } \ No newline at end of file