This commit is contained in:
2020-12-05 18:00:48 +01:00
23 changed files with 317 additions and 224 deletions

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

@@ -0,0 +1,37 @@
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 {
/**
* The group's name.
*/
@IsNotEmpty()
@IsString()
name: string;
/**
* The group's contact.
* Optional
*/
@IsInt()
@IsOptional()
contact?: number;
/**
* Deals with the contact for groups this.
*/
public async getContact(): Promise<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;
}
throw new GroupContactWrongTypeError;
}
}

View File

@@ -1,13 +1,35 @@
import { IsNotEmpty, IsString } 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 { CreateRunnerGroup } from './CreateRunnerGroup';
export class CreateRunnerOrganisation {
export class CreateRunnerOrganisation extends CreateRunnerGroup {
/**
* The Organisation's name.
* The new organisation's address.
* Must be of type number (address id), createAddress (new address) or address (existing address)
* Optional.
*/
@IsString()
@IsNotEmpty()
name: string;
@IsInt()
@IsOptional()
address?: number;
/**
* Creates a Participant entity from this.
*/
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 RunnerOrganisation entity from this.
@@ -16,6 +38,8 @@ export class CreateRunnerOrganisation {
let newRunnerOrganisation: RunnerOrganisation = new RunnerOrganisation();
newRunnerOrganisation.name = this.name;
newRunnerOrganisation.contact = await this.getContact();
newRunnerOrganisation.address = await this.getAddress();
return newRunnerOrganisation;
}

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;
}

View File

@@ -28,18 +28,10 @@ export class DistanceDonation extends Donation {
* The donation's amount in cents (or whatever your currency's smallest unit is.).
* The exact implementation may differ for each type of donation.
*/
@IsInt()
public get amount() {
return this.getAmount();
}
/**
* The function that calculates the amount based on the runner object's distance.
*/
public async getAmount(): Promise<number> {
public get amount(): number {
let calculatedAmount = -1;
try {
calculatedAmount = this.amountPerDistance * await this.runner.distance();
calculatedAmount = this.amountPerDistance * this.runner.distance;
} catch (error) {
throw error;
}

View File

@@ -1,5 +1,5 @@
import { IsInt, IsNotEmpty } from "class-validator";
import { ChildEntity, getConnectionManager, ManyToOne, OneToMany } from "typeorm";
import { ChildEntity, ManyToOne, OneToMany } from "typeorm";
import { DistanceDonation } from "./DistanceDonation";
import { Participant } from "./Participant";
import { RunnerCard } from "./RunnerCard";
@@ -36,25 +36,18 @@ export class Runner extends Participant {
@OneToMany(() => Scan, scan => scan.runner, { nullable: true })
scans: Scan[];
/**
* Returns all scans associated with this runner.
*/
public async getScans(): Promise<Scan[]> {
return await getConnectionManager().get().getRepository(Scan).find({ runner: this });
}
/**
* Returns all valid scans associated with this runner.
*/
public async getValidScans(): Promise<Scan[]> {
return (await this.getScans()).filter(scan => { scan.valid === true });
public get validScans(): Scan[] {
return this.scans.filter(scan => { scan.valid === true });
}
/**
* Returns the total distance ran by this runner.
*/
@IsInt()
public async distance(): Promise<number> {
return await (await this.getValidScans()).reduce((sum, current) => sum + current.distance, 0);
public get distance(): number {
return this.validScans.reduce((sum, current) => sum + current.distance, 0);
}
}

View File

@@ -42,6 +42,4 @@ export abstract class RunnerGroup {
*/
@OneToMany(() => Runner, runner => runner.group, { nullable: true })
runners: Runner[];
public abstract getRunners();
}

View File

@@ -1,7 +1,6 @@
import { IsOptional } from "class-validator";
import { ChildEntity, getConnectionManager, ManyToOne, OneToMany } from "typeorm";
import { ChildEntity, ManyToOne, OneToMany } from "typeorm";
import { Address } from "./Address";
import { Runner } from './Runner';
import { RunnerGroup } from "./RunnerGroup";
import { RunnerTeam } from "./RunnerTeam";
@@ -24,27 +23,4 @@ export class RunnerOrganisation extends RunnerGroup {
*/
@OneToMany(() => RunnerTeam, team => team.parentGroup, { nullable: true })
teams: RunnerTeam[];
/**
* Returns all runners associated with this organisation or it's teams.
*/
public async getRunners() {
let runners: Runner[] = new Array<Runner>();
const teams = await this.getTeams();
await teams.forEach(async team => {
runners.push(... await team.getRunners());
});
await runners.push(... await getConnectionManager().get().getRepository(Runner).find({ group: this }));
return runners;
}
/**
* Returns all teams associated with this organisation.
*/
public async getTeams() {
return await getConnectionManager().get().getRepository(RunnerTeam).find({ parentGroup: this });
}
}

View File

@@ -1,6 +1,5 @@
import { IsNotEmpty } from "class-validator";
import { ChildEntity, getConnectionManager, ManyToOne } from "typeorm";
import { Runner } from './Runner';
import { ChildEntity, ManyToOne } from "typeorm";
import { RunnerGroup } from "./RunnerGroup";
import { RunnerOrganisation } from "./RunnerOrganisation";
@@ -15,13 +14,6 @@ export class RunnerTeam extends RunnerGroup {
* Optional
*/
@IsNotEmpty()
@ManyToOne(() => RunnerOrganisation, org => org.teams, { nullable: false })
@ManyToOne(() => RunnerOrganisation, org => org.teams, { nullable: true })
parentGroup?: RunnerOrganisation;
/**
* Returns all runners associated with this team.
*/
public async getRunners() {
return await getConnectionManager().get().getRepository(Runner).find({ group: this });
}
}