Fixed bugs concerning posts

ref #13
This commit is contained in:
Nicolai Ort 2020-12-05 10:59:15 +01:00
parent ef54dd5e9c
commit aca13f7308
5 changed files with 11 additions and 26 deletions

View File

@ -55,7 +55,8 @@ export class RunnerController {
return error;
}
return new ResponseRunner(await this.runnerRepository.save(runner));
runner = await this.runnerRepository.save(runner)
return new ResponseRunner(await this.runnerRepository.findOne(runner, { relations: ['scans', 'group'] }));
}
@Put('/:id')

View File

@ -58,9 +58,8 @@ export class RunnerOrganisationController {
}
runnerOrganisation = await this.runnerOrganisationRepository.save(runnerOrganisation);
runnerOrganisation = await this.runnerOrganisationRepository.findOne(runnerOrganisation, { relations: ['address', 'contact', 'teams'] });
return new ResponseRunnerOrganisation(runnerOrganisation);
return new ResponseRunnerOrganisation(await this.runnerOrganisationRepository.findOne(runnerOrganisation, { relations: ['address', 'contact', 'teams'] }));
}
@Put('/:id')

View File

@ -28,18 +28,10 @@ export class DistanceDonation extends 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.
*/
@IsInt()
public get amount() {
return this.getAmount();
}
/**
* The function that calculates the amount based on the runner object's distance.
*/
public async getAmount(): Promise<number> {
public get amount(): number {
let calculatedAmount = -1;
try {
calculatedAmount = this.amountPerDistance * await this.runner.distance();
calculatedAmount = this.amountPerDistance * this.runner.distance;
} catch (error) {
throw error;
}

View File

@ -1,5 +1,5 @@
import { IsInt, IsNotEmpty } from "class-validator";
import { ChildEntity, getConnectionManager, ManyToOne, OneToMany } from "typeorm";
import { ChildEntity, ManyToOne, OneToMany } from "typeorm";
import { DistanceDonation } from "./DistanceDonation";
import { Participant } from "./Participant";
import { RunnerCard } from "./RunnerCard";
@ -36,25 +36,18 @@ export class Runner extends Participant {
@OneToMany(() => Scan, scan => scan.runner, { nullable: true })
scans: Scan[];
/**
* Returns all scans associated with this runner.
*/
public async getScans(): Promise<Scan[]> {
return await getConnectionManager().get().getRepository(Scan).find({ runner: this });
}
/**
* Returns all valid scans associated with this runner.
*/
public async getValidScans(): Promise<Scan[]> {
return (await this.getScans()).filter(scan => { scan.valid === true });
public get validScans(): Scan[] {
return this.scans.filter(scan => { scan.valid === true });
}
/**
* Returns the total distance ran by this runner.
*/
@IsInt()
public async distance(): Promise<number> {
return await (await this.getValidScans()).reduce((sum, current) => sum + current.distance, 0);
public get distance(): number {
return this.validScans.reduce((sum, current) => sum + current.distance, 0);
}
}

View File

@ -15,7 +15,7 @@ export class RunnerTeam extends RunnerGroup {
* Optional
*/
@IsNotEmpty()
@ManyToOne(() => RunnerOrganisation, org => org.teams, { nullable: false })
@ManyToOne(() => RunnerOrganisation, org => org.teams, { nullable: true })
parentGroup?: RunnerOrganisation;
/**