diff --git a/src/errors/AddressErrors.ts b/src/errors/AddressErrors.ts new file mode 100644 index 0000000..c655032 --- /dev/null +++ b/src/errors/AddressErrors.ts @@ -0,0 +1,18 @@ +import { IsString } from 'class-validator'; +import { NotAcceptableError, NotFoundError } from 'routing-controllers'; + +export class AddressWrongTypeError extends NotAcceptableError { + @IsString() + 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." +} + +export class AddressNotFoundError extends NotFoundError { + @IsString() + name = "AddressNotFoundError" + + @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/errors/GroupContactErrors.ts b/src/errors/GroupContactErrors.ts new file mode 100644 index 0000000..503fa47 --- /dev/null +++ b/src/errors/GroupContactErrors.ts @@ -0,0 +1,18 @@ +import { IsString } from 'class-validator'; +import { NotAcceptableError, NotFoundError } from 'routing-controllers'; + +export class GroupContactWrongTypeError extends NotAcceptableError { + @IsString() + 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." +} + +export class GroupContactNotFoundError extends NotFoundError { + @IsString() + name = "GroupContactNotFoundError" + + @IsString() + message = "The groupContact you provided couldn't be located in the system. \n Please check your request." +} \ No newline at end of file diff --git a/src/errors/ParticipantErrors.ts b/src/errors/ParticipantErrors.ts deleted file mode 100644 index 97c5412..0000000 --- a/src/errors/ParticipantErrors.ts +++ /dev/null @@ -1,18 +0,0 @@ -import { IsString } from 'class-validator'; -import { NotAcceptableError, NotFoundError } from 'routing-controllers'; - -export class ParticipantAddressWrongTypeError extends NotAcceptableError { - @IsString() - name = "ParticipantAddressWrongTypeError" - - @IsString() - 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 { - @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/errors/RunnerOrganisationErrors.ts b/src/errors/RunnerOrganisationErrors.ts index 1dfb68e..9c0e6c3 100644 --- a/src/errors/RunnerOrganisationErrors.ts +++ b/src/errors/RunnerOrganisationErrors.ts @@ -48,4 +48,12 @@ export class RunnerOrganisationHasTeamsError extends NotAcceptableError { @IsString() message = "This organisation still has teams associated with it. \n If you want to delete this organisation with all it's runners and teams ass `?force` to your query." -} \ No newline at end of file +} + +export class RunnerOrganisationWrongTypeError extends NotAcceptableError { + @IsString() + 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." +} diff --git a/src/errors/RunnerTeamErrors.ts b/src/errors/RunnerTeamErrors.ts index a482c26..33fbea5 100644 --- a/src/errors/RunnerTeamErrors.ts +++ b/src/errors/RunnerTeamErrors.ts @@ -36,4 +36,16 @@ export class RunnerTeamHasRunnersError extends NotAcceptableError { @IsString() message = "This team still has runners associated with it. \n If you want to delete this team with all it's runners and teams ass `?force` to your query." +} + +/** + * Error to throw when a team still has runners associated. + * Implemented this waysto work with the json-schema conversion for openapi. + */ +export class RunnerTeamNeedsParentError extends NotAcceptableError { + @IsString() + name = "RunnerTeamNeedsParentError" + + @IsString() + message = "You provided no runner organisation as this team's parent group." } \ No newline at end of file diff --git a/src/models/creation/CreateGroupContact.ts b/src/models/creation/CreateGroupContact.ts new file mode 100644 index 0000000..21b42d5 --- /dev/null +++ b/src/models/creation/CreateGroupContact.ts @@ -0,0 +1,89 @@ +import { IsEmail, 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 { + /** + * The contact's first name. + */ + @IsNotEmpty() + @IsString() + firstname: string; + + /** + * The contact's middle name. + * Optional + */ + @IsOptional() + @IsString() + middlename?: string; + + /** + * The contact's last name. + */ + @IsNotEmpty() + @IsString() + lastname: string; + + /** + * The contact's address. + * Optional + */ + @IsOptional() + address?: Address | CreateAddress | number; + + /** + * The contact's phone number. + * Optional + */ + @IsOptional() + @IsPhoneNumber("DE") + phone?: string; + + /** + * The contact's email address. + * Optional + */ + @IsOptional() + @IsEmail() + email?: string; + + /** + * Get's this participant's address from this.address. + */ + public async getAddress(): Promise
{ + 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; } + return address; + } + + throw new AddressWrongTypeError; + } + + /** + * Creates a Address object based on this. + */ + public async toGroupContact(): Promise { + let contact: GroupContact = new GroupContact(); + contact.firstname = this.firstname; + contact.middlename = this.middlename; + contact.lastname = this.lastname; + contact.email = this.email; + contact.phone = this.phone; + contact.address = await this.getAddress(); + return null; + } +} \ No newline at end of file diff --git a/src/models/creation/CreateParticipant.ts b/src/models/creation/CreateParticipant.ts index c70cf56..87472ed 100644 --- a/src/models/creation/CreateParticipant.ts +++ b/src/models/creation/CreateParticipant.ts @@ -1,6 +1,6 @@ import { IsEmail, IsNotEmpty, IsObject, IsOptional, IsPhoneNumber, IsString } from 'class-validator'; import { getConnectionManager } from 'typeorm'; -import { ParticipantAddressNotFoundError, ParticipantAddressWrongTypeError } from '../../errors/ParticipantErrors'; +import { AddressNotFoundError, AddressWrongTypeError } from '../../errors/AddressErrors'; import { Address } from '../entities/Address'; import { CreateAddress } from './CreateAddress'; @@ -54,7 +54,7 @@ export abstract class CreateParticipant { address?: number | CreateAddress | Address; /** - * Creates a Participant entity from this. + * Get's this participant's address from this.address. */ public async getAddress(): Promise
{ if (this.address === undefined) { @@ -68,10 +68,10 @@ export abstract class CreateParticipant { } if (!isNaN(this.address)) { let address = await getConnectionManager().get().getRepository(Address).findOne({ id: this.address }); - if (!address) { throw new ParticipantAddressNotFoundError; } + if (!address) { throw new AddressNotFoundError; } return address; } - throw new ParticipantAddressWrongTypeError; + throw new AddressWrongTypeError; } } \ No newline at end of file diff --git a/src/models/creation/CreateRunnerGroup.ts b/src/models/creation/CreateRunnerGroup.ts index 33cb5a9..20a5dab 100644 --- a/src/models/creation/CreateRunnerGroup.ts +++ b/src/models/creation/CreateRunnerGroup.ts @@ -1,5 +1,8 @@ import { IsNotEmpty, IsObject, 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 { /** @@ -15,16 +18,27 @@ export abstract class CreateRunnerGroup { */ @IsObject() @IsOptional() - contact?: GroupContact; + contact?: number | CreateGroupContact | GroupContact; /** * Deals with the contact for groups this. */ public async getContact(): Promise { - let newGroupContact: GroupContact; + 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; } + return address; + } - //TODO: - - return newGroupContact; + throw new GroupContactWrongTypeError; } } \ No newline at end of file diff --git a/src/models/creation/CreateRunnerOrganisation.ts b/src/models/creation/CreateRunnerOrganisation.ts index b90162d..6e40e60 100644 --- a/src/models/creation/CreateRunnerOrganisation.ts +++ b/src/models/creation/CreateRunnerOrganisation.ts @@ -1,6 +1,6 @@ -import { IsInt, IsObject, IsOptional } from 'class-validator'; +import { IsObject } from 'class-validator'; import { getConnectionManager } from 'typeorm'; -import { ParticipantOnlyOneAddressAllowedError } from '../../errors/ParticipantErrors'; +import { AddressNotFoundError, AddressWrongTypeError } from '../../errors/AddressErrors'; import { Address } from '../entities/Address'; import { RunnerOrganisation } from '../entities/RunnerOrganisation'; import { CreateAddress } from './CreateAddress'; @@ -8,39 +8,33 @@ import { CreateRunnerGroup } from './CreateRunnerGroup'; export class CreateRunnerOrganisation extends CreateRunnerGroup { /** - * 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. + * The new organisation's 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 AddressNotFoundError; } + return address; } - return this.address.toAddress(); + throw new AddressWrongTypeError; } /** diff --git a/src/models/creation/CreateRunnerTeam.ts b/src/models/creation/CreateRunnerTeam.ts index 101a516..341a765 100644 --- a/src/models/creation/CreateRunnerTeam.ts +++ b/src/models/creation/CreateRunnerTeam.ts @@ -1,23 +1,35 @@ -import { IsInt, IsNotEmpty, IsString } from 'class-validator'; +import { IsNotEmpty } from 'class-validator'; import { getConnectionManager } from 'typeorm'; -import { RunnerOrganisationNotFoundError } from '../../errors/RunnerOrganisationErrors'; +import { RunnerOrganisationNotFoundError, RunnerOrganisationWrongTypeError } from '../../errors/RunnerOrganisationErrors'; +import { RunnerTeamNeedsParentError } from '../../errors/RunnerTeamErrors'; import { RunnerOrganisation } from '../entities/RunnerOrganisation'; import { RunnerTeam } from '../entities/RunnerTeam'; +import { CreateRunnerGroup } from './CreateRunnerGroup'; -export class CreateRunnerTeam { - /** - * The teams's name. - */ - @IsString() - @IsNotEmpty() - name: string; +export class CreateRunnerTeam extends CreateRunnerGroup { /** * The team's parent group (organisation). */ - @IsInt() @IsNotEmpty() - parentId: number + parentGroup: number | RunnerOrganisation + + 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; + } /** * Creates a RunnerTeam entity from this. @@ -26,10 +38,8 @@ export class CreateRunnerTeam { let newRunnerTeam: RunnerTeam = new RunnerTeam(); newRunnerTeam.name = this.name; - newRunnerTeam.parentGroup = await getConnectionManager().get().getRepository(RunnerOrganisation).findOne({ id: this.parentId }); - if (!newRunnerTeam.parentGroup) { - throw new RunnerOrganisationNotFoundError(); - } + newRunnerTeam.parentGroup = await this.getParent(); + newRunnerTeam.contact = await this.getContact() return newRunnerTeam; }