This commit is contained in:
seb
2026-07-31 17:52:04 +02:00
parent 18ee68312f
commit 63df454861
5 changed files with 157 additions and 7 deletions

View File

@@ -24,18 +24,17 @@ function categoryErrorStatus(error) {
export function register(app, basePath = '') {
/**
* @route GET /category/tree
* @description Returns the category tree for a company the user belongs to.
* @query {number} companyId
* @description Returns the category tree for the authenticated user's selected company.
* @returns {Object} 200 - `{ languageId, tree, articles }`
*/
app.get(`${basePath}/category/tree`, async (req, res) => {
const parsed = parsePositiveInt(req.query.companyId, 'companyId');
if (parsed.error) {
return res.status(400).json({ error: parsed.error });
const companyId = req.user.selectedCompanyId;
if (companyId == null) {
return res.status(400).json({ error: 'No company selected' });
}
try {
const result = await db.getTreeForCompany(req.user.id, parsed.id);
const result = await db.getTreeForCompany(req.user.id, companyId);
res.status(200).json(result);
} catch (error) {
const status = categoryErrorStatus(error);
@@ -76,6 +75,27 @@ export function register(app, basePath = '') {
}
});
/**
* @route GET /category/:id/articles
* @description Returns articles in a category the user is authorized for.
* @returns {Object} 200 - `{ articles }`
*/
app.get(`${basePath}/category/:id/articles`, async (req, res) => {
const parsed = parsePositiveInt(req.params.id, 'id');
if (parsed.error) {
return res.status(400).json({ error: parsed.error });
}
try {
const articles = await db.getCategoryArticles(req.user.id, parsed.id);
res.status(200).json({ articles });
} catch (error) {
const status = categoryErrorStatus(error);
if (status === 500) console.error('Get category articles failed:', error);
res.status(status).json({ error: error.message });
}
});
/**
* @route DELETE /category/:id
* @description Deletes a category the user is authorized for (not the company root).