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

@@ -12,6 +12,9 @@ function requireSelectedCompany(req) {
function ledgerErrorStatus(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 === 'Not authorized') return 403;
if (error.message === 'Company not found') return 404;
if (error.message === 'Manifest article not found') return 404;
@@ -281,4 +284,160 @@ export function register(app, basePath = '') {
});
// #endregion
/**
* @openapi
* /ledger/history:
* get:
* tags: [Ledger]
* summary: Query company-wide ledger history
* description: |
* Queries ledger entries across all articles for the authenticated user's selected company.
* Supports pagination, search filter, and date cutoff via `seekMonths`.
* security:
* - cookieAuth: []
* x-markdown: ledger/history.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, manifest number, external number, transaction type, and transaction text
* - in: query
* name: seekMonths
* schema:
* type: integer
* description: Negative integer cutoff months ago (e.g. -1)
* responses:
* 200:
* description: Paged ledger history list
* content:
* application/json:
* schema:
* type: object
* required: [ledger, total, unfilteredTotal, offset, limit]
* properties:
* ledger:
* type: array
* items:
* type: object
* required: [id, date, transaction_type, transaction_text]
* properties:
* id:
* type: integer
* date:
* type: string
* format: date-time
* sku:
* type: string
* nullable: true
* name:
* type: string
* nullable: true
* transaction_type:
* type: string
* transaction_text:
* type: string
* amount:
* type: number
* 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 /ledger/history
app.get(`${basePath}/ledger/history`, async (req, res) => {
const selected = requireSelectedCompany(req);
if (selected.error) {
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;
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 = ledgerErrorStatus(error);
if (status === 500) console.error('List ledger history failed:', error);
res.status(status).json({ error: error.message });
}
});
// #endregion
}