From 689f12ba69399c01487a926014990955d7166ee5 Mon Sep 17 00:00:00 2001 From: seb Date: Sun, 1 Nov 2026 05:57:35 +0100 Subject: [PATCH] u --- src/api/routes/article/media.js | 15 ++------ src/api/routes/article/shared.js | 11 +++++- src/api/routes/labeltemplate.js | 66 ++++++++++++++++++++++++++++++-- 3 files changed, 76 insertions(+), 16 deletions(-) diff --git a/src/api/routes/article/media.js b/src/api/routes/article/media.js index e4aa795..52d82cb 100644 --- a/src/api/routes/article/media.js +++ b/src/api/routes/article/media.js @@ -14,6 +14,7 @@ import { parseFullHash, articleErrorStatus, interpolateString, + interpolateObjects, } from './shared.js'; /** @@ -366,7 +367,8 @@ export function register(app, basePath = '') { if (skuError) return res.status(400).json({ error: skuError }); const sku = parsedSku; - const { templateId, agentprinterId, quantity } = req.body ?? {}; + const { templateId, quantity } = req.body ?? {}; + const agentprinterId = req.body?.agentprinterId ?? req.body?.printerId; if (templateId === undefined || templateId === null) { return res.status(400).json({ error: 'templateId is required' }); @@ -433,16 +435,7 @@ export function register(app, basePath = '') { ? content : []; - const interpolatedObjects = objects.map(obj => { - const cloned = { ...obj }; - if (typeof cloned.text === 'string') { - cloned.text = interpolateString(cloned.text, article); - } - if (typeof cloned.code === 'string') { - cloned.code = interpolateString(cloned.code, article); - } - return cloned; - }); + const interpolatedObjects = interpolateObjects(objects, article); const printPayload = await renderLabel({ width: template.width, diff --git a/src/api/routes/article/shared.js b/src/api/routes/article/shared.js index c8a0cd5..426ad78 100644 --- a/src/api/routes/article/shared.js +++ b/src/api/routes/article/shared.js @@ -149,7 +149,8 @@ export function interpolateString(str, article) { export function interpolateObjects(objects, article) { if (!Array.isArray(objects)) return []; - const ean = Array.isArray(article?.eans) && article.eans.length > 0 ? String(article.eans[0]) : ''; + const ean = Array.isArray(article?.eans) && article.eans.length > 0 ? String(article.eans[0]).trim() : ''; + const sku = String(article?.sku ?? '').trim(); return objects.map((obj) => { const cloned = { ...obj }; @@ -161,6 +162,14 @@ export function interpolateObjects(objects, article) { if (rawCode === '0' || /^0+$/.test(rawCode)) { if (ean) { cloned.code = ean; + if (ean.length === 8) { + cloned.format = 'EAN8'; + } else if (ean.length === 12 || ean.length === 13) { + cloned.format = 'EAN13'; + } + } else if (sku) { + cloned.code = sku; + cloned.format = 'CODE128'; } } else if (typeof cloned.code === 'string') { cloned.code = interpolateString(cloned.code, article); diff --git a/src/api/routes/labeltemplate.js b/src/api/routes/labeltemplate.js index f2babe8..48cf175 100644 --- a/src/api/routes/labeltemplate.js +++ b/src/api/routes/labeltemplate.js @@ -96,12 +96,45 @@ export function register(app, basePath = '') { try { const templates = await db.list(req.user.id); - res.status(200).json({ - labeltemplates: templates.map((t) => { + + let article = null; + if (req.query?.sku) { + const skuStr = String(req.query.sku).trim(); + const result = await articleDb.getBySku(req.user.id, skuStr); + if (result?.article) { + article = result.article; + } + } + + const labeltemplates = await Promise.all( + templates.map(async (t) => { const { content, ...rest } = serializeTemplate(t); + if (article) { + try { + const full = await db.getById(req.user.id, t.id); + const objects = Array.isArray(full?.content?.objects) + ? full.content.objects + : Array.isArray(full?.content) + ? full.content + : null; + if (objects && objects.length > 0) { + const interpolated = interpolateObjects(objects, article); + const thumbBuffer = await renderThumb({ + width: t.width, + height: t.height, + objects: interpolated, + }); + rest.thumb = thumbBuffer.toString('base64'); + } + } catch (err) { + console.error(`Failed to render preview thumb for template ${t.id} with sku ${req.query.sku}:`, err); + } + } return rest; }) - }); + ); + + res.status(200).json({ labeltemplates }); } catch (error) { const status = labeltemplateErrorStatus(error); if (status === 500) console.error('List label templates failed:', error); @@ -175,7 +208,32 @@ export function register(app, basePath = '') { if (!template) { return res.status(404).json({ error: 'Label template not found' }); } - res.status(200).json({ labeltemplate: serializeTemplate(template) }); + const serialized = serializeTemplate(template); + if (req.query?.sku) { + const skuStr = String(req.query.sku).trim(); + const result = await articleDb.getBySku(req.user.id, skuStr); + if (result?.article) { + const objects = Array.isArray(serialized?.content?.objects) + ? serialized.content.objects + : Array.isArray(serialized?.content) + ? serialized.content + : null; + if (objects && objects.length > 0) { + try { + const interpolated = interpolateObjects(objects, result.article); + const thumbBuffer = await renderThumb({ + width: template.width, + height: template.height, + objects: interpolated, + }); + serialized.thumb = thumbBuffer.toString('base64'); + } catch (err) { + console.error(`Failed to render preview thumb for template ${template.id} with sku ${req.query.sku}:`, err); + } + } + } + } + res.status(200).json({ labeltemplate: serialized }); } catch (error) { const status = labeltemplateErrorStatus(error); if (status === 500) console.error('Get label template failed:', error);