parent
df39166279
commit
860680d001
|
@ -25,7 +25,7 @@ export class RunnerCardIdsNotMatchingError extends NotAcceptableError {
|
|||
}
|
||||
|
||||
/**
|
||||
* Error to throw when a station still has scans associated.
|
||||
* Error to throw when a card still has scans associated.
|
||||
*/
|
||||
export class RunnerCardHasScansError extends NotAcceptableError {
|
||||
@IsString()
|
||||
|
@ -34,3 +34,15 @@ export class RunnerCardHasScansError extends NotAcceptableError {
|
|||
@IsString()
|
||||
message = "This card still has scans associated with it. \n If you want to delete this card with all it's scans add `?force` to your query. \n Otherwise please consider just diableing it."
|
||||
}
|
||||
|
||||
/**
|
||||
* Error to throw when a card's id is too big to generate a ean-13 barcode for it.
|
||||
* This error should never reach a enduser.
|
||||
*/
|
||||
export class RunnerCardIdOutOfRangeError extends Error {
|
||||
@IsString()
|
||||
name = "RunnerCardIdOutOfRangeError"
|
||||
|
||||
@IsString()
|
||||
message = "The card's id is too big to fit into a ean-13 barcode. \n This has a very low probability of happening but means that you might want to switch your barcode format for something that can accept numbers over 9999999999."
|
||||
}
|
|
@ -6,6 +6,7 @@ import {
|
|||
IsOptional
|
||||
} from "class-validator";
|
||||
import { Column, Entity, ManyToOne, OneToMany, PrimaryGeneratedColumn } from "typeorm";
|
||||
import { RunnerCardIdOutOfRangeError } from '../../errors/RunnerCardErrors';
|
||||
import { ResponseRunnerCard } from '../responses/ResponseRunnerCard';
|
||||
import { Runner } from "./Runner";
|
||||
import { TrackScan } from "./TrackScan";
|
||||
|
@ -51,8 +52,27 @@ export class RunnerCard {
|
|||
* Generates a ean-13 compliant string for barcode generation.
|
||||
*/
|
||||
public get code(): string {
|
||||
//TODO: Implement the real deal
|
||||
return '0000000000000'
|
||||
const multiply = [1, 3];
|
||||
let total = 0;
|
||||
this.paddedId.split('').forEach((letter, index) => {
|
||||
total += parseInt(letter, 10) * multiply[index % 2];
|
||||
});
|
||||
const checkSum = (Math.ceil(total / 10) * 10) - total;
|
||||
return this.paddedId + checkSum.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns this card's id as a string padded to the length of 12 characters with leading zeros.
|
||||
*/
|
||||
private get paddedId(): string {
|
||||
let id: string = this.id.toString();
|
||||
|
||||
if (id.length > 12) {
|
||||
throw new RunnerCardIdOutOfRangeError();
|
||||
}
|
||||
while (id.length < 12) { id = '0' + id; }
|
||||
|
||||
return id;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -42,7 +42,12 @@ export class ResponseRunnerCard {
|
|||
this.id = card.id;
|
||||
if (!card.runner) { this.runner = null }
|
||||
else { this.runner = card.runner.toResponse(); }
|
||||
this.code = card.code;
|
||||
try {
|
||||
this.code = card.code;
|
||||
} catch (error) {
|
||||
this.code = "0000000000000"
|
||||
}
|
||||
|
||||
this.enabled = card.enabled;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue