Implmented the EAN generation

ref #77
This commit is contained in:
2021-01-09 12:24:05 +01:00
parent df39166279
commit 860680d001
3 changed files with 41 additions and 4 deletions

View File

@@ -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;
}
/**