This commit is contained in:
seb
2026-10-18 03:15:23 +02:00
parent e085e064ee
commit 04943fb7a6
12 changed files with 1372 additions and 867 deletions

View File

@@ -257,7 +257,155 @@ export function register(app, basePath = '') {
/**
* @openapi
* /article/{sku}:
* /article/history:
* get:
* tags: [Article]
* summary: Query company-wide article history
* description: |
* Queries article change history across all articles for the authenticated user's selected company.
* Supports pagination, search filter, and date cutoff via `seekMonths`.
* security:
* - cookieAuth: []
* x-markdown: article/history-list.md
* parameters:
* - in: query
* name: offset
* schema:
* type: integer
* minimum: 0
* default: 0
* description: Paging offset
* - in: query
* name: limit
* schema:
* type: integer
* minimum: 1
* maximum: 1000
* default: 50
* description: Page size
* - in: query
* name: search
* schema:
* type: string
* description: Optional search filter across SKU, article name, and history text
* - in: query
* name: seekMonths
* schema:
* type: integer
* description: Negative integer cutoff months ago (e.g. -13)
* responses:
* 200:
* description: Paged article history list
* content:
* application/json:
* schema:
* type: object
* required: [history, total, unfilteredTotal, offset, limit]
* properties:
* history:
* type: array
* items:
* type: object
* required: [id, date, sku, historytext]
* properties:
* id:
* type: integer
* date:
* type: string
* format: date-time
* sku:
* type: string
* name:
* type: string
* nullable: true
* historytext:
* type: string
* total:
* type: integer
* unfilteredTotal:
* type: integer
* offset:
* type: integer
* limit:
* type: integer
* 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'
*/
// #region GET /article/history
app.get(`${basePath}/article/history`, async (req, res) => {
const companyId = req.user.selectedCompanyId;
if (companyId == null) {
return res.status(400).json({ error: 'No company selected' });
}
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;
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;
}
try {
const result = await db.listHistory(req.user.id, {
offset,
limit,
search,
seekMonths,
});
res.status(200).json(result);
} catch (error) {
const status = articleErrorStatus(error);
if (status === 500) console.error('List article history failed:', error);
res.status(status).json({ error: error.message });
}
});
// #endregion
/**
* @openapi
* /article/{sku}:
* get:

View File

@@ -43,6 +43,9 @@ export function parseFullHash(value) {
export function articleErrorStatus(error) {
if (error.message === 'No company selected') return 400;
if (error.message === 'Invalid offset') return 400;
if (error.message === 'Invalid limit') return 400;
if (error.message === 'Invalid seekMonths') return 400;
if (error.message === 'Unsupported image format' || error.message?.includes('unsupported image format')) return 400;
if (error.message === 'Not authorized') return 403;
if (error.message === 'Parent not found') return 404;