Added stats and stats responses for orgs

ref #56
This commit is contained in:
2020-12-30 16:59:07 +01:00
parent 53a01ad977
commit 5d31d8d1a2
3 changed files with 100 additions and 3 deletions

View File

@@ -1,6 +1,7 @@
import { IsOptional } from "class-validator";
import { IsInt, IsOptional } from "class-validator";
import { ChildEntity, ManyToOne, OneToMany } from "typeorm";
import { Address } from "./Address";
import { Runner } from './Runner';
import { RunnerGroup } from "./RunnerGroup";
import { RunnerTeam } from "./RunnerTeam";
@@ -24,4 +25,32 @@ export class RunnerOrganisation extends RunnerGroup {
*/
@OneToMany(() => RunnerTeam, team => team.parentGroup, { nullable: true })
teams: RunnerTeam[];
/**
* Returns all runners associated with this organisation (directly or indirectly via teams).
*/
public get allRunners(): Runner[] {
let returnRunners: Runner[] = new Array<Runner>();
returnRunners.push(...this.runners);
for (let team of this.teams) {
returnRunners.push(...team.runners)
}
return returnRunners;
}
/**
* Returns the total distance ran by this group's runners based on all their valid scans.
*/
@IsInt()
public get distance(): number {
return this.allRunners.reduce((sum, current) => sum + current.distance, 0);
}
/**
* Returns the total donations a runner has collected based on his linked donations and distance ran.
*/
@IsInt()
public get distanceDonationAmount(): number {
return this.allRunners.reduce((sum, current) => sum + current.distanceDonationAmount, 0);
}
}

View File

@@ -0,0 +1,47 @@
import {
IsInt,
IsString
} from "class-validator";
import { RunnerOrganisation } from '../entities/RunnerOrganisation';
/**
* Defines the org stats response.
* This differs from the normal org responce.
*/
export class ResponseStatsOrgnisation {
/**
* The orgs's id.
*/
@IsInt()
id: number;
/**
* The orgs's name.
*/
@IsString()
name: string;
/**
* The orgs's runner's currently ran distance in meters.
*/
@IsInt()
distance: number;
/**
* The orgs's currently collected donations.
*/
@IsInt()
donationAmount: number;
/**
* Creates a ResponseRunner object from a runner.
* @param runner The user the response shall be build for.
*/
public constructor(org: RunnerOrganisation) {
this.name = org.name;
this.id = org.id;
this.distance = org.distance;
this.donationAmount = org.distanceDonationAmount;
}
}