444 lines
15 KiB
JavaScript
444 lines
15 KiB
JavaScript
import * as db from '../../db/ledger.js';
|
|
|
|
// #region Helpers
|
|
|
|
function requireSelectedCompany(req) {
|
|
const companyId = req.user.selectedCompanyId;
|
|
if (companyId == null) {
|
|
return { error: 'No company selected' };
|
|
}
|
|
return { companyId };
|
|
}
|
|
|
|
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;
|
|
if (error.message === 'Invalid ledger entry') return 400;
|
|
if (error.message === 'Invalid manifest article id') return 400;
|
|
if (error.message === 'Invalid SKU') return 400;
|
|
if (error.message?.includes('Stock tracking is disabled')) return 409;
|
|
return 500;
|
|
}
|
|
|
|
// #endregion
|
|
|
|
/**
|
|
* Registers ledger routes for the authenticated user's selected company.
|
|
* @param {import('express').Application} app
|
|
* @param {string} [basePath='']
|
|
*/
|
|
export function register(app, basePath = '') {
|
|
|
|
/**
|
|
* @openapi
|
|
* /ledger/manifestarticle/{manifestarticleId}:
|
|
* get:
|
|
* tags: [Ledger]
|
|
* summary: Query ledger by manifest article ID
|
|
* description: Lists ledger entries for a specific manifest article line, optionally filtered by transaction type.
|
|
* security:
|
|
* - cookieAuth: []
|
|
* parameters:
|
|
* - in: path
|
|
* name: manifestarticleId
|
|
* required: true
|
|
* schema:
|
|
* type: integer
|
|
* description: Manifest article line ID
|
|
* - in: query
|
|
* name: transaction_type
|
|
* schema:
|
|
* type: string
|
|
* description: Optional transaction type filter
|
|
* responses:
|
|
* 200:
|
|
* description: List of ledger entries
|
|
* content:
|
|
* application/json:
|
|
* schema:
|
|
* type: object
|
|
* properties:
|
|
* ledger:
|
|
* type: array
|
|
* items:
|
|
* $ref: '#/components/schemas/LedgerEntry'
|
|
* 400:
|
|
* description: Invalid manifest article 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: Manifest article or company not found
|
|
* content:
|
|
* application/json:
|
|
* schema:
|
|
* $ref: '#/components/schemas/Error'
|
|
*/
|
|
// #region GET /ledger/manifestarticle/:manifestarticleId
|
|
|
|
app.get(`${basePath}/ledger/manifestarticle/:manifestarticleId`, async (req, res) => {
|
|
const selected = requireSelectedCompany(req);
|
|
if (selected.error) {
|
|
return res.status(400).json({ error: selected.error });
|
|
}
|
|
|
|
const manifestarticleId = Number(req.params.manifestarticleId);
|
|
const transactionType = req.query.transaction_type ?? req.query.transactionType;
|
|
|
|
try {
|
|
const ledger = await db.listByManifestarticleId(
|
|
req.user.id,
|
|
manifestarticleId,
|
|
transactionType,
|
|
);
|
|
res.status(200).json({ ledger });
|
|
} catch (error) {
|
|
const status = ledgerErrorStatus(error);
|
|
if (status === 500) console.error('Query ledger by manifestarticle failed:', error);
|
|
res.status(status).json({ error: error.message });
|
|
}
|
|
});
|
|
|
|
// #endregion
|
|
|
|
/**
|
|
* @openapi
|
|
* /ledger/sku/{sku}:
|
|
* get:
|
|
* tags: [Ledger]
|
|
* summary: Query ledger by article SKU
|
|
* description: Lists ledger entries for an article SKU (joined from manifestarticle), optionally filtered by transaction type.
|
|
* security:
|
|
* - cookieAuth: []
|
|
* parameters:
|
|
* - in: path
|
|
* name: sku
|
|
* required: true
|
|
* schema:
|
|
* type: string
|
|
* description: Article SKU
|
|
* - in: query
|
|
* name: transaction_type
|
|
* schema:
|
|
* type: string
|
|
* description: Optional transaction type filter
|
|
* responses:
|
|
* 200:
|
|
* description: List of ledger entries
|
|
* content:
|
|
* application/json:
|
|
* schema:
|
|
* type: object
|
|
* properties:
|
|
* ledger:
|
|
* type: array
|
|
* items:
|
|
* $ref: '#/components/schemas/LedgerEntry'
|
|
* 400:
|
|
* description: Invalid SKU 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: Company not found
|
|
* content:
|
|
* application/json:
|
|
* schema:
|
|
* $ref: '#/components/schemas/Error'
|
|
*/
|
|
// #region GET /ledger/sku/:sku
|
|
|
|
app.get(`${basePath}/ledger/sku/:sku`, async (req, res) => {
|
|
const selected = requireSelectedCompany(req);
|
|
if (selected.error) {
|
|
return res.status(400).json({ error: selected.error });
|
|
}
|
|
|
|
const sku = req.params.sku;
|
|
const transactionType = req.query.transaction_type ?? req.query.transactionType;
|
|
|
|
try {
|
|
const ledger = await db.listBySku(req.user.id, sku, transactionType);
|
|
res.status(200).json({ ledger });
|
|
} catch (error) {
|
|
const status = ledgerErrorStatus(error);
|
|
if (status === 500) console.error('Query ledger by sku failed:', error);
|
|
res.status(status).json({ error: error.message });
|
|
}
|
|
});
|
|
|
|
// #endregion
|
|
|
|
/**
|
|
* @openapi
|
|
* /ledger:
|
|
* get:
|
|
* tags: [Ledger]
|
|
* summary: Query ledger by manifest article ID or SKU
|
|
* description: |
|
|
* Alternative query endpoint supporting `?manifestarticle_id=...` or `?sku=...`,
|
|
* with optional `?transaction_type=...`.
|
|
* security:
|
|
* - cookieAuth: []
|
|
* parameters:
|
|
* - in: query
|
|
* name: manifestarticle_id
|
|
* schema:
|
|
* type: integer
|
|
* description: Query entries for this manifest article line ID
|
|
* - in: query
|
|
* name: sku
|
|
* schema:
|
|
* type: string
|
|
* description: Query entries for this article SKU
|
|
* - in: query
|
|
* name: transaction_type
|
|
* schema:
|
|
* type: string
|
|
* description: Optional transaction type filter
|
|
* responses:
|
|
* 200:
|
|
* description: List of ledger entries
|
|
* content:
|
|
* application/json:
|
|
* schema:
|
|
* type: object
|
|
* properties:
|
|
* ledger:
|
|
* type: array
|
|
* items:
|
|
* $ref: '#/components/schemas/LedgerEntry'
|
|
* 400:
|
|
* description: Missing required query parameter or no company selected
|
|
* content:
|
|
* application/json:
|
|
* schema:
|
|
* $ref: '#/components/schemas/Error'
|
|
*/
|
|
// #region GET /ledger
|
|
|
|
app.get(`${basePath}/ledger`, async (req, res) => {
|
|
const selected = requireSelectedCompany(req);
|
|
if (selected.error) {
|
|
return res.status(400).json({ error: selected.error });
|
|
}
|
|
|
|
const rawManifestarticleId = req.query.manifestarticle_id ?? req.query.manifestarticleId;
|
|
const sku = req.query.sku;
|
|
const transactionType = req.query.transaction_type ?? req.query.transactionType;
|
|
|
|
if (rawManifestarticleId == null && (sku == null || !String(sku).trim())) {
|
|
return res.status(400).json({
|
|
error: 'Either manifestarticle_id or sku query parameter is required',
|
|
});
|
|
}
|
|
|
|
try {
|
|
let ledger;
|
|
if (rawManifestarticleId != null) {
|
|
ledger = await db.listByManifestarticleId(
|
|
req.user.id,
|
|
Number(rawManifestarticleId),
|
|
transactionType,
|
|
);
|
|
} else {
|
|
ledger = await db.listBySku(req.user.id, String(sku).trim(), transactionType);
|
|
}
|
|
res.status(200).json({ ledger });
|
|
} catch (error) {
|
|
const status = ledgerErrorStatus(error);
|
|
if (status === 500) console.error('Query ledger failed:', error);
|
|
res.status(status).json({ error: error.message });
|
|
}
|
|
});
|
|
|
|
// #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
|
|
}
|
|
|