2021-01-09 13:52:08 +00:00
|
|
|
import { IsInt, IsPositive } from 'class-validator';
|
2021-01-06 18:44:20 +00:00
|
|
|
import { getConnection } from 'typeorm';
|
2021-01-10 15:23:09 +00:00
|
|
|
import { RunnerCardNotFoundError } from '../../../errors/RunnerCardErrors';
|
|
|
|
import { RunnerNotFoundError } from '../../../errors/RunnerErrors';
|
|
|
|
import { ScanStationNotFoundError } from '../../../errors/ScanStationErrors';
|
|
|
|
import { RunnerCard } from '../../entities/RunnerCard';
|
|
|
|
import { ScanStation } from '../../entities/ScanStation';
|
|
|
|
import { TrackScan } from '../../entities/TrackScan';
|
2021-01-06 18:44:20 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* This classed is used to create a new Scan entity from a json body (post request).
|
|
|
|
*/
|
2021-01-09 13:52:08 +00:00
|
|
|
export class CreateTrackScan {
|
2021-01-06 18:44:20 +00:00
|
|
|
/**
|
|
|
|
* The runnerCard associated with the scan.
|
|
|
|
* This get's saved for documentation and management purposes.
|
|
|
|
*/
|
2021-01-09 13:52:08 +00:00
|
|
|
@IsInt()
|
|
|
|
@IsPositive()
|
2021-01-06 18:44:20 +00:00
|
|
|
card: number;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The scanning station that created the scan.
|
|
|
|
* Mainly used for logging and traceing back scans (or errors)
|
|
|
|
*/
|
2021-01-09 13:52:08 +00:00
|
|
|
@IsInt()
|
|
|
|
@IsPositive()
|
2021-01-06 18:44:20 +00:00
|
|
|
station: number;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a new Track entity from this.
|
|
|
|
*/
|
2021-01-10 15:31:55 +00:00
|
|
|
public async toEntity(): Promise<TrackScan> {
|
2021-01-06 18:44:20 +00:00
|
|
|
let newScan: TrackScan = new TrackScan();
|
|
|
|
|
|
|
|
newScan.station = await this.getStation();
|
|
|
|
newScan.card = await this.getCard();
|
|
|
|
|
|
|
|
newScan.track = newScan.station.track;
|
|
|
|
newScan.runner = newScan.card.runner;
|
|
|
|
|
|
|
|
if (!newScan.runner) {
|
|
|
|
throw new RunnerNotFoundError();
|
|
|
|
}
|
|
|
|
|
2021-01-09 15:47:54 +00:00
|
|
|
newScan.timestamp = Math.round(new Date().getTime() / 1000);
|
2021-01-06 18:44:20 +00:00
|
|
|
newScan.valid = await this.validateScan(newScan);
|
|
|
|
|
|
|
|
return newScan;
|
|
|
|
}
|
|
|
|
|
|
|
|
public async getCard(): Promise<RunnerCard> {
|
|
|
|
const track = await getConnection().getRepository(RunnerCard).findOne({ id: this.card }, { relations: ["runner"] });
|
|
|
|
if (!track) {
|
2021-01-09 15:47:54 +00:00
|
|
|
throw new RunnerCardNotFoundError();
|
2021-01-06 18:44:20 +00:00
|
|
|
}
|
|
|
|
return track;
|
|
|
|
}
|
|
|
|
|
|
|
|
public async getStation(): Promise<ScanStation> {
|
2021-01-09 15:47:54 +00:00
|
|
|
const station = await getConnection().getRepository(ScanStation).findOne({ id: this.station }, { relations: ["track"] });
|
|
|
|
if (!station) {
|
|
|
|
throw new ScanStationNotFoundError();
|
2021-01-06 18:44:20 +00:00
|
|
|
}
|
2021-01-09 15:47:54 +00:00
|
|
|
return station;
|
2021-01-06 18:44:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public async validateScan(scan: TrackScan): Promise<boolean> {
|
2021-01-09 15:47:54 +00:00
|
|
|
const scans = await getConnection().getRepository(TrackScan).find({ where: { runner: scan.runner, valid: true }, relations: ["track"] });
|
2021-01-06 18:44:20 +00:00
|
|
|
if (scans.length == 0) { return true; }
|
|
|
|
|
2021-01-09 15:47:54 +00:00
|
|
|
const newestScan = scans[scans.length - 1];
|
|
|
|
if ((scan.timestamp - newestScan.timestamp) > scan.track.minimumLapTime) {
|
2021-01-06 18:44:20 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|