From 65b2399eaa83ae97a8ade0f4eef5a17f15773fcb Mon Sep 17 00:00:00 2001 From: Nicolai Ort Date: Sat, 5 Dec 2020 17:04:22 +0100 Subject: [PATCH] Reverted to id based relation setter ref #13 --- src/errors/AddressErrors.ts | 2 +- src/errors/GroupContactErrors.ts | 2 +- src/errors/RunnerErrors.ts | 4 +- src/errors/RunnerOrganisationErrors.ts | 2 +- src/models/creation/CreateGroupContact.ts | 12 ++---- src/models/creation/CreateParticipant.ts | 14 ++----- src/models/creation/CreateRunner.ts | 41 ++++++------------- src/models/creation/CreateRunnerGroup.ts | 13 ++---- .../creation/CreateRunnerOrganisation.ts | 14 ++----- src/models/creation/CreateRunnerTeam.ts | 9 ++-- 10 files changed, 34 insertions(+), 79 deletions(-) diff --git a/src/errors/AddressErrors.ts b/src/errors/AddressErrors.ts index c655032..c7ae8af 100644 --- a/src/errors/AddressErrors.ts +++ b/src/errors/AddressErrors.ts @@ -6,7 +6,7 @@ export class AddressWrongTypeError extends NotAcceptableError { name = "AddressWrongTypeError" @IsString() - message = "The address must be either a existing address, new address or a existing adress's id. \n You provided a object of another type." + message = "The address must be an existing adress's id. \n You provided a object of another type." } export class AddressNotFoundError extends NotFoundError { diff --git a/src/errors/GroupContactErrors.ts b/src/errors/GroupContactErrors.ts index 503fa47..c5bf9c7 100644 --- a/src/errors/GroupContactErrors.ts +++ b/src/errors/GroupContactErrors.ts @@ -6,7 +6,7 @@ export class GroupContactWrongTypeError extends NotAcceptableError { name = "GroupContactWrongTypeError" @IsString() - message = "The groupContact must be either a existing groupContact, new groupContact or a existing adress's id. \n You provided a object of another type." + message = "The groupContact must be an existing groupContact's id. \n You provided a object of another type." } export class GroupContactNotFoundError extends NotFoundError { diff --git a/src/errors/RunnerErrors.ts b/src/errors/RunnerErrors.ts index 0be6419..8250d17 100644 --- a/src/errors/RunnerErrors.ts +++ b/src/errors/RunnerErrors.ts @@ -1,5 +1,5 @@ -import { JsonController, Param, Body, Get, Post, Put, Delete, NotFoundError, OnUndefined, NotAcceptableError } from 'routing-controllers'; -import { IsInt, IsNotEmpty, IsPositive, IsString } from 'class-validator'; +import { IsString } from 'class-validator'; +import { NotAcceptableError, NotFoundError } from 'routing-controllers'; /** * Error to throw when a runner couldn't be found. diff --git a/src/errors/RunnerOrganisationErrors.ts b/src/errors/RunnerOrganisationErrors.ts index 9c0e6c3..65736b1 100644 --- a/src/errors/RunnerOrganisationErrors.ts +++ b/src/errors/RunnerOrganisationErrors.ts @@ -55,5 +55,5 @@ export class RunnerOrganisationWrongTypeError extends NotAcceptableError { name = "RunnerOrganisationWrongTypeError" @IsString() - message = "The runner organisation must be either a existing organisation, new organisation or a existing organisation's id. \n You provided a object of another type." + message = "The runner organisation must be an existing organisation's id. \n You provided a object of another type." } diff --git a/src/models/creation/CreateGroupContact.ts b/src/models/creation/CreateGroupContact.ts index 21b42d5..73f43d0 100644 --- a/src/models/creation/CreateGroupContact.ts +++ b/src/models/creation/CreateGroupContact.ts @@ -1,9 +1,8 @@ -import { IsEmail, IsNotEmpty, IsOptional, IsPhoneNumber, IsString } from 'class-validator'; +import { IsEmail, IsInt, IsNotEmpty, IsOptional, IsPhoneNumber, IsString } from 'class-validator'; import { getConnectionManager } from 'typeorm'; import { AddressNotFoundError, AddressWrongTypeError } from '../../errors/AddressErrors'; import { Address } from '../entities/Address'; import { GroupContact } from '../entities/GroupContact'; -import { CreateAddress } from './CreateAddress'; export class CreateGroupContact { /** @@ -32,8 +31,9 @@ export class CreateGroupContact { * The contact's address. * Optional */ + @IsInt() @IsOptional() - address?: Address | CreateAddress | number; + address?: number; /** * The contact's phone number. @@ -58,12 +58,6 @@ export class CreateGroupContact { if (this.address === undefined) { return null; } - 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 AddressNotFoundError; } diff --git a/src/models/creation/CreateParticipant.ts b/src/models/creation/CreateParticipant.ts index 87472ed..00c8783 100644 --- a/src/models/creation/CreateParticipant.ts +++ b/src/models/creation/CreateParticipant.ts @@ -1,8 +1,7 @@ -import { IsEmail, IsNotEmpty, IsObject, IsOptional, IsPhoneNumber, IsString } from 'class-validator'; +import { IsEmail, IsInt, IsNotEmpty, IsOptional, IsPhoneNumber, IsString } from 'class-validator'; import { getConnectionManager } from 'typeorm'; import { AddressNotFoundError, AddressWrongTypeError } from '../../errors/AddressErrors'; import { Address } from '../entities/Address'; -import { CreateAddress } from './CreateAddress'; export abstract class CreateParticipant { /** @@ -50,8 +49,9 @@ export abstract class CreateParticipant { * Must be of type number (address id), createAddress (new address) or address (existing address) * Optional. */ - @IsObject() - address?: number | CreateAddress | Address; + @IsInt() + @IsOptional() + address?: number; /** * Get's this participant's address from this.address. @@ -60,12 +60,6 @@ export abstract class CreateParticipant { if (this.address === undefined) { return null; } - 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 AddressNotFoundError; } diff --git a/src/models/creation/CreateRunner.ts b/src/models/creation/CreateRunner.ts index b34ec9d..4e5f3e9 100644 --- a/src/models/creation/CreateRunner.ts +++ b/src/models/creation/CreateRunner.ts @@ -1,10 +1,10 @@ -import { IsInt, IsOptional } from 'class-validator'; +import { IsInt } from 'class-validator'; import { getConnectionManager } from 'typeorm'; -import { RunnerGroupNeededError, RunnerGroupNotFoundError, RunnerOnlyOneGroupAllowedError } from '../../errors/RunnerErrors'; +import { RunnerGroupNotFoundError } from '../../errors/RunnerErrors'; +import { RunnerOrganisationWrongTypeError } from '../../errors/RunnerOrganisationErrors'; +import { RunnerTeamNeedsParentError } from '../../errors/RunnerTeamErrors'; 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 extends CreateParticipant { @@ -14,16 +14,7 @@ export class CreateRunner extends CreateParticipant { * Either provide this or his organisation's id. */ @IsInt() - @IsOptional() - teamId?: number; - - /** - * The new runner's organisation's id. - * Either provide this or his teams's id. - */ - @IsInt() - @IsOptional() - orgId?: number; + group: number; /** * Creates a Runner entity from this. @@ -46,23 +37,15 @@ export class CreateRunner extends CreateParticipant { * 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(); + if (this.group === undefined) { + throw new RunnerTeamNeedsParentError(); } - if (this.teamId === undefined && this.orgId === undefined) { - throw new RunnerGroupNeededError(); + if (!isNaN(this.group)) { + let group = await getConnectionManager().get().getRepository(RunnerGroup).findOne({ id: this.group }); + if (!group) { throw new RunnerGroupNotFoundError; } + return group; } - if (this.teamId) { - group = await getConnectionManager().get().getRepository(RunnerTeam).findOne({ id: this.teamId }); - } - if (this.orgId) { - group = await getConnectionManager().get().getRepository(RunnerOrganisation).findOne({ id: this.orgId }); - } - if (!group) { - throw new RunnerGroupNotFoundError(); - } - return group; + throw new RunnerOrganisationWrongTypeError; } } \ No newline at end of file diff --git a/src/models/creation/CreateRunnerGroup.ts b/src/models/creation/CreateRunnerGroup.ts index 20a5dab..76552ac 100644 --- a/src/models/creation/CreateRunnerGroup.ts +++ b/src/models/creation/CreateRunnerGroup.ts @@ -1,8 +1,7 @@ -import { IsNotEmpty, IsObject, IsOptional, IsString } from 'class-validator'; +import { IsInt, IsNotEmpty, IsOptional, IsString } from 'class-validator'; import { getConnectionManager } from 'typeorm'; import { GroupContactNotFoundError, GroupContactWrongTypeError } from '../../errors/GroupContactErrors'; import { GroupContact } from '../entities/GroupContact'; -import { CreateGroupContact } from './CreateGroupContact'; export abstract class CreateRunnerGroup { /** @@ -16,9 +15,9 @@ export abstract class CreateRunnerGroup { * The group's contact. * Optional */ - @IsObject() + @IsInt() @IsOptional() - contact?: number | CreateGroupContact | GroupContact; + contact?: number; /** * Deals with the contact for groups this. @@ -27,12 +26,6 @@ export abstract class CreateRunnerGroup { if (this.contact === undefined) { return null; } - if (this.contact! instanceof GroupContact) { - return this.contact; - } - if (this.contact! instanceof CreateGroupContact) { - return this.contact.toGroupContact(); - } if (!isNaN(this.contact)) { let address = await getConnectionManager().get().getRepository(GroupContact).findOne({ id: this.contact }); if (!address) { throw new GroupContactNotFoundError; } diff --git a/src/models/creation/CreateRunnerOrganisation.ts b/src/models/creation/CreateRunnerOrganisation.ts index 6e40e60..01c3732 100644 --- a/src/models/creation/CreateRunnerOrganisation.ts +++ b/src/models/creation/CreateRunnerOrganisation.ts @@ -1,9 +1,8 @@ -import { IsObject } from 'class-validator'; +import { IsInt, IsOptional } from 'class-validator'; import { getConnectionManager } from 'typeorm'; import { AddressNotFoundError, AddressWrongTypeError } from '../../errors/AddressErrors'; import { Address } from '../entities/Address'; import { RunnerOrganisation } from '../entities/RunnerOrganisation'; -import { CreateAddress } from './CreateAddress'; import { CreateRunnerGroup } from './CreateRunnerGroup'; export class CreateRunnerOrganisation extends CreateRunnerGroup { @@ -12,8 +11,9 @@ export class CreateRunnerOrganisation extends CreateRunnerGroup { * Must be of type number (address id), createAddress (new address) or address (existing address) * Optional. */ - @IsObject() - address?: number | CreateAddress | Address; + @IsInt() + @IsOptional() + address?: number; /** * Creates a Participant entity from this. @@ -22,12 +22,6 @@ export class CreateRunnerOrganisation extends CreateRunnerGroup { if (this.address === undefined) { return null; } - 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 AddressNotFoundError; } diff --git a/src/models/creation/CreateRunnerTeam.ts b/src/models/creation/CreateRunnerTeam.ts index 341a765..98429cb 100644 --- a/src/models/creation/CreateRunnerTeam.ts +++ b/src/models/creation/CreateRunnerTeam.ts @@ -1,4 +1,4 @@ -import { IsNotEmpty } from 'class-validator'; +import { IsInt, IsNotEmpty } from 'class-validator'; import { getConnectionManager } from 'typeorm'; import { RunnerOrganisationNotFoundError, RunnerOrganisationWrongTypeError } from '../../errors/RunnerOrganisationErrors'; import { RunnerTeamNeedsParentError } from '../../errors/RunnerTeamErrors'; @@ -11,22 +11,19 @@ export class CreateRunnerTeam extends CreateRunnerGroup { /** * The team's parent group (organisation). */ + @IsInt() @IsNotEmpty() - parentGroup: number | RunnerOrganisation + parentGroup: number; public async getParent(): Promise { if (this.parentGroup === undefined) { throw new RunnerTeamNeedsParentError(); } - if (this.parentGroup! instanceof RunnerOrganisation) { - return this.parentGroup; - } if (!isNaN(this.parentGroup)) { let parentGroup = await getConnectionManager().get().getRepository(RunnerOrganisation).findOne({ id: this.parentGroup }); if (!parentGroup) { throw new RunnerOrganisationNotFoundError();; } return parentGroup; } - console.log(this.parentGroup); throw new RunnerOrganisationWrongTypeError; }