From 860680d001191ac8ab4f5190618b4b0937915992 Mon Sep 17 00:00:00 2001 From: Nicolai Ort Date: Sat, 9 Jan 2021 12:24:05 +0100 Subject: [PATCH] Implmented the EAN generation ref #77 --- src/errors/RunnerCardErrors.ts | 14 ++++++++++++- src/models/entities/RunnerCard.ts | 24 ++++++++++++++++++++-- src/models/responses/ResponseRunnerCard.ts | 7 ++++++- 3 files changed, 41 insertions(+), 4 deletions(-) diff --git a/src/errors/RunnerCardErrors.ts b/src/errors/RunnerCardErrors.ts index 08edb44..63c3485 100644 --- a/src/errors/RunnerCardErrors.ts +++ b/src/errors/RunnerCardErrors.ts @@ -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." +} \ No newline at end of file diff --git a/src/models/entities/RunnerCard.ts b/src/models/entities/RunnerCard.ts index 0517008..a7bccc4 100644 --- a/src/models/entities/RunnerCard.ts +++ b/src/models/entities/RunnerCard.ts @@ -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; } /** diff --git a/src/models/responses/ResponseRunnerCard.ts b/src/models/responses/ResponseRunnerCard.ts index 97b9435..f895a9c 100644 --- a/src/models/responses/ResponseRunnerCard.ts +++ b/src/models/responses/ResponseRunnerCard.ts @@ -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; } }