diff --git a/src/models/entities/DistanceDonation.ts b/src/models/entities/DistanceDonation.ts index b54c642..669f88f 100644 --- a/src/models/entities/DistanceDonation.ts +++ b/src/models/entities/DistanceDonation.ts @@ -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"; diff --git a/src/models/entities/Runner.ts b/src/models/entities/Runner.ts index 843d36a..7a8d0ad 100644 --- a/src/models/entities/Runner.ts +++ b/src/models/entities/Runner.ts @@ -36,13 +36,25 @@ export class Runner extends Participant { @OneToMany(() => Scan, scan => scan.runner, { nullable: true }) scans: Scan[]; - @IsInt() - public get distance(): number { - getConnectionManager().get().getRepository(Scan).find({ runner: this }) - .then(myScans => { - return myScans.reduce((sum, current) => sum + current.distance, 0); - }) - .catch(err => { throw err }) - .finally(do => { return -1; }); + /** + * Returns all scans associated with this runner. + */ + public async getScans(): Promise { + return await getConnectionManager().get().getRepository(Scan).find({ runner: this }); } -} + + /** + * Returns all valid scans associated with this runner. + */ + public async getValidScans(): Promise { + return (await this.getScans()).filter(scan => { scan.valid === true }); + } + + /** + * Returns the total distance ran by this runner. + */ + @IsInt() + public async distance(): Promise { + return await (await this.getValidScans()).reduce((sum, current) => sum + current.distance, 0); + } +} \ No newline at end of file