Files
ApiCore/ApiCore-Backend/src/label-templates/label-text.ts
T
Nick SzaunigandClaude Opus 5.5 c18860c75c
Backend Image / build-and-push (push) Successful in 6m34s
Frontend Image / build-and-push (push) Successful in 10m17s
feat: T6 wave 2 – medicine book, document rights, label particulars, herd events
- Veterinary medicine catalogue, lots and append-only movement ledger
  (rest always computed; corrections as counter-bookings; inactive
  medicines take no new stock; unit must match on lot moves).
- Documents of medicine lots, authority notices and restrictions need the
  domain right on top of documents:* (403 before any lookup).
- Label particulars: pure checkLabel with six mandatory gaps and four
  warnings, AFM font metrics, Berlin-day dates, geometric reading order
  for split addresses, new master-data fields with row-locked updates.
- Herd events ColonyCreated, ColonyMoved, ApiaryChanged and
  TskReferenceDayReached (ids only, never forwarded to webhooks); CSV
  import emits ColonyCreated with audit.
- ARCHITECTURE.md §12 Compliance (T6), Annahmen 59–70.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
2026-10-15 13:43:44 +02:00

66 lines
2.0 KiB
TypeScript

import { LabelTextElement, PlaceholderKey } from './label-element.types';
/**
* Text as the label check compares it: NFKC, lower case, every character that
* is not a letter or digit becomes a space, runs of spaces collapse. So
* "Bühlweg 10, 86450 Altenmünster" also matches "Bühlweg 10 · 86450\nAltenmünster".
*/
export function normalizeForCheck(text: string): string {
return (
text
.normalize('NFKC')
.toLowerCase()
.replace(/[^\p{L}\p{N}]+/gu, ' ')
// "250g" -> "250 g": a number glued to its unit
.replace(/(\p{N})(?=\p{L})/gu, '$1 ')
.trim()
);
}
// Elements whose tops are at most this far apart count as one row.
export const ROW_TOLERANCE_MM = 1;
export interface ResolvedText {
element: LabelTextElement;
text: string;
}
/**
* The text each element prints for one bottling (placeholders filled in),
* top first, then left.
*/
export function resolvedTexts(
elements: readonly LabelTextElement[],
values: Record<PlaceholderKey, string>,
): ResolvedText[] {
const resolved = elements
.map((element) => ({
element,
text: element.placeholderKey
? values[element.placeholderKey]
: element.text,
}))
.sort((a, b) => a.element.y - b.element.y);
// Reading order comes from the geometry only, never from the order of the
// elements array: rows (tops within the tolerance of the row's first top),
// each row from left to right.
const rows: ResolvedText[][] = [];
let rowTop = Number.NEGATIVE_INFINITY;
for (const entry of resolved) {
if (entry.element.y - rowTop > ROW_TOLERANCE_MM) {
rows.push([]);
rowTop = entry.element.y;
}
rows[rows.length - 1].push(entry);
}
return rows.flatMap((row) => row.sort((a, b) => a.element.x - b.element.x));
}
/**
* Does the lot number already start with the lot marker "L" (as its own token
* or glued to digits, "L-001", "L2026")? Then no further "L" is needed.
*/
export function lotStartsWithL(lotNumber: string): boolean {
return /^l( |\d|$)/.test(normalizeForCheck(lotNumber));
}