This commit is contained in:
seb
2026-10-16 22:09:15 +02:00
parent 188cd71f63
commit d7f7d669f9
13 changed files with 446 additions and 171 deletions

View File

@@ -168,10 +168,12 @@ function deliveryErrorStatus(error) {
if (error.message === 'Attachment not found') return 404;
if (error.message === 'File not found') return 404;
if (error.message === 'Supplier not found') return 404;
if (error.message === 'Warehouse not found') return 404;
if (error.message === 'Invalid supplierId') return 400;
if (error.message === 'Article not found') return 404;
if (error.message === 'Invalid delivery') return 400;
if (error.message === 'Invalid lines') return 400;
if (error.message === 'Delivery has no lines') return 400;
if (error.message === 'Invalid file') return 400;
if (error.message === 'Invalid JSON in "data" field') return 400;
if (error.message === 'Only PDF or text files are accepted as attachments') return 400;
@@ -204,13 +206,46 @@ export function register(app, basePath = '') {
* summary: List deliveries
* description: |
* Lists goods receipts (`manifest` rows with `type = 'delivery'`)
* for the authenticated user's selected company.
* for the authenticated user's selected company. Supports pagination and filtering.
* security:
* - cookieAuth: []
* x-markdown: delivery/list.md
* parameters:
* - name: offset
* in: query
* schema:
* type: integer
* default: 0
* description: Number of records to skip
* - name: limit
* in: query
* schema:
* type: integer
* default: 50
* description: Number of records to return (1-1000)
* - name: search
* in: query
* schema:
* type: string
* description: Substring match for delivery number, external number, or supplier name
* - name: onlyUnbooked
* in: query
* schema:
* type: boolean
* description: When true, returns only deliveries with unbooked stock items
* - name: supplierId
* in: query
* schema:
* type: integer
* description: Filter by supplier entity id
* - name: seekMonths
* in: query
* schema:
* type: integer
* description: Negative integer cutoff months ago (e.g. -1)
* responses:
* 200:
* description: Delivery list
* description: Delivery list (windowed when pagination/filters are used)
* content:
* application/json:
* schema:
@@ -220,6 +255,14 @@ export function register(app, basePath = '') {
* type: array
* items:
* $ref: '#/components/schemas/DeliverySummary'
* total:
* type: integer
* unfilteredTotal:
* type: integer
* offset:
* type: integer
* limit:
* type: integer
* 400:
* description: No company selected
* content:
@@ -358,9 +401,67 @@ export function register(app, basePath = '') {
return res.status(400).json({ error: selected.error });
}
const rawOffset = req.query.offset;
const rawLimit = req.query.limit;
let offset = 0;
let limit = 50;
if (rawOffset !== undefined) {
offset = parseInt(rawOffset, 10);
if (!Number.isInteger(offset) || offset < 0) {
return res.status(400).json({ error: 'offset must be an integer >= 0' });
}
}
if (rawLimit !== undefined) {
limit = parseInt(rawLimit, 10);
if (!Number.isInteger(limit) || limit < 1 || limit > 1000) {
return res.status(400).json({ error: 'limit must be an integer between 1 and 1000' });
}
}
const search = typeof req.query.search === 'string' && req.query.search.trim() !== ''
? req.query.search.trim()
: undefined;
const onlyUnbooked = req.query.onlyUnbooked === 'true';
let supplierId = undefined;
if (req.query.supplierId !== undefined && req.query.supplierId !== '') {
const parsedSupplier = parseInt(req.query.supplierId, 10);
if (!Number.isInteger(parsedSupplier) || parsedSupplier <= 0) {
return res.status(400).json({ error: 'supplierId must be a positive integer' });
}
supplierId = parsedSupplier;
}
let seekMonths = undefined;
if (req.query.seekMonths !== undefined && req.query.seekMonths !== '') {
const parsedSeek = parseInt(req.query.seekMonths, 10);
if (!Number.isInteger(parsedSeek)) {
return res.status(400).json({ error: 'seekMonths must be an integer' });
}
seekMonths = parsedSeek;
}
const isPaged = rawOffset !== undefined || rawLimit !== undefined || search !== undefined || onlyUnbooked || supplierId !== undefined || seekMonths !== undefined;
try {
const deliveries = await db.list(req.user.id);
res.status(200).json({ deliveries });
if (isPaged) {
const result = await db.list(req.user.id, {
offset,
limit,
search,
onlyUnbooked: onlyUnbooked ? true : undefined,
supplierId,
seekMonths,
});
res.status(200).json(result);
} else {
const deliveries = await db.list(req.user.id);
res.status(200).json({ deliveries: Array.isArray(deliveries) ? deliveries : (deliveries.deliveries || []) });
}
} catch (error) {
res.locals.error = error;
const status = deliveryErrorStatus(error);
@@ -369,6 +470,21 @@ export function register(app, basePath = '') {
}
});
app.post(`${basePath}/delivery/rebuild-summaries`, async (req, res) => {
const selected = requireSelectedCompany(req);
if (selected.error) {
return res.status(400).json({ error: selected.error });
}
try {
const result = await db.rebuildSummaries(req.user.id);
res.status(200).json(result);
} catch (error) {
const status = deliveryErrorStatus(error);
if (status === 500) console.error('Rebuild delivery summaries failed:', error);
res.status(status).json({ error: status === 500 ? 'Internal server error' : error.message });
}
});
// #endregion
// #region POST /delivery
@@ -749,6 +865,8 @@ export function register(app, basePath = '') {
// #endregion
/**
* @openapi
* /delivery/{id}/file/{fileId}: