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