Merge branch 'dev' into feature/12-jwt-creation

This commit is contained in:
2020-12-04 22:45:54 +01:00
24 changed files with 861 additions and 116 deletions

View File

@@ -0,0 +1,64 @@
import { IsNotEmpty, IsOptional, IsPostalCode, IsString } from 'class-validator';
import { Address } from '../entities/Address';
export class CreateAddress {
/**
* The address's description.
*/
@IsString()
@IsOptional()
description?: string;
/**
* The address's first line.
* Containing the street and house number.
*/
@IsString()
@IsNotEmpty()
address1: string;
/**
* The address's second line.
* Containing optional information.
*/
@IsString()
@IsOptional()
address2?: string;
/**
* The address's postal code.
*/
@IsString()
@IsNotEmpty()
@IsPostalCode("DE")
postalcode: string;
/**
* The address's city.
*/
@IsString()
@IsNotEmpty()
city: string;
/**
* The address's country.
*/
@IsString()
@IsNotEmpty()
country: string;
/**
* Creates a Address object based on this.
*/
public toAddress(): Address {
let newAddress: Address = new Address();
newAddress.address1 = this.address1;
newAddress.address2 = this.address2;
newAddress.postalcode = this.postalcode;
newAddress.city = this.city;
newAddress.country = this.country;
return newAddress;
}
}

View File

