feat(F-120): completed feature

This commit is contained in:
chattie
2026-08-21 15:31:13 +02:00
parent ba05e99a63
commit b9610bf546
10 changed files with 258 additions and 31 deletions

View File

@@ -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();
});

View File

@@ -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';
}
}