Smoothed out the participant creation process regarting addresses
ref #13
This commit is contained in:
		| @@ -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 { | ||||
|   | ||||
| @@ -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<Address> { | ||||
|         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; | ||||
|     } | ||||
| } | ||||
		Reference in New Issue
	
	Block a user