45
src/models/actions/CreateRunnerCard.ts
Normal file
45
src/models/actions/CreateRunnerCard.ts
Normal file
@@ -0,0 +1,45 @@
|
||||
import { IsBoolean, IsInt, IsOptional } from 'class-validator';
|
||||
import { getConnection } from 'typeorm';
|
||||
import { RunnerNotFoundError } from '../../errors/RunnerErrors';
|
||||
import { Runner } from '../entities/Runner';
|
||||
import { RunnerCard } from '../entities/RunnerCard';
|
||||
|
||||
/**
|
||||
* This classed is used to create a new RunnerCard entity from a json body (post request).
|
||||
*/
|
||||
export class CreateRunnerCard {
|
||||
/**
|
||||
* The card's associated runner.
|
||||
*/
|
||||
@IsInt()
|
||||
@IsOptional()
|
||||
runner?: number;
|
||||
|
||||
/**
|
||||
* Is the new card enabled (for fraud reasons)?
|
||||
* Default: true
|
||||
*/
|
||||
@IsBoolean()
|
||||
enabled: boolean = true;
|
||||
|
||||
/**
|
||||
* Creates a new RunnerCard entity from this.
|
||||
*/
|
||||
public async toEntity(): Promise<RunnerCard> {
|
||||
let newCard: RunnerCard = new RunnerCard();
|
||||
|
||||
newCard.enabled = this.enabled;
|
||||
newCard.runner = await this.getRunner();
|
||||
|
||||
return newCard;
|
||||
}
|
||||
|
||||
public async getRunner(): Promise<Runner> {
|
||||
if (!this.runner) { return null; }
|
||||
const runner = await getConnection().getRepository(Runner).findOne({ id: this.runner });
|
||||
if (!runner) {
|
||||
throw new RunnerNotFoundError();
|
||||
}
|
||||
return runner;
|
||||
}
|
||||
}
|
||||
@@ -1,10 +1,9 @@
|
||||
import {
|
||||
IsBoolean,
|
||||
IsEAN,
|
||||
|
||||
IsInt,
|
||||
IsNotEmpty,
|
||||
IsOptional,
|
||||
IsString
|
||||
|
||||
IsOptional
|
||||
} from "class-validator";
|
||||
import { Column, Entity, ManyToOne, OneToMany, PrimaryGeneratedColumn } from "typeorm";
|
||||
import { ResponseRunnerCard } from '../responses/ResponseRunnerCard';
|
||||
@@ -33,17 +32,6 @@ export class RunnerCard {
|
||||
@ManyToOne(() => Runner, runner => runner.cards, { nullable: true })
|
||||
runner: Runner;
|
||||
|
||||
/**
|
||||
* The card's code.
|
||||
* This has to be able to being converted to something barcode compatible.
|
||||
* Will get automaticlly generated (not implemented yet).
|
||||
*/
|
||||
@Column()
|
||||
@IsEAN()
|
||||
@IsString()
|
||||
@IsNotEmpty()
|
||||
code: string;
|
||||
|
||||
/**
|
||||
* Is the card enabled (for fraud reasons)?
|
||||
* Default: true
|
||||
@@ -59,6 +47,14 @@ export class RunnerCard {
|
||||
@OneToMany(() => TrackScan, scan => scan.track, { nullable: true })
|
||||
scans: TrackScan[];
|
||||
|
||||
/**
|
||||
* Generates a ean-13 compliant string for barcode generation.
|
||||
*/
|
||||
public get code(): string {
|
||||
//TODO: Implement the real deal
|
||||
return '0000000000000'
|
||||
}
|
||||
|
||||
/**
|
||||
* Turns this entity into it's response class.
|
||||
*/
|
||||
|
||||
@@ -17,7 +17,7 @@ export class ResponseRunnerCard {
|
||||
* This is important to link scans to runners.
|
||||
*/
|
||||
@IsObject()
|
||||
runner: ResponseRunner;
|
||||
runner: ResponseRunner | null;
|
||||
|
||||
/**
|
||||
* The card's code.
|
||||
@@ -40,7 +40,8 @@ export class ResponseRunnerCard {
|
||||
*/
|
||||
public constructor(card: RunnerCard) {
|
||||
this.id = card.id;
|
||||
this.runner = card.runner.toResponse() || null;
|
||||
if (!card.runner) { this.runner = null }
|
||||
else { this.runner = card.runner.toResponse(); }
|
||||
this.code = card.code;
|
||||
this.enabled = card.enabled;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user