Enhance CategoryController and CategoryService to enforce admin-only access for category management; introduce restaurant-specific handling by adding RestId parameter to relevant methods; implement new endpoint for retrieving categories by restaurant for user access.

This commit is contained in:
2025-11-18 23:44:28 +03:30
parent b9d0904577
commit fdaa8b3dfd
3 changed files with 54 additions and 18 deletions
@@ -47,8 +47,8 @@ export class CategoryService {
})) as unknown as Category[];
}
async findOne(id: string): Promise<Category> {
const cat = await this.categoryRepository.findOne({ id }, { populate: ['foods'] });
async findOne(restId: string, id: string): Promise<Category> {
const cat = await this.categoryRepository.findOne({ id, restId }, { populate: ['foods'] });
if (!cat) throw new NotFoundException(CategoryMessage.NOT_FOUND);
return {
@@ -63,16 +63,31 @@ export class CategoryService {
} as unknown as Category;
}
async update(id: string, dto: UpdateCategoryDto): Promise<Category> {
const cat = await this.categoryRepository.findOne({ id });
async findRestaurantCategories(restId: string): Promise<Category> {
const cat = await this.categoryRepository.findOne({ restId }, { populate: ['foods'] });
if (!cat) throw new NotFoundException(CategoryMessage.NOT_FOUND);
this.em.assign(cat, dto as Partial<Category>);
return {
id: cat.id,
title: cat.title,
isActive: cat.isActive,
restId: cat.restId,
avatarUrl: cat.avatarUrl,
createdAt: cat.createdAt,
updatedAt: cat.updatedAt,
} as unknown as Category;
}
async update(restId: string, id: string, dto: UpdateCategoryDto): Promise<Category> {
const cat = await this.categoryRepository.findOne({ id, restId });
if (!cat) throw new NotFoundException(CategoryMessage.NOT_FOUND);
this.em.assign(cat, dto);
await this.em.persistAndFlush(cat);
return cat;
}
async remove(id: string): Promise<void> {
const cat = await this.categoryRepository.findOne({ id });
async remove(restId: string, id: string): Promise<void> {
const cat = await this.categoryRepository.findOne({ id, restId });
if (!cat) throw new NotFoundException(CategoryMessage.NOT_FOUND);
cat.deletedAt = new Date();
await this.em.persistAndFlush(cat);