This commit is contained in:
seb
2026-10-30 16:48:39 +01:00
parent 02301ff172
commit 2df7c59feb
11 changed files with 844 additions and 213 deletions

View File

@@ -0,0 +1,346 @@
import { randomInt } from 'node:crypto';
import * as db from '../../db/printeragent.js';
// #region Helpers
function requireSelectedCompany(req) {
const companyId = req.user.selectedCompanyId;
if (companyId == null) {
return { error: 'No company selected' };
}
return { companyId };
}
function parsePositiveInt(value, field) {
const id = Number(value);
if (!Number.isInteger(id) || id <= 0) {
return { error: `${field} is required` };
}
return { id };
}
function printeragentErrorStatus(error) {
if (error.message === 'No company selected') return 400;
if (error.message === 'Not authorized') return 403;
if (error.message === 'Company not found') return 404;
if (error.message === 'Printeragent not found') return 404;
if (error.message === 'Invalid printeragent') return 400;
return 500;
}
// #endregion
/**
* Registers printeragent routes for the authenticated user's selected company.
* Management only (create / rename / delete); the agent's own register/operate endpoints are separate.
* @param {import('express').Application} app
* @param {string} [basePath='']
*/
export function register(app, basePath = '') {
/**
* @openapi
* /printeragent:
* get:
* tags: [Printeragent]
* summary: List printer agents
* description: Lists printer agents for the authenticated user's selected company.
* security:
* - cookieAuth: []
* responses:
* 200:
* description: Printer agent list
* content:
* application/json:
* schema:
* type: object
* properties:
* printeragents:
* type: array
* items:
* $ref: '#/components/schemas/Printeragent'
* 400:
* description: No company selected or invalid input
* content:
* application/json:
* schema:
* $ref: '#/components/schemas/Error'
* 401:
* description: Unauthorized
* content:
* application/json:
* schema:
* $ref: '#/components/schemas/Error'
* 403:
* description: Not authorized
* content:
* application/json:
* schema:
* $ref: '#/components/schemas/Error'
* 404:
* description: Company not found
* content:
* application/json:
* schema:
* $ref: '#/components/schemas/Error'
*/
// #region GET /printeragent
app.get(`${basePath}/printeragent`, async (req, res) => {
const selected = requireSelectedCompany(req);
if (selected.error) {
return res.status(400).json({ error: selected.error });
}
try {
const printeragents = await db.list(req.user.id);
res.status(200).json({ printeragents });
} catch (error) {
const status = printeragentErrorStatus(error);
if (status === 500) console.error('List printeragents failed:', error);
res.status(status).json({ error: error.message });
}
});
// #endregion
/**
* @openapi
* /printeragent:
* put:
* tags: [Printeragent]
* summary: Create printer agent
* description: |
* Creates an unpaired printer agent for the selected company.
* The pairingpin is randomly generated server-side and returned in the response.
* security:
* - cookieAuth: []
* requestBody:
* required: true
* content:
* application/json:
* schema:
* type: object
* required: [name]
* properties:
* name:
* type: string
* example:
* name: "Bar printer"
* responses:
* 200:
* description: Created printer agent with generated pairingpin
* content:
* application/json:
* schema:
* type: object
* properties:
* printeragent:
* $ref: '#/components/schemas/Printeragent'
* 400:
* description: No company selected or invalid input
* content:
* application/json:
* schema:
* $ref: '#/components/schemas/Error'
* 401:
* description: Unauthorized
* content:
* application/json:
* schema:
* $ref: '#/components/schemas/Error'
* 403:
* description: Not authorized
* content:
* application/json:
* schema:
* $ref: '#/components/schemas/Error'
* 404:
* description: Company not found
* content:
* application/json:
* schema:
* $ref: '#/components/schemas/Error'
*/
// #region PUT /printeragent
app.put(`${basePath}/printeragent`, async (req, res) => {
const selected = requireSelectedCompany(req);
if (selected.error) {
return res.status(400).json({ error: selected.error });
}
const { name } = req.body ?? {};
if (typeof name !== 'string' || !name.trim()) {
return res.status(400).json({ error: 'name is required' });
}
const pairingpin = String(randomInt(100000, 999999));
try {
const printeragent = await db.create(req.user.id, { name: name.trim(), pairingpin });
res.status(200).json({ printeragent });
} catch (error) {
const status = printeragentErrorStatus(error);
if (status === 500) console.error('Create printeragent failed:', error);
res.status(status).json({ error: error.message });
}
});
// #endregion
/**
* @openapi
* /printeragent/{id}:
* patch:
* tags: [Printeragent]
* summary: Rename printer agent
* security:
* - cookieAuth: []
* parameters:
* - in: path
* name: id
* required: true
* schema:
* type: integer
* requestBody:
* required: true
* content:
* application/json:
* schema:
* type: object
* required: [name]
* properties:
* name:
* type: string
* responses:
* 200:
* description: Updated printer agent
* content:
* application/json:
* schema:
* type: object
* properties:
* printeragent:
* $ref: '#/components/schemas/Printeragent'
* 400:
* description: No company selected or invalid input
* content:
* application/json:
* schema:
* $ref: '#/components/schemas/Error'
* 401:
* description: Unauthorized
* content:
* application/json:
* schema:
* $ref: '#/components/schemas/Error'
* 403:
* description: Not authorized
* content:
* application/json:
* schema:
* $ref: '#/components/schemas/Error'
* 404:
* description: Printeragent not found
* content:
* application/json:
* schema:
* $ref: '#/components/schemas/Error'
*/
// #region PATCH /printeragent/:id
app.patch(`${basePath}/printeragent/:id`, async (req, res) => {
const selected = requireSelectedCompany(req);
if (selected.error) {
return res.status(400).json({ error: selected.error });
}
const parsed = parsePositiveInt(req.params.id, 'id');
if (parsed.error) {
return res.status(400).json({ error: parsed.error });
}
const { name } = req.body ?? {};
if (typeof name !== 'string' || !name.trim()) {
return res.status(400).json({ error: 'name is required' });
}
try {
const printeragent = await db.update(req.user.id, { id: parsed.id, name: name.trim() });
res.status(200).json({ printeragent });
} catch (error) {
const status = printeragentErrorStatus(error);
if (status === 500) console.error('Update printeragent failed:', error);
res.status(status).json({ error: error.message });
}
});
// #endregion
/**
* @openapi
* /printeragent/{id}:
* delete:
* tags: [Printeragent]
* summary: Delete printer agent
* security:
* - cookieAuth: []
* parameters:
* - in: path
* name: id
* required: true
* schema:
* type: integer
* responses:
* 204:
* description: Deleted
* 400:
* description: No company selected or invalid id
* content:
* application/json:
* schema:
* $ref: '#/components/schemas/Error'
* 401:
* description: Unauthorized
* content:
* application/json:
* schema:
* $ref: '#/components/schemas/Error'
* 403:
* description: Not authorized
* content:
* application/json:
* schema:
* $ref: '#/components/schemas/Error'
* 404:
* description: Printeragent not found
* content:
* application/json:
* schema:
* $ref: '#/components/schemas/Error'
*/
// #region DELETE /printeragent/:id
app.delete(`${basePath}/printeragent/:id`, async (req, res) => {
const selected = requireSelectedCompany(req);
if (selected.error) {
return res.status(400).json({ error: selected.error });
}
const parsed = parsePositiveInt(req.params.id, 'id');
if (parsed.error) {
return res.status(400).json({ error: parsed.error });
}
try {
await db.remove(req.user.id, parsed.id);
res.sendStatus(204);
} catch (error) {
const status = printeragentErrorStatus(error);
if (status === 500) console.error('Delete printeragent failed:', error);
res.status(status).json({ error: error.message });
}
});
// #endregion
}