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 { renderLabel, validateObjects } from 'react-canvas-label-editor';
|
||||||
import * as db from '../../db/labeltemplate.js';
|
import * as db from '../../db/labeltemplate.js';
|
||||||
import * as printerDb from '../../db/printer.js';
|
|
||||||
import * as agentprinterDb from '../../db/agentprinter.js';
|
import * as agentprinterDb from '../../db/agentprinter.js';
|
||||||
import * as printerjobDb from '../../db/printerjob.js';
|
import * as printerjobDb from '../../db/printerjob.js';
|
||||||
import * as articleDb from '../../db/article.js';
|
import * as articleDb from '../../db/article.js';
|
||||||
@@ -562,33 +559,9 @@ export function register(app, basePath = '') {
|
|||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
let agentPrinter = null;
|
const printer = await agentprinterDb.getById(req.user.id, parsedPrinterId.id);
|
||||||
let printer = null;
|
if (!printer) {
|
||||||
|
return res.status(404).json({ error: 'Printer not found' });
|
||||||
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;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (printer.type && printer.type.toLowerCase() !== 'zpl') {
|
if (printer.type && printer.type.toLowerCase() !== 'zpl') {
|
||||||
@@ -673,31 +646,17 @@ export function register(app, basePath = '') {
|
|||||||
zplData = await pngToZpl(printPayload, quantity);
|
zplData = await pngToZpl(printPayload, quantity);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (agentPrinter) {
|
const job = await printerjobDb.create(req.user.id, {
|
||||||
const job = await printerjobDb.create(req.user.id, {
|
agentprinterId: printer.id,
|
||||||
agentprinterId: agentPrinter.id,
|
type: 'zpl',
|
||||||
type: 'zpl',
|
data: zplData,
|
||||||
data: zplData,
|
});
|
||||||
});
|
|
||||||
|
|
||||||
if (job.printeragentId) {
|
if (job.printeragentId) {
|
||||||
notifyAgentJob(job.printeragentId, 'created', job);
|
notifyAgentJob(job.printeragentId, 'created', job);
|
||||||
}
|
|
||||||
|
|
||||||
return res.status(200).json({ success: true, printerjob: job });
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Fallback for legacy network printers with static IP / address
|
return res.status(200).json({ success: true, printerjob: job });
|
||||||
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) {
|
} catch (error) {
|
||||||
console.error('Testprint label template failed:', error);
|
console.error('Testprint label template failed:', error);
|
||||||
return res.status(500).json({ error: error.message });
|
return res.status(500).json({ error: error.message });
|
||||||
|
|||||||
Reference in New Issue
Block a user