From 26adfc5aa98345ff856f702ecac366e6262205dd Mon Sep 17 00:00:00 2001 From: seb Date: Sat, 31 Oct 2026 22:14:05 +0100 Subject: [PATCH] u --- src/api/routes/printerjob.js | 79 ++++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) diff --git a/src/api/routes/printerjob.js b/src/api/routes/printerjob.js index 845e987..7a7d1bf 100644 --- a/src/api/routes/printerjob.js +++ b/src/api/routes/printerjob.js @@ -260,6 +260,85 @@ export function register(app, basePath = '') { // #endregion + /** + * @openapi + * /printerjob/cancel-all: + * post: + * tags: [PrinterJob] + * summary: Cancel all pending and processing printer jobs + * description: | + * Cancels all queued or in-flight print jobs for the selected company, optionally filtered by agentprinterId. + * Notifies connected agents in real time. + * security: + * - cookieAuth: [] + * x-signals: [printerjob] + * requestBody: + * content: + * application/json: + * schema: + * type: object + * properties: + * agentprinterId: + * type: integer + * responses: + * 200: + * description: All matching jobs cancelled + * content: + * application/json: + * schema: + * type: object + * properties: + * success: + * type: boolean + * cancelledCount: + * type: integer + * jobs: + * type: array + * items: + * $ref: '#/components/schemas/Printerjob' + */ + // #region POST /printerjob/cancel-all + + app.post(`${basePath}/printerjob/cancel-all`, async (req, res) => { + const selected = requireSelectedCompany(req); + if (selected.error) { + return res.status(400).json({ error: selected.error }); + } + + try { + const rawApId = req.body?.agentprinterId ?? req.query?.agentprinterId; + const agentprinterId = rawApId != null ? Number(rawApId) : undefined; + const allJobs = await printerjobDb.list(req.user.id, { agentprinterId }); + const cancellable = allJobs.filter((j) => j.state === 'pending' || j.state === 'processing'); + + const cancelledJobs = []; + for (const j of cancellable) { + try { + const job = await printerjobDb.updateState(req.user.id, j.id, 'cancelled'); + if (job.printeragentId) { + notifyAgentJob(job.printeragentId, 'cancelled', job); + } + emit('printerjob', { companyId: selected.companyId, data: { job } }); + cancelledJobs.push(job); + } catch (e) { + console.error(`Failed to cancel printerjob #${j.id}:`, e); + } + } + + res.status(200).json({ + success: true, + cancelledCount: cancelledJobs.length, + jobs: cancelledJobs, + }); + } catch (error) { + const status = printerjobErrorStatus(error); + if (status === 500) console.error('Cancel all printerjobs failed:', error); + res.status(status).json({ error: error.message }); + } + }); + + // #endregion + /** * @openapi * /printerjob/{id}/cancel: