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
@@ -5,8 +5,8 @@ import { Restaurant } from 'src/modules/restaurants/entities/restaurant.entity';
import { Admin } from './admin.entity'; import { Admin } from './admin.entity';
@Entity({ tableName: 'admin_roles' }) @Entity({ tableName: 'admin_roles' })
@Unique({ properties: ['admin', 'role', 'restaurant'] })
export class AdminRole extends BaseEntity { export class AdminRole extends BaseEntity {
@Unique({ properties: ['admin.id', 'role.id', 'restaurant.id'] })
@ManyToOne(() => Admin) @ManyToOne(() => Admin)
admin!: Admin; admin!: Admin;
@@ -11,13 +11,19 @@ import {
ApiParam, ApiParam,
ApiBody, ApiBody,
ApiQuery, ApiQuery,
ApiBearerAuth,
} from '@nestjs/swagger'; } from '@nestjs/swagger';
import { AdminAuthGuard } from 'src/modules/auth/guards/adminAuth.guard';
import { UseGuards } from '@nestjs/common';
import { RestId } from 'src/common/decorators';
@ApiTags('categories') @ApiTags('categories')
@Controller('categories') @Controller()
export class CategoryController { export class CategoryController {
constructor(private readonly categoryService: CategoryService) {} constructor(private readonly categoryService: CategoryService) {}
@UseGuards(AdminAuthGuard)
@ApiBearerAuth('/admin/categories')
@Post() @Post()
@ApiOperation({ summary: 'Create category' }) @ApiOperation({ summary: 'Create category' })
@ApiCreatedResponse({ description: 'Category created', type: CreateCategoryDto }) @ApiCreatedResponse({ description: 'Category created', type: CreateCategoryDto })
@@ -26,40 +32,55 @@ export class CategoryController {
return this.categoryService.create(dto); return this.categoryService.create(dto);
} }
@UseGuards(AdminAuthGuard)
@ApiBearerAuth('/admin/categories')
@Get() @Get()
@ApiOperation({ summary: 'List categories' }) @ApiOperation({ summary: 'List categories for admin' })
@ApiOkResponse({ description: 'List of categories' }) @ApiOkResponse({ description: 'List of categories' })
@ApiQuery({ name: 'restId', required: false, type: String }) @ApiQuery({ name: 'restId', required: false, type: String })
@ApiQuery({ name: 'isActive', required: false, type: Boolean }) @ApiQuery({ name: 'isActive', required: false, type: Boolean })
findAll(@Query('restId') restId?: string, @Query('isActive') isActive?: string) { findAll(@RestId() restId: string, @Query('isActive') isActive?: string) {
// convert isActive from query string to boolean when provided // convert isActive from query string to boolean when provided
const isActiveBool: boolean | undefined = typeof isActive !== 'undefined' ? isActive === 'true' : undefined; const isActiveBool: boolean | undefined = typeof isActive !== 'undefined' ? isActive === 'true' : undefined;
return this.categoryService.findAll({ restId, isActive: isActiveBool }); return this.categoryService.findAll({ restId, isActive: isActiveBool });
} }
@UseGuards(AdminAuthGuard)
@ApiBearerAuth('/admin/categories')
@Get(':id') @Get(':id')
@ApiOperation({ summary: 'Get category' }) @ApiOperation({ summary: 'Get category' })
@ApiParam({ name: 'id' })
@ApiOkResponse({ description: 'Category detail', type: CreateCategoryDto }) @ApiOkResponse({ description: 'Category detail', type: CreateCategoryDto })
@ApiNotFoundResponse({ description: 'Category not found' }) @ApiNotFoundResponse({ description: 'Category not found' })
findOne(@Param('id') id: string) { findOne(@Param() id: string, @RestId() restId: string) {
return this.categoryService.findOne(id); return this.categoryService.findOne(restId, id);
} }
@UseGuards(AdminAuthGuard)
@ApiBearerAuth('/admin/categories')
@Patch(':id') @Patch(':id')
@ApiOperation({ summary: 'Update category' }) @ApiOperation({ summary: 'Update category' })
@ApiParam({ name: 'id' }) @ApiParam({ name: 'id' })
@ApiBody({ type: UpdateCategoryDto }) @ApiBody({ type: UpdateCategoryDto })
@ApiOkResponse({ description: 'Updated category', type: UpdateCategoryDto }) @ApiOkResponse({ description: 'Updated category', type: UpdateCategoryDto })
update(@Param('id') id: string, @Body() dto: UpdateCategoryDto) { update(@Param('id') id: string, @Body() dto: UpdateCategoryDto, @RestId() restId: string) {
return this.categoryService.update(id, dto); return this.categoryService.update(restId, id, dto);
} }
@UseGuards(AdminAuthGuard)
@ApiBearerAuth('/admin/categories')
@Delete(':id') @Delete(':id')
@ApiOperation({ summary: 'Delete category' }) @ApiOperation({ summary: 'Delete category' })
@ApiParam({ name: 'id' }) @ApiParam({ name: 'id' })
@ApiOkResponse({ description: 'Deleted' }) @ApiOkResponse({ description: 'Deleted' })
remove(@Param('id') id: string) { remove(@Param('id') id: string, @RestId() restId: string) {
return this.categoryService.remove(id); return this.categoryService.remove(restId, id);
}
@Get('restaurant/:restId')
@ApiOperation({ summary: 'Restaurant categories for user' })
@ApiOkResponse({ description: 'List of categories' })
@ApiParam({ name: 'restId', required: true, type: String })
findAllForRestaurant(@Param('restId') restId: string) {
return this.categoryService.findRestaurantCategories(restId);
} }
} }
@@ -47,8 +47,8 @@ export class CategoryService {
})) as unknown as Category[]; })) as unknown as Category[];
} }
async findOne(id: string): Promise<Category> { async findOne(restId: string, id: string): Promise<Category> {
const cat = await this.categoryRepository.findOne({ id }, { populate: ['foods'] }); const cat = await this.categoryRepository.findOne({ id, restId }, { populate: ['foods'] });
if (!cat) throw new NotFoundException(CategoryMessage.NOT_FOUND); if (!cat) throw new NotFoundException(CategoryMessage.NOT_FOUND);
return { return {
@@ -63,16 +63,31 @@ export class CategoryService {
} as unknown as Category; } as unknown as Category;
} }
async update(id: string, dto: UpdateCategoryDto): Promise<Category> { async findRestaurantCategories(restId: string): Promise<Category> {
const cat = await this.categoryRepository.findOne({ id }); const cat = await this.categoryRepository.findOne({ restId }, { populate: ['foods'] });
if (!cat) throw new NotFoundException(CategoryMessage.NOT_FOUND); 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); await this.em.persistAndFlush(cat);
return cat; return cat;
} }
async remove(id: string): Promise<void> { async remove(restId: string, id: string): Promise<void> {
const cat = await this.categoryRepository.findOne({ id }); const cat = await this.categoryRepository.findOne({ id, restId });
if (!cat) throw new NotFoundException(CategoryMessage.NOT_FOUND); if (!cat) throw new NotFoundException(CategoryMessage.NOT_FOUND);
cat.deletedAt = new Date(); cat.deletedAt = new Date();
await this.em.persistAndFlush(cat); await this.em.persistAndFlush(cat);