64 lines
1.8 KiB
TypeScript
64 lines
1.8 KiB
TypeScript
import { IsInt, IsOptional } from "class-validator";
|
|
import { ChildEntity, Column, OneToMany } from "typeorm";
|
|
import { ResponseRunnerOrganisation } from '../responses/ResponseRunnerOrganisation';
|
|
import { Address } from './Address';
|
|
import { Runner } from './Runner';
|
|
import { RunnerGroup } from "./RunnerGroup";
|
|
import { RunnerTeam } from "./RunnerTeam";
|
|
|
|
/**
|
|
* Defines the RunnerOrganisation entity.
|
|
* This usually is a school, club or company.
|
|
*/
|
|
@ChildEntity()
|
|
export class RunnerOrganisation extends RunnerGroup {
|
|
|
|
/**
|
|
* The organisations's address.
|
|
*/
|
|
@IsOptional()
|
|
@Column(type => Address)
|
|
address?: Address;
|
|
|
|
/**
|
|
* The organisation's teams.
|
|
* Used to link teams to a organisation.
|
|
*/
|
|
@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);
|
|
}
|
|
|
|
/**
|
|
* Turns this entity into it's response class.
|
|
*/
|
|
public toResponse(): ResponseRunnerOrganisation {
|
|
return new ResponseRunnerOrganisation(this);
|
|
}
|
|
} |