diff --git a/src/controllers/RunnerController.ts b/src/controllers/RunnerController.ts index 14658da..96853cc 100644 --- a/src/controllers/RunnerController.ts +++ b/src/controllers/RunnerController.ts @@ -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') diff --git a/src/controllers/RunnerOrganisationController.ts b/src/controllers/RunnerOrganisationController.ts index bed6eec..451a6e7 100644 --- a/src/controllers/RunnerOrganisationController.ts +++ b/src/controllers/RunnerOrganisationController.ts @@ -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') diff --git a/src/models/entities/DistanceDonation.ts b/src/models/entities/DistanceDonation.ts index 9051849..3103865 100644 --- a/src/models/entities/DistanceDonation.ts +++ b/src/models/entities/DistanceDonation.ts @@ -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 { + 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; } diff --git a/src/models/entities/Runner.ts b/src/models/entities/Runner.ts index 5454f64..6f4033a 100644 --- a/src/models/entities/Runner.ts +++ b/src/models/entities/Runner.ts @@ -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 { - 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 }); + 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 { - 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); } } \ No newline at end of file diff --git a/src/models/entities/RunnerTeam.ts b/src/models/entities/RunnerTeam.ts index 21cdcb4..44a047a 100644 --- a/src/models/entities/RunnerTeam.ts +++ b/src/models/entities/RunnerTeam.ts @@ -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; /**