feat(F-120): completed feature
This commit is contained in:
@@ -155,9 +155,7 @@ export async function registerCategoriesRoutes(
|
||||
if (result === 'not_found') {
|
||||
throw new AppError(404, 'NOT_FOUND', 'Category not found');
|
||||
}
|
||||
if (result === 'has_children') {
|
||||
throw new AppError(409, 'CATEGORY_HAS_CHILDREN', 'Category has child categories');
|
||||
}
|
||||
// Children and product-category links cascade at the DB level (F-120).
|
||||
return reply.code(204).send();
|
||||
});
|
||||
|
||||
|
||||
@@ -75,19 +75,22 @@ export class UpdateCategory {
|
||||
}
|
||||
}
|
||||
|
||||
export type DeleteCategoryResult = 'deleted' | 'not_found' | 'has_children';
|
||||
export type DeleteCategoryResult = 'deleted' | 'not_found';
|
||||
|
||||
export class DeleteCategory {
|
||||
constructor(private readonly categories: CategoryRepository) {}
|
||||
|
||||
/**
|
||||
* Deletes the category and lets the database cascade to children and
|
||||
* product-category links (F-120). Returns:
|
||||
* - 'not_found' if the id does not exist;
|
||||
* - 'deleted' on success.
|
||||
*/
|
||||
async execute(id: string): Promise<DeleteCategoryResult> {
|
||||
const category = await this.categories.findById(id);
|
||||
if (!category) {
|
||||
return 'not_found';
|
||||
}
|
||||
if (await this.categories.hasChildren(id)) {
|
||||
return 'has_children';
|
||||
}
|
||||
return (await this.categories.delete(id)) ? 'deleted' : 'not_found';
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user