This commit is contained in:
seb
2026-11-01 05:01:21 +01:00
parent 8a5a307c50
commit e6c15dd053
2 changed files with 24 additions and 55 deletions

View File

@@ -1,9 +1,6 @@
import fs from 'fs/promises';
import path from 'path';
import express from 'express';
import { renderLabel } from 'react-canvas-label-editor';
import * as db from '../../../db/article.js';
import * as printerDb from '../../../db/printer.js';
import * as agentprinterDb from '../../../db/agentprinter.js';
import * as labeltemplateDb from '../../../db/labeltemplate.js';
import * as printerjobDb from '../../../db/printerjob.js';
@@ -330,11 +327,11 @@ export function register(app, basePath = '') {
* application/json:
* schema:
* type: object
* required: [templateId, printerId, quantity]
* required: [templateId, agentprinterId, quantity]
* properties:
* templateId:
* type: integer
* printerId:
* agentprinterId:
* type: integer
* quantity:
* type: integer
@@ -369,8 +366,7 @@ export function register(app, basePath = '') {
if (skuError) return res.status(400).json({ error: skuError });
const sku = parsedSku;
const rawPrinterId = req.body?.printerId ?? req.body?.agentprinterId;
const { templateId, quantity } = req.body ?? {};
const { templateId, agentprinterId, quantity } = req.body ?? {};
if (templateId === undefined || templateId === null) {
return res.status(400).json({ error: 'templateId is required' });
@@ -379,11 +375,11 @@ export function register(app, basePath = '') {
return res.status(400).json({ error: 'templateId must be an integer' });
}
if (rawPrinterId === undefined || rawPrinterId === null) {
return res.status(400).json({ error: 'printerId is required' });
if (agentprinterId === undefined || agentprinterId === null) {
return res.status(400).json({ error: 'agentprinterId is required' });
}
if (typeof rawPrinterId !== 'number' || !Number.isInteger(rawPrinterId) || rawPrinterId <= 0) {
return res.status(400).json({ error: 'printerId must be an integer' });
if (typeof agentprinterId !== 'number' || !Number.isInteger(agentprinterId) || agentprinterId <= 0) {
return res.status(400).json({ error: 'agentprinterId must be an integer' });
}
if (quantity === undefined || quantity === null) {
@@ -393,28 +389,16 @@ export function register(app, basePath = '') {
return res.status(400).json({ error: 'quantity must be an integer' });
}
const parsedPrinterId = { id: rawPrinterId };
const parsedTemplateId = { id: templateId };
const parsedQuantity = { id: quantity };
try {
let printer = await printerDb.getById(req.user.id, parsedPrinterId.id);
let agentPrinter = null;
if (!printer || printer.company_id !== companyId) {
agentPrinter = await agentprinterDb.getById(req.user.id, parsedPrinterId.id);
if (agentPrinter) {
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, agentprinterId);
if (!printer) {
return res.status(404).json({ error: 'Printer not found' });
}
if (printer.type && printer.type.toLowerCase() !== 'zpl') {
return res.status(400).json({ error: 'Printer is not a ZPL printer' });
}
const template = await labeltemplateDb.getById(req.user.id, parsedTemplateId.id);
const template = await labeltemplateDb.getById(req.user.id, templateId);
if (!template || template.company_id !== companyId) {
return res.status(404).json({ error: 'Label template not found' });
}
@@ -438,8 +422,8 @@ export function register(app, basePath = '') {
if (typeof content === 'string' && content.includes('^XA')) {
let interpolated = interpolateString(content, article);
if (parsedQuantity.id > 1) {
interpolated = applyZplCopies(interpolated, parsedQuantity.id);
if (quantity > 1) {
interpolated = applyZplCopies(interpolated, quantity);
}
zplData = interpolated;
} else {
@@ -466,34 +450,20 @@ export function register(app, basePath = '') {
objects: interpolatedObjects,
});
zplData = await pngToZpl(printPayload, parsedQuantity.id);
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');
res.status(200).json({ success: true });
return res.status(200).json({ success: true, printerjob: job });
} catch (error) {
console.error('Print article label failed:', error);
res.status(500).json({ error: error.message });

View File

@@ -130,8 +130,7 @@ export function register(app, basePath = '') {
return res.status(400).json({ error: selected.error });
}
const rawApId = req.body?.agentprinterId ?? req.body?.agentprinterid;
const parsedApId = parsePositiveInt(rawApId, 'agentprinterId');
const parsedApId = parsePositiveInt(req.body?.agentprinterId, 'agentprinterId');
if (parsedApId.error) {
return res.status(400).json({ error: parsedApId.error });
}
@@ -141,7 +140,7 @@ export function register(app, basePath = '') {
return res.status(400).json({ error: 'data is required' });
}
const rawCopies = req.body?.copiescount ?? req.body?.copiesCount ?? req.body?.copies;
const rawCopies = req.body?.copiescount;
const copies = rawCopies != null ? Number(rawCopies) : 1;
if (!Number.isInteger(copies) || copies <= 0) {
return res.status(400).json({ error: 'copiescount must be a positive integer' });