u
This commit is contained in:
@@ -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 });
|
||||
|
||||
Reference in New Issue
Block a user