This commit is contained in:
seb
2026-11-01 04:50:07 +01:00
parent c60b899c4d
commit 43d7049a7b

View File

@@ -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,34 +559,10 @@ 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 {
const printer = await agentprinterDb.getById(req.user.id, parsedPrinterId.id);
if (!printer) {
return res.status(404).json({ error: 'Printer not found' });
}
} else if (printer.printeragentId != null) {
agentPrinter = printer;
}
}
if (printer.type && printer.type.toLowerCase() !== 'zpl') {
return res.status(400).json({ error: 'Printer is not a ZPL printer' });
@@ -673,9 +646,8 @@ export function register(app, basePath = '') {
zplData = await pngToZpl(printPayload, quantity);
}
if (agentPrinter) {
const job = await printerjobDb.create(req.user.id, {
agentprinterId: agentPrinter.id,
agentprinterId: printer.id,
type: 'zpl',
data: zplData,
});
@@ -685,19 +657,6 @@ export function register(app, basePath = '') {
}
return res.status(200).json({ success: true, printerjob: 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 });
} catch (error) {
console.error('Testprint label template failed:', error);
return res.status(500).json({ error: error.message });