This commit is contained in:
seb
2026-11-01 00:20:26 +01:00
parent 26adfc5aa9
commit ce7f5aad2a

View File

@@ -4,6 +4,7 @@ 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 printerDb from '../../../db/printer.js';
import * as agentprinterDb from '../../../db/agentprinter.js';
import * as labeltemplateDb from '../../../db/labeltemplate.js'; import * as labeltemplateDb from '../../../db/labeltemplate.js';
import { import {
AVIF_BODY_LIMIT, AVIF_BODY_LIMIT,
@@ -364,7 +365,8 @@ 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 { templateId, printerId, quantity } = req.body ?? {}; const rawPrinterId = req.body?.printerId ?? req.body?.agentprinterId;
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' });
@@ -373,10 +375,10 @@ 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 (printerId === undefined || printerId === null) { if (rawPrinterId === undefined || rawPrinterId === null) {
return res.status(400).json({ error: 'printerId is required' }); return res.status(400).json({ error: 'printerId is required' });
} }
if (typeof printerId !== 'number' || !Number.isInteger(printerId) || printerId <= 0) { if (typeof rawPrinterId !== 'number' || !Number.isInteger(rawPrinterId) || rawPrinterId <= 0) {
return res.status(400).json({ error: 'printerId must be an integer' }); return res.status(400).json({ error: 'printerId must be an integer' });
} }
@@ -387,16 +389,21 @@ 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: printerId }; const parsedPrinterId = { id: rawPrinterId };
const parsedTemplateId = { id: templateId }; const parsedTemplateId = { id: templateId };
const parsedQuantity = { id: quantity }; const parsedQuantity = { id: quantity };
try { try {
const printer = await printerDb.getById(req.user.id, parsedPrinterId.id); let printer = await printerDb.getById(req.user.id, parsedPrinterId.id);
if (!printer || printer.company_id !== companyId) { if (!printer || printer.company_id !== companyId) {
const agentPrinter = await agentprinterDb.getById(req.user.id, parsedPrinterId.id);
if (agentPrinter) {
printer = agentPrinter;
} else {
return res.status(404).json({ error: 'Printer not found' }); return res.status(404).json({ error: 'Printer not found' });
} }
if (printer.type !== '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' });
} }
@@ -444,7 +451,8 @@ export function register(app, basePath = '') {
}); });
// Mock network dispatch // Mock network dispatch
const printDir = path.join(process.cwd(), 'print_spool', `${printer.address}_${printer.type}`); const spoolKey = printer.address ? `${printer.address}_${printer.type}` : `agent_${printer.id}_${printer.type || 'zpl'}`;
const printDir = path.join(process.cwd(), 'print_spool', spoolKey);
await fs.mkdir(printDir, { recursive: true }); await fs.mkdir(printDir, { recursive: true });
for (let i = 0; i < parsedQuantity.id; i++) { for (let i = 0; i < parsedQuantity.id; i++) {