diff --git a/src/models/entities/DistanceDonation.ts b/src/models/entities/DistanceDonation.ts index 669f88f..d5610d7 100644 --- a/src/models/entities/DistanceDonation.ts +++ b/src/models/entities/DistanceDonation.ts @@ -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 { let calculatedAmount = -1; try { - calculatedAmount = this.amountPerDistance * this.runner.distance; + calculatedAmount = this.amountPerDistance * await this.runner.distance(); } catch (error) { throw error; } diff --git a/src/models/entities/Donation.ts b/src/models/entities/Donation.ts index c4ab699..2609a98 100644 --- a/src/models/entities/Donation.ts +++ b/src/models/entities/Donation.ts @@ -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; } \ No newline at end of file diff --git a/src/models/entities/Runner.ts b/src/models/entities/Runner.ts index 2496d98..7a8d0ad 100644 --- a/src/models/entities/Runner.ts +++ b/src/models/entities/Runner.ts @@ -54,7 +54,7 @@ export class Runner extends Participant { * Returns the total distance ran by this runner. */ @IsInt() - public get distance(): number { - return 0; + public async distance(): Promise { + return await (await this.getValidScans()).reduce((sum, current) => sum + current.distance, 0); } } \ No newline at end of file diff --git a/src/models/responses/ResponseRunner.ts b/src/models/responses/ResponseRunner.ts index 677ca93..01327c2 100644 --- a/src/models/responses/ResponseRunner.ts +++ b/src/models/responses/ResponseRunner.ts @@ -26,7 +26,7 @@ export class ResponseRunner extends ResponseParticipant { public constructor(runner: Runner) { super(runner); - this.distance = runner.scans.reduce((sum, current) => sum + current.distance, 0); + this.distance = runner.scans.filter(scan => { scan.valid === true }).reduce((sum, current) => sum + current.distance, 0); this.group = runner.group; } }