568 lines
18 KiB
JavaScript
568 lines
18 KiB
JavaScript
import * as db from '../../db/printer.js';
|
|
import { emit } from '../sse.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 printerErrorStatus(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 === 'Printer not found') return 404;
|
|
if (error.message === 'Address required') return 400;
|
|
if (error.message?.startsWith('Invalid printer type')) return 400;
|
|
if (error.message === 'Invalid sort') return 400;
|
|
if (error.message === 'Invalid printer order') return 400;
|
|
return 500;
|
|
}
|
|
|
|
// #endregion
|
|
|
|
/**
|
|
* Registers printer routes for the authenticated user's selected company.
|
|
* @param {import('express').Application} app
|
|
* @param {string} [basePath='']
|
|
*/
|
|
export function register(app, basePath = '') {
|
|
/**
|
|
* @openapi
|
|
* /printer:
|
|
* get:
|
|
* tags: [Printer]
|
|
* summary: List printers
|
|
* description: Lists printers for the selected company, ordered by sort and id.
|
|
* security:
|
|
* - cookieAuth: []
|
|
* x-markdown: printer/list.md
|
|
* responses:
|
|
* 200:
|
|
* description: List of printers
|
|
* content:
|
|
* application/json:
|
|
* schema:
|
|
* type: object
|
|
* properties:
|
|
* printers:
|
|
* type: array
|
|
* items:
|
|
* $ref: '#/components/schemas/Printer'
|
|
* 400:
|
|
* description: No company selected
|
|
* 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'
|
|
*/
|
|
// #region GET /printer
|
|
|
|
app.get(`${basePath}/printer`, async (req, res) => {
|
|
const selected = requireSelectedCompany(req);
|
|
if (selected.error) {
|
|
return res.status(400).json({ error: selected.error });
|
|
}
|
|
|
|
try {
|
|
const printers = await db.list(req.user.id);
|
|
res.status(200).json({ printers });
|
|
} catch (error) {
|
|
const status = printerErrorStatus(error);
|
|
if (status === 500) console.error('List printers failed:', error);
|
|
res.status(status).json({ error: error.message });
|
|
}
|
|
});
|
|
|
|
// #endregion
|
|
|
|
/**
|
|
* @openapi
|
|
* /printer/order:
|
|
* put:
|
|
* tags: [Printer]
|
|
* summary: Reorder printers
|
|
* description: |
|
|
* Reorders printers for the selected company.
|
|
* `printerIds` must include all existing printer IDs exactly once.
|
|
* security:
|
|
* - cookieAuth: []
|
|
* x-signals: [printer]
|
|
* x-markdown: printer/order.md
|
|
* requestBody:
|
|
* required: true
|
|
* content:
|
|
* application/json:
|
|
* schema:
|
|
* type: object
|
|
* required: [printerIds]
|
|
* properties:
|
|
* printerIds:
|
|
* type: array
|
|
* items:
|
|
* type: integer
|
|
* responses:
|
|
* 200:
|
|
* description: Applied printer order
|
|
* content:
|
|
* application/json:
|
|
* schema:
|
|
* type: object
|
|
* properties:
|
|
* printerIds:
|
|
* type: array
|
|
* items:
|
|
* type: integer
|
|
* 400:
|
|
* description: Invalid printer order or no company selected
|
|
* 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'
|
|
*/
|
|
// #region PUT /printer/order
|
|
|
|
app.put(`${basePath}/printer/order`, async (req, res) => {
|
|
const selected = requireSelectedCompany(req);
|
|
if (selected.error) {
|
|
return res.status(400).json({ error: selected.error });
|
|
}
|
|
|
|
const { printerIds } = req.body ?? {};
|
|
|
|
try {
|
|
const result = await db.order(req.user.id, { printerIds });
|
|
emit('printer', { companyId: selected.companyId });
|
|
res.status(200).json(result);
|
|
} catch (error) {
|
|
const status = printerErrorStatus(error);
|
|
if (status === 500) console.error('Order printers failed:', error);
|
|
res.status(status).json({ error: error.message });
|
|
}
|
|
});
|
|
|
|
// #endregion
|
|
|
|
/**
|
|
* @openapi
|
|
* /printer/{id}:
|
|
* get:
|
|
* tags: [Printer]
|
|
* summary: Get printer by ID
|
|
* description: Retrieves a single printer for the authenticated user's selected company.
|
|
* security:
|
|
* - cookieAuth: []
|
|
* parameters:
|
|
* - in: path
|
|
* name: id
|
|
* required: true
|
|
* schema:
|
|
* type: integer
|
|
* description: Printer ID
|
|
* x-markdown: printer/get.md
|
|
* responses:
|
|
* 200:
|
|
* description: Printer details
|
|
* content:
|
|
* application/json:
|
|
* schema:
|
|
* type: object
|
|
* properties:
|
|
* printer:
|
|
* $ref: '#/components/schemas/Printer'
|
|
* 400:
|
|
* description: Invalid ID or no company selected
|
|
* 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: Printer not found
|
|
* content:
|
|
* application/json:
|
|
* schema:
|
|
* $ref: '#/components/schemas/Error'
|
|
*/
|
|
// #region GET /printer/:id
|
|
|
|
app.get(`${basePath}/printer/:id`, async (req, res) => {
|
|
const selected = requireSelectedCompany(req);
|
|
if (selected.error) {
|
|
return res.status(400).json({ error: selected.error });
|
|
}
|
|
|
|
const parsedId = parsePositiveInt(req.params.id, 'id');
|
|
if (parsedId.error) {
|
|
return res.status(400).json({ error: parsedId.error });
|
|
}
|
|
|
|
try {
|
|
const printer = await db.getById(req.user.id, parsedId.id);
|
|
if (!printer) {
|
|
return res.status(404).json({ error: 'Printer not found' });
|
|
}
|
|
res.status(200).json({ printer });
|
|
} catch (error) {
|
|
const status = printerErrorStatus(error);
|
|
if (status === 500) console.error('Get printer failed:', error);
|
|
res.status(status).json({ error: error.message });
|
|
}
|
|
});
|
|
|
|
// #endregion
|
|
|
|
/**
|
|
* @openapi
|
|
* /printer:
|
|
* post:
|
|
* tags: [Printer]
|
|
* summary: Create printer
|
|
* description: |
|
|
* Creates a new printer for the selected company.
|
|
* `type` must be `"esc/pos"` (lines), `"zpl"` (pages), or `"ipp"`.
|
|
* security:
|
|
* - cookieAuth: []
|
|
* x-signals: [printer]
|
|
* x-markdown: printer/create.md
|
|
* requestBody:
|
|
* required: true
|
|
* content:
|
|
* application/json:
|
|
* schema:
|
|
* $ref: '#/components/schemas/PrinterCreateInput'
|
|
* responses:
|
|
* 201:
|
|
* description: Created printer
|
|
* content:
|
|
* application/json:
|
|
* schema:
|
|
* type: object
|
|
* properties:
|
|
* printer:
|
|
* $ref: '#/components/schemas/Printer'
|
|
* 400:
|
|
* description: Invalid input or missing fields
|
|
* 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'
|
|
*/
|
|
// #region POST /printer
|
|
|
|
app.post(`${basePath}/printer`, async (req, res) => {
|
|
const selected = requireSelectedCompany(req);
|
|
if (selected.error) {
|
|
return res.status(400).json({ error: selected.error });
|
|
}
|
|
|
|
const { address, type, model, serial, sort, desc } = req.body ?? {};
|
|
|
|
try {
|
|
const printer = await db.create(req.user.id, {
|
|
address,
|
|
type,
|
|
model,
|
|
serial,
|
|
sort,
|
|
desc,
|
|
});
|
|
emit('printer', { companyId: selected.companyId });
|
|
res.status(201).json({ printer });
|
|
} catch (error) {
|
|
const status = printerErrorStatus(error);
|
|
if (status === 500) console.error('Create printer failed:', error);
|
|
res.status(status).json({ error: error.message });
|
|
}
|
|
});
|
|
|
|
// #endregion
|
|
|
|
async function handleUpdate(req, res) {
|
|
const selected = requireSelectedCompany(req);
|
|
if (selected.error) {
|
|
return res.status(400).json({ error: selected.error });
|
|
}
|
|
|
|
const parsedId = parsePositiveInt(req.params.id, 'id');
|
|
if (parsedId.error) {
|
|
return res.status(400).json({ error: parsedId.error });
|
|
}
|
|
|
|
const { address, type, model, serial, sort, desc } = req.body ?? {};
|
|
|
|
try {
|
|
const printer = await db.update(req.user.id, parsedId.id, {
|
|
address,
|
|
type,
|
|
model,
|
|
serial,
|
|
sort,
|
|
desc,
|
|
});
|
|
emit('printer', { companyId: selected.companyId });
|
|
res.status(200).json({ printer });
|
|
} catch (error) {
|
|
const status = printerErrorStatus(error);
|
|
if (status === 500) console.error('Update printer failed:', error);
|
|
res.status(status).json({ error: error.message });
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @openapi
|
|
* /printer/{id}:
|
|
* patch:
|
|
* tags: [Printer]
|
|
* summary: Update printer (partial)
|
|
* description: Updates specified fields of an existing printer for the selected company.
|
|
* security:
|
|
* - cookieAuth: []
|
|
* x-signals: [printer]
|
|
* parameters:
|
|
* - in: path
|
|
* name: id
|
|
* required: true
|
|
* schema:
|
|
* type: integer
|
|
* description: Printer ID
|
|
* x-markdown: printer/update.md
|
|
* requestBody:
|
|
* required: true
|
|
* content:
|
|
* application/json:
|
|
* schema:
|
|
* $ref: '#/components/schemas/PrinterUpdateInput'
|
|
* responses:
|
|
* 200:
|
|
* description: Updated printer
|
|
* content:
|
|
* application/json:
|
|
* schema:
|
|
* type: object
|
|
* properties:
|
|
* printer:
|
|
* $ref: '#/components/schemas/Printer'
|
|
* 400:
|
|
* description: 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: Printer not found
|
|
* content:
|
|
* application/json:
|
|
* schema:
|
|
* $ref: '#/components/schemas/Error'
|
|
* put:
|
|
* tags: [Printer]
|
|
* summary: Update printer
|
|
* description: Updates specified fields of an existing printer for the selected company.
|
|
* security:
|
|
* - cookieAuth: []
|
|
* x-signals: [printer]
|
|
* parameters:
|
|
* - in: path
|
|
* name: id
|
|
* required: true
|
|
* schema:
|
|
* type: integer
|
|
* description: Printer ID
|
|
* requestBody:
|
|
* required: true
|
|
* content:
|
|
* application/json:
|
|
* schema:
|
|
* $ref: '#/components/schemas/PrinterUpdateInput'
|
|
* responses:
|
|
* 200:
|
|
* description: Updated printer
|
|
* content:
|
|
* application/json:
|
|
* schema:
|
|
* type: object
|
|
* properties:
|
|
* printer:
|
|
* $ref: '#/components/schemas/Printer'
|
|
* 400:
|
|
* description: 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: Printer not found
|
|
* content:
|
|
* application/json:
|
|
* schema:
|
|
* $ref: '#/components/schemas/Error'
|
|
*/
|
|
// #region PATCH /printer/:id
|
|
|
|
app.patch(`${basePath}/printer/:id`, handleUpdate);
|
|
|
|
// #endregion
|
|
|
|
// #region PUT /printer/:id
|
|
|
|
app.put(`${basePath}/printer/:id`, handleUpdate);
|
|
|
|
// #endregion
|
|
|
|
/**
|
|
* @openapi
|
|
* /printer/{id}:
|
|
* delete:
|
|
* tags: [Printer]
|
|
* summary: Delete printer
|
|
* description: Deletes a printer for the authenticated user's selected company.
|
|
* security:
|
|
* - cookieAuth: []
|
|
* x-signals: [printer]
|
|
* parameters:
|
|
* - in: path
|
|
* name: id
|
|
* required: true
|
|
* schema:
|
|
* type: integer
|
|
* description: Printer ID
|
|
* x-markdown: printer/delete.md
|
|
* responses:
|
|
* 204:
|
|
* description: Printer deleted
|
|
* 400:
|
|
* description: Invalid ID or no company selected
|
|
* 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: Printer not found
|
|
* content:
|
|
* application/json:
|
|
* schema:
|
|
* $ref: '#/components/schemas/Error'
|
|
*/
|
|
// #region DELETE /printer/:id
|
|
|
|
app.delete(`${basePath}/printer/:id`, async (req, res) => {
|
|
const selected = requireSelectedCompany(req);
|
|
if (selected.error) {
|
|
return res.status(400).json({ error: selected.error });
|
|
}
|
|
|
|
const parsedId = parsePositiveInt(req.params.id, 'id');
|
|
if (parsedId.error) {
|
|
return res.status(400).json({ error: parsedId.error });
|
|
}
|
|
|
|
try {
|
|
await db.remove(req.user.id, parsedId.id);
|
|
emit('printer', { companyId: selected.companyId });
|
|
res.sendStatus(204);
|
|
} catch (error) {
|
|
const status = printerErrorStatus(error);
|
|
if (status === 500) console.error('Delete printer failed:', error);
|
|
res.status(status).json({ error: error.message });
|
|
}
|
|
});
|
|
|
|
// #endregion
|
|
}
|