@@ -0,0 +1,83 @@
import { IsEmail, IsInt, IsNotEmpty, IsObject, IsOptional, IsPhoneNumber, IsString } from 'class-validator';
import { getConnectionManager } from 'typeorm';
import { ParticipantOnlyOneAddressAllowedError } from '../../errors/ParticipantErrors';
import { Address } from '../entities/Address';
import { CreateAddress } from './CreateAddress';
export abstract class CreateParticipant {
/**
* The new participant's first name.
*/
@IsString()
@IsNotEmpty()
firstname: string;
/**
* The new participant's middle name.
* Optional.
*/
@IsString()
@IsNotEmpty()
middlename?: string;
/**
* The new participant's last name.
*/
@IsString()
@IsNotEmpty()
lastname: string;
/**
* The new participant's phone number.
* Optional.
*/
@IsString()
@IsOptional()
@IsPhoneNumber("ZZ")
phone?: string;
/**
* The new participant's e-mail address.
* Optional.
*/
@IsString()
@IsOptional()
@IsEmail()
email?: string;
/**
* 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.
*/
@IsObject()
@IsOptional()
address?: CreateAddress;
/**
* 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) {
return null;
}
if (this.addressId) {
return await getConnectionManager().get().getRepository(Address).findOne({ id: this.addressId });
}
return this.address.toAddress();
}
}

View File

@@ -1,34 +1,52 @@
import { IsEmail, IsInt, IsNotEmpty, IsOptional, IsPhoneNumber, IsPositive, IsString } from 'class-validator';
import { IsInt, IsOptional } from 'class-validator';
import { getConnectionManager } from 'typeorm';
import { RunnerGroupNeededError, RunnerGroupNotFoundError, RunnerOnlyOneGroupAllowedError } from '../../errors/RunnerErrors';
import { Runner } from '../entities/Runner';
import { getConnectionManager, Repository } from 'typeorm';
import { group } from 'console';
import { RunnerOnlyOneGroupAllowedError, RunnerGroupNeededError, RunnerGroupNotFoundError } from '../../errors/RunnerErrors';
import { RunnerGroup } from '../entities/RunnerGroup';
import { RunnerOrganisation } from '../entities/RunnerOrganisation';
import { RunnerTeam } from '../entities/RunnerTeam';
import { RunnerGroup } from '../entities/RunnerGroup';
import { Address } from 'cluster';
import { CreateParticipant } from './CreateParticipant';
export class CreateRunner {
@IsString()
firstname: string;
@IsString()
middlename?: string;
@IsString()
lastname: string;
@IsString()
phone?: string;
@IsString()
email?: string;
export class CreateRunner extends CreateParticipant {
/**
* The new runner's team's id.
* Either provide this or his organisation's id.
*/
@IsInt()
@IsOptional()
teamId?: number
teamId?: number;
/**
* The new runner's organisation's id.
* Either provide this or his teams's id.
*/
@IsInt()
@IsOptional()
orgId?: number
orgId?: number;
/**
* Creates a Runner entity from this.
*/
public async toRunner(): Promise<Runner> {
let newRunner: Runner = new Runner();
newRunner.firstname = this.firstname;
newRunner.middlename = this.middlename;
newRunner.lastname = this.lastname;
newRunner.phone = this.phone;
newRunner.email = this.email;
newRunner.group = await this.getGroup();
newRunner.address = await this.getAddress();
return newRunner;
}
/**
* 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();
}
@@ -37,22 +55,14 @@ export class CreateRunner {
}
if (this.teamId) {
newRunner.group = await getConnectionManager().get().getRepository(RunnerTeam).findOne({ id: this.teamId });
group = await getConnectionManager().get().getRepository(RunnerTeam).findOne({ id: this.teamId });
}
if (this.orgId) {
newRunner.group = await getConnectionManager().get().getRepository(RunnerOrganisation).findOne({ id: this.orgId });
group = await getConnectionManager().get().getRepository(RunnerOrganisation).findOne({ id: this.orgId });
}
if (!newRunner.group) {
if (!group) {
throw new RunnerGroupNotFoundError();
}
newRunner.firstname = this.firstname;
newRunner.middlename = this.middlename;
newRunner.lastname = this.lastname;
newRunner.phone = this.phone;
newRunner.email = this.email;
console.log(newRunner)
return newRunner;
return group;
}
}

View File

@@ -1,10 +1,17 @@
import { IsString } from 'class-validator';
import { IsNotEmpty, IsString } from 'class-validator';
import { RunnerOrganisation } from '../entities/RunnerOrganisation';
export class CreateRunnerOrganisation {
/**
* The Organisation's name.
*/
@IsString()
@IsNotEmpty()
name: string;
/**
* Creates a RunnerOrganisation entity from this.
*/
public async toRunnerOrganisation(): Promise<RunnerOrganisation> {
let newRunnerOrganisation: RunnerOrganisation = new RunnerOrganisation();

View File

@@ -0,0 +1,36 @@
import { IsInt, IsNotEmpty, IsString } from 'class-validator';
import { getConnectionManager } from 'typeorm';
import { RunnerOrganisationNotFoundError } from '../../errors/RunnerOrganisationErrors';
import { RunnerOrganisation } from '../entities/RunnerOrganisation';
import { RunnerTeam } from '../entities/RunnerTeam';
export class CreateRunnerTeam {
/**
* The teams's name.
*/
@IsString()
@IsNotEmpty()
name: string;
/**
* The team's parent group (organisation).
*/
@IsInt()
@IsNotEmpty()
parentId: number
/**
* Creates a RunnerTeam entity from this.
*/
public async toRunnerTeam(): Promise<RunnerTeam> {
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();
}
return newRunnerTeam;
}
}

View File

@@ -1,5 +1,5 @@
import { Entity, Column, ManyToOne, ChildEntity } from "typeorm";
import { IsInt, IsNotEmpty, IsPositive, } from "class-validator";
import { IsInt, IsNotEmpty, IsPositive } from "class-validator";
import { ChildEntity, Column, ManyToOne } from "typeorm";
import { Donation } from "./Donation";
import { Runner } from "./Runner";
@@ -29,10 +29,17 @@ export class DistanceDonation extends Donation {
* The exact implementation may differ for each type of donation.
*/
@IsInt()
public get amount(): number {
public get amount() {
return this.getAmount();
}
/**
* The function that calculates the amount based on the runner object's distance.
*/
public async getAmount(): Promise<number> {
let calculatedAmount = -1;
try {
calculatedAmount = this.amountPerDistance * this.runner.distance;
calculatedAmount = this.amountPerDistance * await this.runner.distance();
} catch (error) {
throw error;
}

View File

@@ -1,10 +1,9 @@
import { PrimaryGeneratedColumn, Column, ManyToOne, Entity, TableInheritance } from "typeorm";
import {
IsInt,
IsNotEmpty,
IsOptional,
IsPositive,
IsOptional
} from "class-validator";
import { Entity, ManyToOne, PrimaryGeneratedColumn, TableInheritance } from "typeorm";
import { Participant } from "./Participant";
/**
@@ -32,5 +31,5 @@ export abstract class 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.
*/
abstract amount: number;
abstract amount: number | Promise<number>;
}

View File

@@ -1,9 +1,9 @@
import { Entity, Column, OneToMany, ManyToOne, ChildEntity } from "typeorm";
import { IsInt, IsNotEmpty, } from "class-validator";
import { Participant } from "./Participant";
import { RunnerGroup } from "./RunnerGroup";
import { IsInt, IsNotEmpty } from "class-validator";
import { ChildEntity, getConnectionManager, ManyToOne, OneToMany } from "typeorm";
import { DistanceDonation } from "./DistanceDonation";
import { Participant } from "./Participant";
import { RunnerCard } from "./RunnerCard";
import { RunnerGroup } from "./RunnerGroup";
import { Scan } from "./Scan";
/**
@@ -36,9 +36,25 @@ export class Runner extends Participant {
@OneToMany(() => Scan, scan => scan.runner, { nullable: true })
scans: Scan[];
@IsInt()
public get distance(): number {
return this.scans.filter(scan => scan.valid === true).reduce((sum, current) => sum + current.distance, 0);
/**
* 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 });
}
/**
* 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);
}
}

View File

@@ -1,13 +1,12 @@
import { PrimaryGeneratedColumn, Column, OneToMany, ManyToOne, Entity, TableInheritance } from "typeorm";
import {
IsInt,
IsNotEmpty,
IsOptional,
IsString,
IsString
} from "class-validator";
import { Column, Entity, ManyToOne, OneToMany, PrimaryGeneratedColumn, TableInheritance } from "typeorm";
import { GroupContact } from "./GroupContact";
import { Runner } from "./Runner";
import { RunnerTeam } from "./RunnerTeam";
/**
* Defines the runnerGroup interface.
@@ -44,4 +43,6 @@ export abstract class RunnerGroup {
*/
@OneToMany(() => Runner, runner => runner.group, { nullable: true })
runners: Runner[];
public abstract getRunners();
}

View File

@@ -1,7 +1,8 @@
import { Entity, Column, ManyToOne, OneToMany, ChildEntity } from "typeorm";
import { IsOptional, } from "class-validator";
import { RunnerGroup } from "./RunnerGroup";
import { IsOptional } from "class-validator";
import { ChildEntity, getConnectionManager, ManyToOne, OneToMany } from "typeorm";
import { Address } from "./Address";
import { Runner } from './Runner';
import { RunnerGroup } from "./RunnerGroup";
import { RunnerTeam } from "./RunnerTeam";
/**
@@ -23,4 +24,27 @@ 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,5 +1,6 @@
import { Entity, Column, ManyToOne, ChildEntity } from "typeorm";
import { IsNotEmpty } from "class-validator";
import { ChildEntity, getConnectionManager, ManyToOne } from "typeorm";
import { Runner } from './Runner';
import { RunnerGroup } from "./RunnerGroup";
import { RunnerOrganisation } from "./RunnerOrganisation";
@@ -16,4 +17,11 @@ export class RunnerTeam extends RunnerGroup {
@IsNotEmpty()
@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 });
}
}

View File

@@ -0,0 +1,61 @@
import {
IsInt,
IsString
} from "class-validator";
import { Participant } from '../entities/Participant';
/**
* Defines a participant response.
*/
export abstract class ResponseParticipant {
/**
* Autogenerated unique id (primary key).
*/
@IsInt()
id: number;;
/**
* The participant's first name.
*/
@IsString()
firstname: string;
/**
* The participant's middle name.
* Optional.
*/
@IsString()
middlename?: string;
/**
* The participant's last name.
*/
@IsString()
lastname: string;
/**
* The participant's phone number.
* Optional.
*/
@IsString()
phone?: string;
/**
* The participant's e-mail address.
* Optional.
*/
@IsString()
email?: string;
public constructor(participant: Participant) {
this.id = participant.id;
this.firstname = participant.firstname;
this.middlename = participant.middlename;
this.lastname = participant.lastname;
this.phone = participant.phone;
this.email = participant.email;
}
}

View File

@@ -0,0 +1,32 @@
import {
IsInt,
IsObject
} from "class-validator";
import { Runner } from '../entities/Runner';
import { RunnerGroup } from '../entities/RunnerGroup';
import { ResponseParticipant } from './ResponseParticipant';
/**
* Defines RunnerTeam's response class.
*/
export class ResponseRunner extends ResponseParticipant {
/**
* The runner's currently ran distance in meters.
* Optional.
*/
@IsInt()
distance: number;
/**
* The runner's group.
*/
@IsObject()
group: RunnerGroup;
public constructor(runner: Runner) {
super(runner);
this.distance = runner.scans.filter(scan => { scan.valid === true }).reduce((sum, current) => sum + current.distance, 0);
this.group = runner.group;
}
}

View File

@@ -0,0 +1,55 @@
import {
IsInt,
IsNotEmpty,
IsObject,
IsOptional,
IsString
} from "class-validator";
import { GroupContact } from '../entities/GroupContact';
import { RunnerGroup } from '../entities/RunnerGroup';
/**
* Defines a track of given length.
*/
export abstract class ResponseRunnerGroup {
/**
* Autogenerated unique id (primary key).
*/
@IsInt()
@IsNotEmpty()
id: number;;
/**
* The groups's name.
*/
@IsString()
@IsNotEmpty()
name: string;
/**
* The group's contact.
* Optional.
*/
@IsObject()
@IsOptional()
contact?: GroupContact;
public constructor(group: RunnerGroup) {
this.id = group.id;
this.name = group.name;
this.contact = group.contact;
}
}

View File

@@ -0,0 +1,37 @@
import {
IsArray,
IsNotEmpty,
IsObject
} from "class-validator";
import { Address } from '../entities/Address';
import { RunnerOrganisation } from '../entities/RunnerOrganisation';
import { RunnerTeam } from '../entities/RunnerTeam';
import { ResponseRunnerGroup } from './ResponseRunnerGroup';
/**
* Defines RunnerOrgs's response class.
*/
export class ResponseRunnerOrganisation extends ResponseRunnerGroup {
/**
* The orgs's address.
* Optional.
*/
@IsObject()
@IsNotEmpty()
address?: Address;
/**
* The orgs associated teams.
*/
@IsObject()
@IsArray()
teams: RunnerTeam[];
public constructor(org: RunnerOrganisation) {
super(org);
this.address = org.address;
this.teams = org.teams;
}
}

View File

@@ -0,0 +1,26 @@
import {
IsNotEmpty,
IsObject
} from "class-validator";
import { RunnerOrganisation } from '../entities/RunnerOrganisation';
import { RunnerTeam } from '../entities/RunnerTeam';
import { ResponseRunnerGroup } from './ResponseRunnerGroup';
/**
* Defines RunnerTeam's response class.
*/
export class ResponseRunnerTeam extends ResponseRunnerGroup {
/**
* The team's parent group (organisation).
* Optional.
*/
@IsObject()
@IsNotEmpty()
parentGroup: RunnerOrganisation;
public constructor(team: RunnerTeam) {
super(team);
this.parentGroup = team.parentGroup;
}
}

View File

@@ -0,0 +1,35 @@
import {
IsInt,
IsString
} from "class-validator";
import { Track } from '../entities/Track';
/**
* Defines a track of given length.
*/
export class ResponseTrack {
/**
* Autogenerated unique id (primary key).
*/
@IsInt()
id: number;;
/**
* The track's name.
*/
@IsString()
name: string;
/**
* The track's length/distance in meters.
*/
@IsInt()
distance: number;
public constructor(track: Track) {
this.id = track.id;
this.name = track.name;
this.distance = track.distance;
}
}