Compare commits

..

3 Commits

12 changed files with 214 additions and 120 deletions

View File

@ -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 an 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."
}

View File

@ -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 an existing groupContact'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."
}

View File

@ -1,18 +0,0 @@
import { IsString } from 'class-validator';
import { NotAcceptableError, NotFoundError } from 'routing-controllers';
export class ParticipantOnlyOneAddressAllowedError extends NotAcceptableError {
@IsString()
name = "ParticipantOnlyOneAddressAllowedError"
@IsString()
message = "Participant's can only have one address! \n You provided an id and address object.."
}
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."
}

View File

@ -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.

View File

@ -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."
}
}
export class RunnerOrganisationWrongTypeError extends NotAcceptableError {
@IsString()
name = "RunnerOrganisationWrongTypeError"
@IsString()
message = "The runner organisation must be an existing organisation's id. \n You provided a object of another type."
}

View File

@ -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."
}

View File

@ -0,0 +1,83 @@
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';
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
*/
@IsInt()
@IsOptional()
address?: 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<Address> {
if (this.address === undefined) {
return null;
}
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<GroupContact> {
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;
}
}

View File

@ -1,8 +1,7 @@
import { IsEmail, IsInt, IsNotEmpty, IsObject, IsOptional, IsPhoneNumber, IsString } from 'class-validator';
import { IsEmail, IsInt, IsNotEmpty, IsOptional, IsPhoneNumber, IsString } from 'class-validator';
import { getConnectionManager } from 'typeorm';
import { ParticipantOnlyOneAddressAllowedError } from '../../errors/ParticipantErrors';
import { AddressNotFoundError, AddressWrongTypeError } from '../../errors/AddressErrors';
import { Address } from '../entities/Address';
import { CreateAddress } from './CreateAddress';
export abstract class CreateParticipant {
/**
@ -46,38 +45,27 @@ export abstract class CreateParticipant {
email?: string;
/**
* The new participant's address's id.
* Optional - please provide either addressId or address.
* The new participant's address.
* Must be of type number (address id), createAddress (new address) or address (existing address)
* Optional.
*/
@IsInt()
@IsOptional()
addressId?: number;
address?: number;
/**
* The new participant's address.
* Optional - please provide either addressId or address.
*/
@IsObject()
@IsOptional()
address?: CreateAddress;
/**
* Creates a Participant entity from this.
* Get's this participant's address from this.address.
*/
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 (!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;
}
}

View File

@ -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<RunnerGroup> {
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;
}
}

View File

@ -1,4 +1,6 @@
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';
export abstract class CreateRunnerGroup {
@ -13,18 +15,23 @@ export abstract class CreateRunnerGroup {
* The group's contact.
* Optional
*/
@IsObject()
@IsInt()
@IsOptional()
contact?: GroupContact;
contact?: number;
/**
* Deals with the contact for groups this.
*/
public async getContact(): Promise<GroupContact> {
let newGroupContact: GroupContact;
if (this.contact === undefined) {
return null;
}
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;
}
}

View File

@ -1,46 +1,34 @@
import { IsInt, IsObject, IsOptional } from 'class-validator';
import { IsInt, IsOptional } 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';
import { CreateRunnerGroup } from './CreateRunnerGroup';
export class CreateRunnerOrganisation extends CreateRunnerGroup {
/**
* The new participant's address's id.
* 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.
*/
@IsInt()
@IsOptional()
addressId?: number;
/**
* The new participant's address.
* Optional - please provide either addressId or address.
*/
@IsObject()
@IsOptional()
address?: CreateAddress;
address?: number;
/**
* 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 (!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;
}
/**

View File

@ -1,23 +1,32 @@
import { IsInt, IsNotEmpty, IsString } from 'class-validator';
import { IsInt, 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;
public async getParent(): Promise<RunnerOrganisation> {
if (this.parentGroup === undefined) {
throw new RunnerTeamNeedsParentError();
}
if (!isNaN(this.parentGroup)) {
let parentGroup = await getConnectionManager().get().getRepository(RunnerOrganisation).findOne({ id: this.parentGroup });
if (!parentGroup) { throw new RunnerOrganisationNotFoundError();; }
return parentGroup;
}
throw new RunnerOrganisationWrongTypeError;
}
/**
* Creates a RunnerTeam entity from this.
@ -26,10 +35,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;
}