Files
dashback/src/api/routes/shipping.js
2026-10-09 10:41:21 +02:00

249 lines
8.0 KiB
JavaScript

import * as db from '../../db/shipping.js';
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 shippingErrorStatus(error) {
if (error.message === 'No company selected') return 400;
if (error.message === 'Not authorized') return 403;
if (error.message?.endsWith(' not found')) return 404;
if (error.message?.endsWith(' required')) return 400;
return 500;
}
/**
* Registers shipping routes (shippings belong to an order / manifest).
* @param {import('express').Application} app
* @param {string} [basePath='']
*/
export function register(app, basePath = '') {
/**
* @openapi
* /order/{id}/shipping:
* get:
* tags: [Shipping]
* summary: List shippings of an order
* description: Lists shippings of the order (manifest) for the selected company.
* security:
* - cookieAuth: []
* parameters:
* - in: path
* name: id
* required: true
* schema:
* type: integer
* description: Order ID
* responses:
* 200:
* description: List of shippings
* content:
* application/json:
* schema:
* type: object
* properties:
* shippings:
* type: array
* items:
* $ref: '#/components/schemas/Shipping'
* 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'
* 404:
* description: Order not found
* content:
* application/json:
* schema:
* $ref: '#/components/schemas/Error'
*/
app.get(`${basePath}/order/:id/shipping`, 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 shippings = await db.listByManifestId(req.user.id, parsedId.id);
res.status(200).json({ shippings });
} catch (error) {
const status = shippingErrorStatus(error);
if (status === 500) console.error('List shippings failed:', error);
res.status(status).json({ error: error.message });
}
});
/**
* @openapi
* /order/{id}/shipping:
* post:
* tags: [Shipping]
* summary: Create shipping for an order
* description: |
* Creates a shipping for the order. The shipping number is allocated from the
* `shipping` number sequence (prefix and counter from the numbers API).
* security:
* - cookieAuth: []
* parameters:
* - in: path
* name: id
* required: true
* schema:
* type: integer
* description: Order ID
* requestBody:
* required: true
* content:
* application/json:
* schema:
* type: object
* required: [type, state]
* properties:
* type:
* type: string
* state:
* type: string
* responses:
* 201:
* description: Created shipping
* content:
* application/json:
* schema:
* type: object
* properties:
* shipping:
* $ref: '#/components/schemas/Shipping'
* 400:
* description: Validation error 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: Order not found
* content:
* application/json:
* schema:
* $ref: '#/components/schemas/Error'
*/
app.post(`${basePath}/order/:id/shipping`, 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 shipping = await db.create(req.user.id, {
...(req.body ?? {}),
manifestId: parsedId.id,
});
res.status(201).json({ shipping });
} catch (error) {
const status = shippingErrorStatus(error);
if (status === 500) console.error('Create shipping failed:', error);
res.status(status).json({ error: error.message });
}
});
/**
* @openapi
* /shipping/{id}:
* delete:
* tags: [Shipping]
* summary: Delete shipping
* description: Deletes a shipping of the selected company. Its packages are removed as well.
* security:
* - cookieAuth: []
* parameters:
* - in: path
* name: id
* required: true
* schema:
* type: integer
* description: Shipping ID
* responses:
* 204:
* description: Shipping 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'
*/
app.delete(`${basePath}/shipping/: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);
res.status(204).end();
} catch (error) {
const status = shippingErrorStatus(error);
if (status === 500) console.error('Delete shipping failed:', error);
res.status(status).json({ error: error.message });
}
});
}