Reverted to id based relation setter

ref #13
This commit is contained in:
Nicolai Ort 2020-12-05 17:04:22 +01:00
parent 4352910d54
commit 65b2399eaa
10 changed files with 34 additions and 79 deletions

View File

@ -6,7 +6,7 @@ export class AddressWrongTypeError extends NotAcceptableError {
name = "AddressWrongTypeError" name = "AddressWrongTypeError"
@IsString() @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 { export class AddressNotFoundError extends NotFoundError {

View File

@ -6,7 +6,7 @@ export class GroupContactWrongTypeError extends NotAcceptableError {
name = "GroupContactWrongTypeError" name = "GroupContactWrongTypeError"
@IsString() @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 { export class GroupContactNotFoundError extends NotFoundError {

View File

@ -1,5 +1,5 @@
import { JsonController, Param, Body, Get, Post, Put, Delete, NotFoundError, OnUndefined, NotAcceptableError } from 'routing-controllers'; import { IsString } from 'class-validator';
import { IsInt, IsNotEmpty, IsPositive, IsString } from 'class-validator'; import { NotAcceptableError, NotFoundError } from 'routing-controllers';
/** /**
* Error to throw when a runner couldn't be found. * Error to throw when a runner couldn't be found.

View File

@ -55,5 +55,5 @@ export class RunnerOrganisationWrongTypeError extends NotAcceptableError {
name = "RunnerOrganisationWrongTypeError" name = "RunnerOrganisationWrongTypeError"
@IsString() @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."
} }

View File

@ -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 { getConnectionManager } from 'typeorm';
import { AddressNotFoundError, AddressWrongTypeError } from '../../errors/AddressErrors'; import { AddressNotFoundError, AddressWrongTypeError } from '../../errors/AddressErrors';
import { Address } from '../entities/Address'; import { Address } from '../entities/Address';
import { GroupContact } from '../entities/GroupContact'; import { GroupContact } from '../entities/GroupContact';
import { CreateAddress } from './CreateAddress';
export class CreateGroupContact { export class CreateGroupContact {
/** /**
@ -32,8 +31,9 @@ export class CreateGroupContact {
* The contact's address. * The contact's address.
* Optional * Optional
*/ */
@IsInt()
@IsOptional() @IsOptional()
address?: Address | CreateAddress | number; address?: number;
/** /**
* The contact's phone number. * The contact's phone number.
@ -58,12 +58,6 @@ export class CreateGroupContact {
if (this.address === undefined) { if (this.address === undefined) {
return null; return null;
} }
if (this.address! instanceof Address) {
return this.address;
}
if (this.address! instanceof CreateAddress) {
return this.address.toAddress();
}
if (!isNaN(this.address)) { if (!isNaN(this.address)) {
let address = await getConnectionManager().get().getRepository(Address).findOne({ id: this.address }); let address = await getConnectionManager().get().getRepository(Address).findOne({ id: this.address });
if (!address) { throw new AddressNotFoundError; } if (!address) { throw new AddressNotFoundError; }

View File

@ -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 { getConnectionManager } from 'typeorm';
import { AddressNotFoundError, AddressWrongTypeError } from '../../errors/AddressErrors'; import { AddressNotFoundError, AddressWrongTypeError } from '../../errors/AddressErrors';
import { Address } from '../entities/Address'; import { Address } from '../entities/Address';
import { CreateAddress } from './CreateAddress';
export abstract class CreateParticipant { 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) * Must be of type number (address id), createAddress (new address) or address (existing address)
* Optional. * Optional.
*/ */
@IsObject() @IsInt()
address?: number | CreateAddress | Address; @IsOptional()
address?: number;
/** /**
* Get's this participant's address from this.address. * Get's this participant's address from this.address.
@ -60,12 +60,6 @@ export abstract class CreateParticipant {
if (this.address === undefined) { if (this.address === undefined) {
return null; return null;
} }
if (this.address! instanceof Address) {
return this.address;
}
if (this.address! instanceof CreateAddress) {
return this.address.toAddress();
}
if (!isNaN(this.address)) { if (!isNaN(this.address)) {
let address = await getConnectionManager().get().getRepository(Address).findOne({ id: this.address }); let address = await getConnectionManager().get().getRepository(Address).findOne({ id: this.address });
if (!address) { throw new AddressNotFoundError; } if (!address) { throw new AddressNotFoundError; }

View File

@ -1,10 +1,10 @@
import { IsInt, IsOptional } from 'class-validator'; import { IsInt } from 'class-validator';
import { getConnectionManager } from 'typeorm'; 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 { Runner } from '../entities/Runner';
import { RunnerGroup } from '../entities/RunnerGroup'; import { RunnerGroup } from '../entities/RunnerGroup';
import { RunnerOrganisation } from '../entities/RunnerOrganisation';
import { RunnerTeam } from '../entities/RunnerTeam';
import { CreateParticipant } from './CreateParticipant'; import { CreateParticipant } from './CreateParticipant';
export class CreateRunner extends CreateParticipant { export class CreateRunner extends CreateParticipant {
@ -14,16 +14,7 @@ export class CreateRunner extends CreateParticipant {
* Either provide this or his organisation's id. * Either provide this or his organisation's id.
*/ */
@IsInt() @IsInt()
@IsOptional() group: number;
teamId?: number;
/**
* The new runner's organisation's id.
* Either provide this or his teams's id.
*/
@IsInt()
@IsOptional()
orgId?: number;
/** /**
* Creates a Runner entity from this. * 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. * Manages all the different ways a group can be provided.
*/ */
public async getGroup(): Promise<RunnerGroup> { public async getGroup(): Promise<RunnerGroup> {
let group: RunnerGroup; if (this.group === undefined) {
if (this.teamId !== undefined && this.orgId !== undefined) { throw new RunnerTeamNeedsParentError();
throw new RunnerOnlyOneGroupAllowedError();
} }
if (this.teamId === undefined && this.orgId === undefined) { if (!isNaN(this.group)) {
throw new RunnerGroupNeededError(); let group = await getConnectionManager().get().getRepository(RunnerGroup).findOne({ id: this.group });
if (!group) { throw new RunnerGroupNotFoundError; }
return group;
} }
if (this.teamId) { throw new RunnerOrganisationWrongTypeError;
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;
} }
} }

View File

@ -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 { getConnectionManager } from 'typeorm';
import { GroupContactNotFoundError, GroupContactWrongTypeError } from '../../errors/GroupContactErrors'; import { GroupContactNotFoundError, GroupContactWrongTypeError } from '../../errors/GroupContactErrors';
import { GroupContact } from '../entities/GroupContact'; import { GroupContact } from '../entities/GroupContact';
import { CreateGroupContact } from './CreateGroupContact';
export abstract class CreateRunnerGroup { export abstract class CreateRunnerGroup {
/** /**
@ -16,9 +15,9 @@ export abstract class CreateRunnerGroup {
* The group's contact. * The group's contact.
* Optional * Optional
*/ */
@IsObject() @IsInt()
@IsOptional() @IsOptional()
contact?: number | CreateGroupContact | GroupContact; contact?: number;
/** /**
* Deals with the contact for groups this. * Deals with the contact for groups this.
@ -27,12 +26,6 @@ export abstract class CreateRunnerGroup {
if (this.contact === undefined) { if (this.contact === undefined) {
return null; return null;
} }
if (this.contact! instanceof GroupContact) {
return this.contact;
}
if (this.contact! instanceof CreateGroupContact) {
return this.contact.toGroupContact();
}
if (!isNaN(this.contact)) { if (!isNaN(this.contact)) {
let address = await getConnectionManager().get().getRepository(GroupContact).findOne({ id: this.contact }); let address = await getConnectionManager().get().getRepository(GroupContact).findOne({ id: this.contact });
if (!address) { throw new GroupContactNotFoundError; } if (!address) { throw new GroupContactNotFoundError; }

View File

@ -1,9 +1,8 @@
import { IsObject } from 'class-validator'; import { IsInt, IsOptional } from 'class-validator';
import { getConnectionManager } from 'typeorm'; import { getConnectionManager } from 'typeorm';
import { AddressNotFoundError, AddressWrongTypeError } from '../../errors/AddressErrors'; import { AddressNotFoundError, AddressWrongTypeError } from '../../errors/AddressErrors';
import { Address } from '../entities/Address'; import { Address } from '../entities/Address';
import { RunnerOrganisation } from '../entities/RunnerOrganisation'; import { RunnerOrganisation } from '../entities/RunnerOrganisation';
import { CreateAddress } from './CreateAddress';
import { CreateRunnerGroup } from './CreateRunnerGroup'; import { CreateRunnerGroup } from './CreateRunnerGroup';
export class CreateRunnerOrganisation extends 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) * Must be of type number (address id), createAddress (new address) or address (existing address)
* Optional. * Optional.
*/ */
@IsObject() @IsInt()
address?: number | CreateAddress | Address; @IsOptional()
address?: number;
/** /**
* Creates a Participant entity from this. * Creates a Participant entity from this.
@ -22,12 +22,6 @@ export class CreateRunnerOrganisation extends CreateRunnerGroup {
if (this.address === undefined) { if (this.address === undefined) {
return null; return null;
} }
if (this.address! instanceof Address) {
return this.address;
}
if (this.address! instanceof CreateAddress) {
return this.address.toAddress();
}
if (!isNaN(this.address)) { if (!isNaN(this.address)) {
let address = await getConnectionManager().get().getRepository(Address).findOne({ id: this.address }); let address = await getConnectionManager().get().getRepository(Address).findOne({ id: this.address });
if (!address) { throw new AddressNotFoundError; } if (!address) { throw new AddressNotFoundError; }

View File

@ -1,4 +1,4 @@
import { IsNotEmpty } from 'class-validator'; import { IsInt, IsNotEmpty } from 'class-validator';
import { getConnectionManager } from 'typeorm'; import { getConnectionManager } from 'typeorm';
import { RunnerOrganisationNotFoundError, RunnerOrganisationWrongTypeError } from '../../errors/RunnerOrganisationErrors'; import { RunnerOrganisationNotFoundError, RunnerOrganisationWrongTypeError } from '../../errors/RunnerOrganisationErrors';
import { RunnerTeamNeedsParentError } from '../../errors/RunnerTeamErrors'; import { RunnerTeamNeedsParentError } from '../../errors/RunnerTeamErrors';
@ -11,22 +11,19 @@ export class CreateRunnerTeam extends CreateRunnerGroup {
/** /**
* The team's parent group (organisation). * The team's parent group (organisation).
*/ */
@IsInt()
@IsNotEmpty() @IsNotEmpty()
parentGroup: number | RunnerOrganisation parentGroup: number;
public async getParent(): Promise<RunnerOrganisation> { public async getParent(): Promise<RunnerOrganisation> {
if (this.parentGroup === undefined) { if (this.parentGroup === undefined) {
throw new RunnerTeamNeedsParentError(); throw new RunnerTeamNeedsParentError();
} }
if (this.parentGroup! instanceof RunnerOrganisation) {
return this.parentGroup;
}
if (!isNaN(this.parentGroup)) { if (!isNaN(this.parentGroup)) {
let parentGroup = await getConnectionManager().get().getRepository(RunnerOrganisation).findOne({ id: this.parentGroup }); let parentGroup = await getConnectionManager().get().getRepository(RunnerOrganisation).findOne({ id: this.parentGroup });
if (!parentGroup) { throw new RunnerOrganisationNotFoundError();; } if (!parentGroup) { throw new RunnerOrganisationNotFoundError();; }
return parentGroup; return parentGroup;
} }
console.log(this.parentGroup);
throw new RunnerOrganisationWrongTypeError; throw new RunnerOrganisationWrongTypeError;
} }