More dynamic creation of objects

ref #13
This commit is contained in:
Nicolai Ort 2020-12-05 15:50:28 +01:00
parent 975d30e411
commit 4352910d54
10 changed files with 212 additions and 67 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 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."
}

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

View File

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

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 either a existing organisation, new organisation or a 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,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<Address> {
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<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,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<Address> {
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;
}
}

View File

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

View File

@ -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<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 AddressNotFoundError; }
return address;
}
return this.address.toAddress();
throw new AddressWrongTypeError;
}
/**

View File

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