Implemented proper scan invalidation

ref #78
This commit is contained in:
Nicolai Ort 2021-01-09 16:47:54 +01:00
parent 0c86e5dae1
commit 61cf0fc08d
3 changed files with 14 additions and 14 deletions

View File

@ -1,6 +1,8 @@
import { IsInt, IsPositive } from 'class-validator'; import { IsInt, IsPositive } from 'class-validator';
import { getConnection } from 'typeorm'; import { getConnection } from 'typeorm';
import { RunnerCardNotFoundError } from '../../errors/RunnerCardErrors';
import { RunnerNotFoundError } from '../../errors/RunnerErrors'; import { RunnerNotFoundError } from '../../errors/RunnerErrors';
import { ScanStationNotFoundError } from '../../errors/ScanStationErrors';
import { RunnerCard } from '../entities/RunnerCard'; import { RunnerCard } from '../entities/RunnerCard';
import { ScanStation } from '../entities/ScanStation'; import { ScanStation } from '../entities/ScanStation';
import { TrackScan } from '../entities/TrackScan'; import { TrackScan } from '../entities/TrackScan';
@ -41,7 +43,7 @@ export class CreateTrackScan {
throw new RunnerNotFoundError(); throw new RunnerNotFoundError();
} }
newScan.timestamp = new Date(Date.now()).toString(); newScan.timestamp = Math.round(new Date().getTime() / 1000);
newScan.valid = await this.validateScan(newScan); newScan.valid = await this.validateScan(newScan);
return newScan; return newScan;
@ -50,25 +52,25 @@ export class CreateTrackScan {
public async getCard(): Promise<RunnerCard> { public async getCard(): Promise<RunnerCard> {
const track = await getConnection().getRepository(RunnerCard).findOne({ id: this.card }, { relations: ["runner"] }); const track = await getConnection().getRepository(RunnerCard).findOne({ id: this.card }, { relations: ["runner"] });
if (!track) { if (!track) {
throw new Error(); throw new RunnerCardNotFoundError();
} }
return track; return track;
} }
public async getStation(): Promise<ScanStation> { public async getStation(): Promise<ScanStation> {
const track = await getConnection().getRepository(ScanStation).findOne({ id: this.card }, { relations: ["track"] }); const station = await getConnection().getRepository(ScanStation).findOne({ id: this.station }, { relations: ["track"] });
if (!track) { if (!station) {
throw new Error(); throw new ScanStationNotFoundError();
} }
return track; return station;
} }
public async validateScan(scan: TrackScan): Promise<boolean> { public async validateScan(scan: TrackScan): Promise<boolean> {
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; } if (scans.length == 0) { return true; }
const newestScan = scans[0]; const newestScan = scans[scans.length - 1];
if ((new Date(scan.timestamp).getTime() - new Date(newestScan.timestamp).getTime()) > scan.track.minimumLapTime) { if ((scan.timestamp - newestScan.timestamp) > scan.track.minimumLapTime) {
return true; return true;
} }

View File

@ -1,5 +1,4 @@
import { import {
IsDateString,
IsInt, IsInt,
IsNotEmpty, IsNotEmpty,
@ -57,9 +56,8 @@ export class TrackScan extends Scan {
* Will be used to implement fraud detection. * Will be used to implement fraud detection.
*/ */
@Column() @Column()
@IsDateString() @IsInt()
@IsNotEmpty() timestamp: number;
timestamp: string;
/** /**
* Turns this entity into it's response class. * Turns this entity into it's response class.

View File

@ -32,7 +32,7 @@ export class ResponseTrackScan extends ResponseScan {
*/ */
@IsDateString() @IsDateString()
@IsNotEmpty() @IsNotEmpty()
timestamp: string; timestamp: number;
/** /**
* Creates a ResponseTrackScan object from a scan. * Creates a ResponseTrackScan object from a scan.