parent
eea656bd7b
commit
857de9ffcc
53
src/models/actions/CreateScanStation.ts
Normal file
53
src/models/actions/CreateScanStation.ts
Normal file
@ -0,0 +1,53 @@
|
|||||||
|
import * as argon2 from "argon2";
|
||||||
|
import { IsInt, IsOptional, IsPositive, IsString } from 'class-validator';
|
||||||
|
import crypto from 'crypto';
|
||||||
|
import { getConnection } from 'typeorm';
|
||||||
|
import * as uuid from 'uuid';
|
||||||
|
import { TrackNotFoundError } from '../../errors/TrackErrors';
|
||||||
|
import { ScanStation } from '../entities/ScanStation';
|
||||||
|
import { Track } from '../entities/Track';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This classed is used to create a new StatsClient entity from a json body (post request).
|
||||||
|
*/
|
||||||
|
export class CreateStatsClient {
|
||||||
|
/**
|
||||||
|
* The new client's description.
|
||||||
|
*/
|
||||||
|
@IsString()
|
||||||
|
@IsOptional()
|
||||||
|
description?: string;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The scan's associated track.
|
||||||
|
* This is used to determine the scan's distance.
|
||||||
|
*/
|
||||||
|
@IsInt()
|
||||||
|
@IsPositive()
|
||||||
|
track: number;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Converts this to a ScanStation entity.
|
||||||
|
*/
|
||||||
|
public async toEntity(): Promise<ScanStation> {
|
||||||
|
let newStation: ScanStation = new ScanStation();
|
||||||
|
|
||||||
|
newStation.description = this.description;
|
||||||
|
newStation.track = await this.getTrack();
|
||||||
|
|
||||||
|
let newUUID = uuid.v4().toUpperCase();
|
||||||
|
newStation.prefix = crypto.createHash("sha3-512").update(newUUID).digest('hex').substring(0, 7).toUpperCase();
|
||||||
|
newStation.key = await argon2.hash(newStation.prefix + "." + newUUID);
|
||||||
|
newStation.cleartextkey = newStation.prefix + "." + newUUID;
|
||||||
|
|
||||||
|
return newStation;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async getTrack(): Promise<Track> {
|
||||||
|
const track = await getConnection().getRepository(Track).findOne({ id: this.track });
|
||||||
|
if (!track) {
|
||||||
|
throw new TrackNotFoundError();
|
||||||
|
}
|
||||||
|
return track;
|
||||||
|
}
|
||||||
|
}
|
@ -1,5 +1,4 @@
|
|||||||
import {
|
import {
|
||||||
IsBoolean,
|
|
||||||
IsInt,
|
IsInt,
|
||||||
IsNotEmpty,
|
IsNotEmpty,
|
||||||
IsOptional,
|
IsOptional,
|
||||||
@ -39,6 +38,14 @@ export class ScanStation {
|
|||||||
@ManyToOne(() => Track, track => track.stations, { nullable: false })
|
@ManyToOne(() => Track, track => track.stations, { nullable: false })
|
||||||
track: Track;
|
track: Track;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The client's api key prefix.
|
||||||
|
* This is used identitfy a client by it's api key.
|
||||||
|
*/
|
||||||
|
@Column({ unique: true })
|
||||||
|
@IsString()
|
||||||
|
prefix: string;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The station's api key.
|
* The station's api key.
|
||||||
* This is used to authorize a station against the api (not implemented yet).
|
* This is used to authorize a station against the api (not implemented yet).
|
||||||
@ -49,12 +56,12 @@ export class ScanStation {
|
|||||||
key: string;
|
key: string;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Is the station enabled (for fraud and setup reasons)?
|
* The client's api key in plain text.
|
||||||
* Default: true
|
* This will only be used to display the full key on creation and updates.
|
||||||
*/
|
*/
|
||||||
@Column()
|
@IsString()
|
||||||
@IsBoolean()
|
@IsOptional()
|
||||||
enabled: boolean = true;
|
cleartextkey?: string;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Used to link track scans to a scan station.
|
* Used to link track scans to a scan station.
|
||||||
|
Loading…
x
Reference in New Issue
Block a user