This commit is contained in:
seb
2026-08-02 19:04:56 +02:00
parent bfb1d8762a
commit aed0857f23
21 changed files with 1953 additions and 0 deletions

View File

@@ -13,6 +13,8 @@ function categoryErrorStatus(error) {
if (error.message === 'Company not found') return 404;
if (error.message === 'Parent not found') return 404;
if (error.message === 'Cannot delete root category') return 400;
if (error.message === 'Name required') return 400;
if (error.message === 'Category not found') return 404;
return 500;
}
@@ -246,6 +248,74 @@ export function register(app, basePath = '') {
/**
* @openapi
* /category/{id}:
* patch:
* tags: [Category]
* summary: Rename category
* description: Renames a category the user is authorized for (updates name and re-slugifies in the company default language).
* security:
* - cookieAuth: []
* x-markdown: category/rename.md
* parameters:
* - in: path
* name: id
* required: true
* schema:
* type: integer
* description: Category id
* requestBody:
* required: true
* content:
* application/json:
* schema:
* type: object
* required: [name]
* properties:
* name:
* type: string
* example:
* name: Renamed category
* responses:
* 200:
* description: Renamed category
* content:
* application/json:
* schema:
* type: object
* properties:
* category:
* type: object
* required: [id, name, slug]
* properties:
* id:
* type: integer
* name:
* type: string
* slug:
* type: string
* 400:
* description: Validation error (name required)
* 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: Category not found
* content:
* application/json:
* schema:
* $ref: '#/components/schemas/Error'
* delete:
* tags: [Category]
* summary: Delete category
@@ -300,4 +370,29 @@ export function register(app, basePath = '') {
res.status(status).json({ error: error.message });
}
});
app.patch(`${basePath}/category/:id`, async (req, res) => {
const parsed = parsePositiveInt(req.params.id, 'id');
if (parsed.error) {
return res.status(400).json({ error: parsed.error });
}
const name = typeof req.body?.name === 'string' ? req.body.name.trim() : '';
if (!name) {
return res.status(400).json({ error: 'name is required' });
}
try {
const category = await db.rename({
googleAuthId: req.user.id,
id: parsed.id,
name,
});
res.status(200).json({ category });
} catch (error) {
const status = categoryErrorStatus(error);
if (status === 500) console.error('Rename category failed:', error);
res.status(status).json({ error: error.message });
}
});
}