From 43d7049a7be1fe940844b82e294b8b2c254884d5 Mon Sep 17 00:00:00 2001 From: seb Date: Sun, 1 Nov 2026 04:50:07 +0100 Subject: [PATCH] u --- src/api/routes/labeltemplate.js | 63 ++++++--------------------------- 1 file changed, 11 insertions(+), 52 deletions(-) diff --git a/src/api/routes/labeltemplate.js b/src/api/routes/labeltemplate.js index 7dde68a..2fff20c 100644 --- a/src/api/routes/labeltemplate.js +++ b/src/api/routes/labeltemplate.js @@ -1,8 +1,5 @@ -import fs from 'fs/promises'; -import path from 'path'; import { renderLabel, validateObjects } from 'react-canvas-label-editor'; import * as db from '../../db/labeltemplate.js'; -import * as printerDb from '../../db/printer.js'; import * as agentprinterDb from '../../db/agentprinter.js'; import * as printerjobDb from '../../db/printerjob.js'; import * as articleDb from '../../db/article.js'; @@ -562,33 +559,9 @@ export function register(app, basePath = '') { } try { - let agentPrinter = null; - let printer = null; - - if (req.body?.agentprinterId != null) { - const parsedApId = parsePositiveInt(req.body.agentprinterId, 'agentprinterId'); - if (!parsedApId.error) { - agentPrinter = await agentprinterDb.getById(req.user.id, parsedApId.id); - if (agentPrinter && agentPrinter.company_id === companyId) { - printer = agentPrinter; - } else { - agentPrinter = null; - } - } - } - - if (!agentPrinter) { - printer = await printerDb.getById(req.user.id, parsedPrinterId.id); - if (!printer || printer.company_id !== companyId) { - agentPrinter = await agentprinterDb.getById(req.user.id, parsedPrinterId.id); - if (agentPrinter && agentPrinter.company_id === companyId) { - printer = agentPrinter; - } else { - return res.status(404).json({ error: 'Printer not found' }); - } - } else if (printer.printeragentId != null) { - agentPrinter = printer; - } + const printer = await agentprinterDb.getById(req.user.id, parsedPrinterId.id); + if (!printer) { + return res.status(404).json({ error: 'Printer not found' }); } if (printer.type && printer.type.toLowerCase() !== 'zpl') { @@ -673,31 +646,17 @@ export function register(app, basePath = '') { zplData = await pngToZpl(printPayload, quantity); } - if (agentPrinter) { - const job = await printerjobDb.create(req.user.id, { - agentprinterId: agentPrinter.id, - type: 'zpl', - data: zplData, - }); + const job = await printerjobDb.create(req.user.id, { + agentprinterId: printer.id, + type: 'zpl', + data: zplData, + }); - if (job.printeragentId) { - notifyAgentJob(job.printeragentId, 'created', job); - } - - return res.status(200).json({ success: true, printerjob: job }); + if (job.printeragentId) { + notifyAgentJob(job.printeragentId, 'created', job); } - // Fallback for legacy network printers with static IP / address - const spoolKey = printer.address ? `${printer.address}_${printer.type}` : `printer_${printer.id}_${printer.type || 'zpl'}`; - const printDir = path.join(process.cwd(), 'print_spool', spoolKey); - await fs.mkdir(printDir, { recursive: true }); - - const timestamp = Date.now(); - const filename = `${timestamp}.zpl`; - const filePath = path.join(printDir, filename); - await fs.writeFile(filePath, zplData, 'utf8'); - - return res.status(200).json({ success: true }); + return res.status(200).json({ success: true, printerjob: job }); } catch (error) { console.error('Testprint label template failed:', error); return res.status(500).json({ error: error.message });