From 61cf0fc08d3af733b30640880f4b3981cd9f827a Mon Sep 17 00:00:00 2001 From: Nicolai Ort Date: Sat, 9 Jan 2021 16:47:54 +0100 Subject: [PATCH] Implemented proper scan invalidation ref #78 --- src/models/actions/CreateTrackScan.ts | 20 +++++++++++--------- src/models/entities/TrackScan.ts | 6 ++---- src/models/responses/ResponseTrackScan.ts | 2 +- 3 files changed, 14 insertions(+), 14 deletions(-) diff --git a/src/models/actions/CreateTrackScan.ts b/src/models/actions/CreateTrackScan.ts index 44877f0..51078c4 100644 --- a/src/models/actions/CreateTrackScan.ts +++ b/src/models/actions/CreateTrackScan.ts @@ -1,6 +1,8 @@ import { IsInt, IsPositive } from 'class-validator'; import { getConnection } from 'typeorm'; +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'; @@ -41,7 +43,7 @@ export class CreateTrackScan { throw new RunnerNotFoundError(); } - newScan.timestamp = new Date(Date.now()).toString(); + newScan.timestamp = Math.round(new Date().getTime() / 1000); newScan.valid = await this.validateScan(newScan); return newScan; @@ -50,25 +52,25 @@ export class CreateTrackScan { public async getCard(): Promise { const track = await getConnection().getRepository(RunnerCard).findOne({ id: this.card }, { relations: ["runner"] }); if (!track) { - throw new Error(); + throw new RunnerCardNotFoundError(); } return track; } public async getStation(): Promise { - const track = await getConnection().getRepository(ScanStation).findOne({ id: this.card }, { relations: ["track"] }); - if (!track) { - throw new Error(); + const station = await getConnection().getRepository(ScanStation).findOne({ id: this.station }, { relations: ["track"] }); + if (!station) { + throw new ScanStationNotFoundError(); } - return track; + return station; } public async validateScan(scan: TrackScan): Promise { - const scans = await getConnection().getRepository(TrackScan).find({ where: { runner: scan.runner }, relations: ["track"] }); + const scans = await getConnection().getRepository(TrackScan).find({ where: { runner: scan.runner, valid: true }, relations: ["track"] }); if (scans.length == 0) { return true; } - const newestScan = scans[0]; - if ((new Date(scan.timestamp).getTime() - new Date(newestScan.timestamp).getTime()) > scan.track.minimumLapTime) { + const newestScan = scans[scans.length - 1]; + if ((scan.timestamp - newestScan.timestamp) > scan.track.minimumLapTime) { return true; } diff --git a/src/models/entities/TrackScan.ts b/src/models/entities/TrackScan.ts index 8c4143b..cd8049a 100644 --- a/src/models/entities/TrackScan.ts +++ b/src/models/entities/TrackScan.ts @@ -1,5 +1,4 @@ import { - IsDateString, IsInt, IsNotEmpty, @@ -57,9 +56,8 @@ export class TrackScan extends Scan { * Will be used to implement fraud detection. */ @Column() - @IsDateString() - @IsNotEmpty() - timestamp: string; + @IsInt() + timestamp: number; /** * Turns this entity into it's response class. diff --git a/src/models/responses/ResponseTrackScan.ts b/src/models/responses/ResponseTrackScan.ts index dba450c..d52b4f7 100644 --- a/src/models/responses/ResponseTrackScan.ts +++ b/src/models/responses/ResponseTrackScan.ts @@ -32,7 +32,7 @@ export class ResponseTrackScan extends ResponseScan { */ @IsDateString() @IsNotEmpty() - timestamp: string; + timestamp: number; /** * Creates a ResponseTrackScan object from a scan.