Extend PATCH /category/:id to also move (parentId) alongside rename.
PATCH now accepts name (rename, re-slugify) and/or parentId (move under a new parent, cycle/self guarded), merging both results. Renames the PATCH docs file to category/update.md and regenerates API docs.
This commit is contained in:
@@ -252,11 +252,15 @@ export function register(app, basePath = '') {
|
||||
* /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).
|
||||
* summary: Update category (rename or move)
|
||||
* description: |
|
||||
* Updates a category the user is authorized for.
|
||||
* `name` renames the category (re-slugifies in the company default language).
|
||||
* `parentId` moves it under a new parent (cycle/self → `Invalid parent`).
|
||||
* Either `name` or `parentId` (or both) is required.
|
||||
* security:
|
||||
* - cookieAuth: []
|
||||
* x-markdown: category/rename.md
|
||||
* x-markdown: category/update.md
|
||||
* parameters:
|
||||
* - in: path
|
||||
* name: id
|
||||
@@ -270,15 +274,17 @@ export function register(app, basePath = '') {
|
||||
* application/json:
|
||||
* schema:
|
||||
* type: object
|
||||
* required: [name]
|
||||
* properties:
|
||||
* name:
|
||||
* type: string
|
||||
* parentId:
|
||||
* type: integer
|
||||
* description: New parent category id
|
||||
* example:
|
||||
* name: Renamed category
|
||||
* parentId: 3
|
||||
* responses:
|
||||
* 200:
|
||||
* description: Renamed category
|
||||
* description: Updated category (renamed and/or moved)
|
||||
* content:
|
||||
* application/json:
|
||||
* schema:
|
||||
@@ -286,7 +292,7 @@ export function register(app, basePath = '') {
|
||||
* properties:
|
||||
* category:
|
||||
* type: object
|
||||
* required: [id, name, slug]
|
||||
* required: [id, name, slug, parentId, sort]
|
||||
* properties:
|
||||
* id:
|
||||
* type: integer
|
||||
@@ -294,8 +300,12 @@ export function register(app, basePath = '') {
|
||||
* type: string
|
||||
* slug:
|
||||
* type: string
|
||||
* parentId:
|
||||
* type: integer
|
||||
* sort:
|
||||
* type: integer
|
||||
* 400:
|
||||
* description: Validation error (name required)
|
||||
* description: Validation error (name or parentId required, or invalid parent)
|
||||
* content:
|
||||
* application/json:
|
||||
* schema:
|
||||
@@ -379,21 +389,46 @@ export function register(app, basePath = '') {
|
||||
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' });
|
||||
const body = req.body ?? {};
|
||||
const hasName = Object.prototype.hasOwnProperty.call(body, 'name');
|
||||
const hasParentId = Object.prototype.hasOwnProperty.call(body, 'parentId');
|
||||
|
||||
if (!hasName && !hasParentId) {
|
||||
return res.status(400).json({ error: 'name or parentId is required' });
|
||||
}
|
||||
|
||||
try {
|
||||
const category = await db.rename({
|
||||
googleAuthId: req.user.id,
|
||||
id: parsed.id,
|
||||
name,
|
||||
});
|
||||
let category;
|
||||
if (hasParentId) {
|
||||
const parentId = Number(body.parentId);
|
||||
if (!Number.isInteger(parentId) || parentId <= 0) {
|
||||
return res.status(400).json({ error: 'parentId is required' });
|
||||
}
|
||||
category = await db.move({ googleAuthId: req.user.id, id: parsed.id, parentId });
|
||||
}
|
||||
|
||||
if (hasName) {
|
||||
const name = typeof body.name === 'string' ? body.name.trim() : '';
|
||||
if (!name) {
|
||||
return res.status(400).json({ error: 'name is required' });
|
||||
}
|
||||
if (category) {
|
||||
// Merge: preserve the parentId/sort from the move, apply the new name/slug.
|
||||
const renamed = await db.rename({
|
||||
googleAuthId: req.user.id,
|
||||
id: parsed.id,
|
||||
name,
|
||||
});
|
||||
category = { ...category, ...renamed };
|
||||
} else {
|
||||
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);
|
||||
if (status === 500) console.error('Update category failed:', error);
|
||||
res.status(status).json({ error: error.message });
|
||||
}
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user