Added basics for scan creation (to be tested after scanstations got added)
ref #67
This commit is contained in:
parent
aeec2e1c32
commit
72b5ca4153
@ -1,7 +1,8 @@
|
|||||||
import { Authorized, Get, JsonController, OnUndefined, Param } from 'routing-controllers';
|
import { Authorized, Body, Get, JsonController, OnUndefined, Param, Post } from 'routing-controllers';
|
||||||
import { OpenAPI, ResponseSchema } from 'routing-controllers-openapi';
|
import { OpenAPI, ResponseSchema } from 'routing-controllers-openapi';
|
||||||
import { getConnectionManager, Repository } from 'typeorm';
|
import { getConnectionManager, Repository } from 'typeorm';
|
||||||
import { ScanNotFoundError } from '../errors/ScanErrors';
|
import { ScanNotFoundError } from '../errors/ScanErrors';
|
||||||
|
import { CreateScan } from '../models/actions/CreateScan';
|
||||||
import { Scan } from '../models/entities/Scan';
|
import { Scan } from '../models/entities/Scan';
|
||||||
import { ResponseScan } from '../models/responses/ResponseScan';
|
import { ResponseScan } from '../models/responses/ResponseScan';
|
||||||
import { ResponseTrackScan } from '../models/responses/ResponseTrackScan';
|
import { ResponseTrackScan } from '../models/responses/ResponseTrackScan';
|
||||||
@ -33,7 +34,7 @@ export class ScanController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Get('/:id')
|
@Get('/:id')
|
||||||
@Authorized("DONOR:GET")
|
@Authorized("SCAN:GET")
|
||||||
@ResponseSchema(ResponseScan)
|
@ResponseSchema(ResponseScan)
|
||||||
@ResponseSchema(ResponseTrackScan)
|
@ResponseSchema(ResponseTrackScan)
|
||||||
@ResponseSchema(ScanNotFoundError, { statusCode: 404 })
|
@ResponseSchema(ScanNotFoundError, { statusCode: 404 })
|
||||||
@ -45,21 +46,15 @@ export class ScanController {
|
|||||||
return scan;
|
return scan;
|
||||||
}
|
}
|
||||||
|
|
||||||
// @Post()
|
@Post()
|
||||||
// @Authorized("DONOR:CREATE")
|
@Authorized("SCAN:CREATE")
|
||||||
// @ResponseSchema(ResponseDonor)
|
@ResponseSchema(ResponseScan)
|
||||||
// @OpenAPI({ description: 'Create a new runner. <br> Please remeber to provide the runner\'s group\'s id.' })
|
@OpenAPI({ description: 'Create a new runner. <br> Please remeber to provide the runner\'s group\'s id.' })
|
||||||
// async post(@Body({ validate: true }) createRunner: CreateDonor) {
|
async post(@Body({ validate: true }) createScan: CreateScan) {
|
||||||
// let donor;
|
let scan = await createScan.toScan();
|
||||||
// try {
|
scan = await this.scanRepository.save(scan)
|
||||||
// donor = await createRunner.toDonor();
|
return (await this.scanRepository.findOne(scan)).toResponse();
|
||||||
// } catch (error) {
|
}
|
||||||
// throw error;
|
|
||||||
// }
|
|
||||||
|
|
||||||
// donor = await this.donorRepository.save(donor)
|
|
||||||
// return new ResponseDonor(await this.donorRepository.findOne(donor));
|
|
||||||
// }
|
|
||||||
|
|
||||||
// @Put('/:id')
|
// @Put('/:id')
|
||||||
// @Authorized("DONOR:UPDATE")
|
// @Authorized("DONOR:UPDATE")
|
||||||
|
11
src/models/actions/CreateScan.ts
Normal file
11
src/models/actions/CreateScan.ts
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
import { Scan } from '../entities/Scan';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This classed is used to create a new Scan entity from a json body (post request).
|
||||||
|
*/
|
||||||
|
export abstract class CreateScan {
|
||||||
|
/**
|
||||||
|
* Creates a new Scan entity from this.
|
||||||
|
*/
|
||||||
|
public abstract toScan(): Promise<Scan>;
|
||||||
|
}
|
84
src/models/actions/CreateTrackScan.ts
Normal file
84
src/models/actions/CreateTrackScan.ts
Normal file
@ -0,0 +1,84 @@
|
|||||||
|
import { IsNotEmpty } from 'class-validator';
|
||||||
|
import { getConnection } from 'typeorm';
|
||||||
|
import { RunnerNotFoundError } from '../../errors/RunnerErrors';
|
||||||
|
import { RunnerCard } from '../entities/RunnerCard';
|
||||||
|
import { ScanStation } from '../entities/ScanStation';
|
||||||
|
import { TrackScan } from '../entities/TrackScan';
|
||||||
|
import { CreateScan } from './CreateScan';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This classed is used to create a new Scan entity from a json body (post request).
|
||||||
|
*/
|
||||||
|
export class CreateTrackScan extends CreateScan {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The scan's associated track.
|
||||||
|
* This is used to determine the scan's distance.
|
||||||
|
*/
|
||||||
|
@IsNotEmpty()
|
||||||
|
track: number;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The runnerCard associated with the scan.
|
||||||
|
* This get's saved for documentation and management purposes.
|
||||||
|
*/
|
||||||
|
@IsNotEmpty()
|
||||||
|
card: number;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The scanning station that created the scan.
|
||||||
|
* Mainly used for logging and traceing back scans (or errors)
|
||||||
|
*/
|
||||||
|
@IsNotEmpty()
|
||||||
|
station: number;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a new Track entity from this.
|
||||||
|
*/
|
||||||
|
public async toScan(): Promise<TrackScan> {
|
||||||
|
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();
|
||||||
|
}
|
||||||
|
|
||||||
|
newScan.timestamp = new Date(Date.now()).toString();
|
||||||
|
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) {
|
||||||
|
throw new Error();
|
||||||
|
}
|
||||||
|
return track;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async getStation(): Promise<ScanStation> {
|
||||||
|
const track = await getConnection().getRepository(ScanStation).findOne({ id: this.card }, { relations: ["track"] });
|
||||||
|
if (!track) {
|
||||||
|
throw new Error();
|
||||||
|
}
|
||||||
|
return track;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async validateScan(scan: TrackScan): Promise<boolean> {
|
||||||
|
const scans = await getConnection().getRepository(TrackScan).find({ where: { runner: scan.runner }, 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) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user