u
This commit is contained in:
@@ -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).
|
||||
|
||||
Reference in New Issue
Block a user