Fixed amount calculations

This commit is contained in:
Nicolai Ort 2020-12-04 22:07:30 +01:00
parent b480912bd8
commit afef95e14e
4 changed files with 15 additions and 9 deletions

View File

@ -29,10 +29,17 @@ export class DistanceDonation extends Donation {
* The exact implementation may differ for each type of donation. * The exact implementation may differ for each type of donation.
*/ */
@IsInt() @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; let calculatedAmount = -1;
try { try {
calculatedAmount = this.amountPerDistance * this.runner.distance; calculatedAmount = this.amountPerDistance * await this.runner.distance();
} catch (error) { } catch (error) {
throw error; throw error;
} }

View File

@ -1,10 +1,9 @@
import { PrimaryGeneratedColumn, Column, ManyToOne, Entity, TableInheritance } from "typeorm";
import { import {
IsInt, IsInt,
IsNotEmpty, IsNotEmpty,
IsOptional, IsOptional
IsPositive,
} from "class-validator"; } from "class-validator";
import { Entity, ManyToOne, PrimaryGeneratedColumn, TableInheritance } from "typeorm";
import { Participant } from "./Participant"; 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 donation's amount in cents (or whatever your currency's smallest unit is.).
* The exact implementation may differ for each type of donation. * The exact implementation may differ for each type of donation.
*/ */
abstract amount: number; abstract amount: number | Promise<number>;
} }

View File

@ -54,7 +54,7 @@ export class Runner extends Participant {
* Returns the total distance ran by this runner. * Returns the total distance ran by this runner.
*/ */
@IsInt() @IsInt()
public get distance(): number { public async distance(): Promise<number> {
return 0; return await (await this.getValidScans()).reduce((sum, current) => sum + current.distance, 0);
} }
} }

View File

@ -26,7 +26,7 @@ export class ResponseRunner extends ResponseParticipant {
public constructor(runner: Runner) { public constructor(runner: Runner) {
super(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; this.group = runner.group;
} }
} }