feat(scanner): ean-barcode mit der handykamera scannen
Knopf im Katalog neben "Set anlegen": Strichcode ins Bild halten, und entweder oeffnet sich das passende Set oder das Formular "Neues Set" mit ausgefuellter EAN. Dekodiert wird im Browser mit zxing, das nur beim Oeffnen des Scanners nachgeladen wird - nichts verlaesst das Geraet, und die Katalogseite bleibt leicht fuer alle, die nie scannen. Jeder gelesene Code wird gegen seine Pruefziffer geprueft, bevor er verwendet wird. Ohne das wuerde eine falsch erkannte Ziffer stillschweigend ein zweites Katalog-Set fuer ein Set anlegen, das es schon gibt; bei einem Fehlversuch scannt die Kamera einfach weiter. Die Kamera braucht wie Passkeys einen sicheren Kontext. Fehlt HTTPS oder wird der Zugriff abgelehnt, steht das als deutscher Hinweis im Dialog statt einer stummen schwarzen Flaeche. 8 Unit-Tests mit echten EAN-13-, EAN-8- und UPC-A-Beispielen. Refs Phase 2 Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,41 @@
|
||||
/**
|
||||
* Barcode helpers.
|
||||
*
|
||||
* Kept free of server-only imports so both the scanner in the browser and the
|
||||
* lookup on the server use the very same rules.
|
||||
*/
|
||||
|
||||
/** Digits only - scanners and humans disagree about spaces and dashes. */
|
||||
export function normalizeEan(input: string): string {
|
||||
return input.replace(/\D/g, '');
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks an EAN-8, EAN-13 or UPC-A against its check digit.
|
||||
*
|
||||
* Worth doing: a camera occasionally reads a digit wrong, and without this a
|
||||
* misread would silently create a second catalog entry for a set that is
|
||||
* already there.
|
||||
*/
|
||||
export function isValidEan(input: string): boolean {
|
||||
const digits = normalizeEan(input);
|
||||
if (![8, 12, 13].includes(digits.length)) return false;
|
||||
|
||||
// From the right: every second digit counts three times.
|
||||
const checkDigit = Number(digits[digits.length - 1]);
|
||||
const body = digits.slice(0, -1);
|
||||
|
||||
let sum = 0;
|
||||
for (let index = body.length - 1, weight = 3; index >= 0; index -= 1, weight = weight === 3 ? 1 : 3) {
|
||||
sum += Number(body[index]) * weight;
|
||||
}
|
||||
|
||||
return (10 - (sum % 10)) % 10 === checkDigit;
|
||||
}
|
||||
|
||||
/** "4005556603985" -> "4 005556 603985" for readable display. */
|
||||
export function formatEan(input: string): string {
|
||||
const digits = normalizeEan(input);
|
||||
if (digits.length !== 13) return digits;
|
||||
return `${digits.slice(0, 1)} ${digits.slice(1, 7)} ${digits.slice(7)}`;
|
||||
}
|
||||
@@ -160,6 +160,24 @@ export function getSetById(id: string) {
|
||||
return row ?? null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Lookup by barcode. EANs are stored as typed, so the comparison strips
|
||||
* everything but digits on both sides - a scanner never produces the spaces
|
||||
* and dashes people write by hand.
|
||||
*/
|
||||
export function findSetByEan(ean: string) {
|
||||
const digits = ean.replace(/\D/g, '');
|
||||
if (digits.length === 0) return null;
|
||||
|
||||
return (
|
||||
db
|
||||
.select({ id: set.id, setNumber: set.setNumber, name: set.name })
|
||||
.from(set)
|
||||
.where(sql`replace(replace(coalesce(${set.ean}, ''), '-', ''), ' ', '') = ${digits}`)
|
||||
.get() ?? null
|
||||
);
|
||||
}
|
||||
|
||||
export function getSetByNumber(manufacturerId: string, setNumber: string) {
|
||||
return (
|
||||
db
|
||||
|
||||
Reference in New Issue
Block a user