Switched to full update from partial and resolved relation

ref #90
This commit is contained in:
Nicolai Ort 2021-01-15 18:33:53 +01:00
parent 1b799a6973
commit bae8290273

View File

@ -1,4 +1,4 @@
import { IsBoolean, IsInt, IsOptional } from 'class-validator'; import { IsBoolean, IsInt, IsOptional, IsPositive } from 'class-validator';
import { getConnection } from 'typeorm'; import { getConnection } from 'typeorm';
import { RunnerNotFoundError } from '../../../errors/RunnerErrors'; import { RunnerNotFoundError } from '../../../errors/RunnerErrors';
import { ScanStationNotFoundError } from '../../../errors/ScanStationErrors'; import { ScanStationNotFoundError } from '../../../errors/ScanStationErrors';
@ -22,8 +22,8 @@ export abstract class UpdateTrackScan {
* This is important to link ran distances to runners. * This is important to link ran distances to runners.
*/ */
@IsInt() @IsInt()
@IsOptional() @IsPositive()
runner?: number; runner: number;
/** /**
* Is the updated scan valid (for fraud reasons). * Is the updated scan valid (for fraud reasons).
@ -37,8 +37,8 @@ export abstract class UpdateTrackScan {
* This is important to link ran distances to runners. * This is important to link ran distances to runners.
*/ */
@IsInt() @IsInt()
@IsOptional() @IsPositive()
public station?: number; public station: number;
/** /**
* Update a TrackScan entity based on this. * Update a TrackScan entity based on this.
@ -46,12 +46,8 @@ export abstract class UpdateTrackScan {
*/ */
public async update(scan: TrackScan): Promise<TrackScan> { public async update(scan: TrackScan): Promise<TrackScan> {
scan.valid = this.valid; scan.valid = this.valid;
if (this.runner) {
scan.runner = await this.getRunner(); scan.runner = await this.getRunner();
}
if (this.station) {
scan.station = await this.getStation(); scan.station = await this.getStation();
}
scan.track = scan.station.track; scan.track = scan.station.track;
return scan; return scan;
@ -72,7 +68,7 @@ export abstract class UpdateTrackScan {
* Gets a runner based on the runner id provided via this.runner. * Gets a runner based on the runner id provided via this.runner.
*/ */
public async getStation(): Promise<ScanStation> { public async getStation(): Promise<ScanStation> {
const station = await getConnection().getRepository(ScanStation).findOne({ id: this.station }); const station = await getConnection().getRepository(ScanStation).findOne({ id: this.station }, { relations: ['track'] });
if (!station) { if (!station) {
throw new ScanStationNotFoundError(); throw new ScanStationNotFoundError();
} }