66aeab282c
Build and Push Multi-Platform Images / build-and-push (push) Successful in 19s
This reverts commit 07dfd7e840.
70 lines
1.9 KiB
TypeScript
70 lines
1.9 KiB
TypeScript
import type { BarcodeTemplate } from '../database/entities/barcode-template.entity';
|
|
import type { InboxDocument } from '../database/entities/inbox-document.entity';
|
|
|
|
export interface ResolverContext {
|
|
doc: InboxDocument;
|
|
template: BarcodeTemplate;
|
|
matchingQrValue: string | null;
|
|
now?: Date;
|
|
}
|
|
|
|
function pad(n: number): string {
|
|
return n < 10 ? `0${n}` : String(n);
|
|
}
|
|
|
|
function dateVars(d: Date): Record<string, string> {
|
|
const yyyy = String(d.getFullYear());
|
|
const mm = pad(d.getMonth() + 1);
|
|
const dd = pad(d.getDate());
|
|
return {
|
|
datum: `${yyyy}-${mm}-${dd}`,
|
|
jahr: yyyy,
|
|
monat: mm,
|
|
tag: dd,
|
|
zeitstempel: d.toISOString(),
|
|
};
|
|
}
|
|
|
|
function sanitizeKey(name: string): string {
|
|
return name.replace(/[^A-Za-z0-9_]/g, '_');
|
|
}
|
|
|
|
/**
|
|
* Berechnet alle Platzhalter für eine Aktion einer bestimmten Barcode-Vorlage:
|
|
* - Datums-Variablen: {datum}, {jahr}, {monat}, {tag}, {zeitstempel}
|
|
* - {barcode} = gesamter Barcode-Wert
|
|
* - {barcode.<gruppe>} = Named Capture Group aus der Vorlage-Regex
|
|
*/
|
|
export function buildVariables(ctx: ResolverContext): Record<string, string> {
|
|
const vars: Record<string, string> = {};
|
|
Object.assign(vars, dateVars(ctx.now ?? new Date()));
|
|
|
|
if (ctx.matchingQrValue !== null) {
|
|
vars['barcode'] = ctx.matchingQrValue;
|
|
try {
|
|
const m = ctx.matchingQrValue.match(new RegExp(ctx.template.Regex));
|
|
if (m?.groups) {
|
|
for (const [g, gv] of Object.entries(m.groups)) {
|
|
if (gv !== undefined) vars[`barcode.${sanitizeKey(g)}`] = gv;
|
|
}
|
|
}
|
|
} catch {}
|
|
}
|
|
|
|
return vars;
|
|
}
|
|
|
|
/**
|
|
* Ersetzt Platzhalter der Form `{name}` und `{name.group}` im Template.
|
|
* Unbekannte Platzhalter bleiben unverändert.
|
|
*/
|
|
export function applyTemplate(
|
|
template: string,
|
|
vars: Record<string, string>,
|
|
): string {
|
|
if (!template) return template;
|
|
return template.replace(/\{([A-Za-z0-9_.]+)\}/g, (full, name: string) => {
|
|
return name in vars ? vars[name] : full;
|
|
});
|
|
}
|