This commit is contained in:
seb
2026-10-31 22:14:05 +01:00
parent 0480a5dcbe
commit 26adfc5aa9

View File

@@ -260,6 +260,85 @@ export function register(app, basePath = '') {
// #endregion // #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 * @openapi
* /printerjob/{id}/cancel: * /printerjob/{id}/cancel: