From 0386082d5ff79f909815931ec9105587e9eef25d Mon Sep 17 00:00:00 2001 From: morteza-mortezai Date: Tue, 7 Apr 2026 16:31:06 +0330 Subject: [PATCH] clean --- .../foods/controllers/category.controller.ts | 6 ++-- .../foods/controllers/food.controller.ts | 2 +- src/modules/foods/entities/category.entity.ts | 1 + .../foods/providers/category.service.ts | 30 ++++++------------- src/modules/foods/providers/food.service.ts | 30 ++++++------------- 5 files changed, 23 insertions(+), 46 deletions(-) diff --git a/src/modules/foods/controllers/category.controller.ts b/src/modules/foods/controllers/category.controller.ts index 9360e58..4da84aa 100644 --- a/src/modules/foods/controllers/category.controller.ts +++ b/src/modules/foods/controllers/category.controller.ts @@ -49,10 +49,10 @@ export class CategoryController { @ApiOperation({ summary: 'my restaurant categories' }) @UseGuards(AdminAuthGuard) @ApiBearerAuth() - @Get('admin/categories') + @Get('admin/categories') findAll(@RestId() restId: string) { return this.categoryService.findAllByRestaurantId(restId); - } + } @Permissions(Permission.MANAGE_CATEGORIES) @UseGuards(AdminAuthGuard) @@ -61,7 +61,7 @@ export class CategoryController { @ApiOperation({ summary: 'Get category' }) @ApiParam({ name: 'id', required: true, type: String }) findOne(@Param('id') id: string, @RestId() restId: string) { - return this.categoryService.findOne(restId, id); + return this.categoryService.findOneOrFail(restId, id); } @UseGuards(AdminAuthGuard) diff --git a/src/modules/foods/controllers/food.controller.ts b/src/modules/foods/controllers/food.controller.ts index 097bd7a..a645204 100644 --- a/src/modules/foods/controllers/food.controller.ts +++ b/src/modules/foods/controllers/food.controller.ts @@ -40,7 +40,7 @@ export class FoodController { @ApiOperation({ summary: 'Get a food by id' }) @ApiParam({ name: 'foodId', required: true }) findPublicFoodById(@Param('foodId') foodId: string, @UserId() userId?: string): Promise { - const a = this.foodsService.findPublicById(foodId, userId); + const a = this.foodsService.findByIdPublic(foodId, userId); return a; } diff --git a/src/modules/foods/entities/category.entity.ts b/src/modules/foods/entities/category.entity.ts index a2c679f..79c00ed 100644 --- a/src/modules/foods/entities/category.entity.ts +++ b/src/modules/foods/entities/category.entity.ts @@ -5,6 +5,7 @@ import { Restaurant } from 'src/modules/restaurants/entities/restaurant.entity'; @Entity({ tableName: 'categories' }) @Index({ properties: ['restaurant', 'isActive'] }) +@Index({ properties: ['restaurant', 'id'] }) @Index({ properties: ['isActive'] }) export class Category extends BaseEntity { @Property() diff --git a/src/modules/foods/providers/category.service.ts b/src/modules/foods/providers/category.service.ts index 2443825..0e0d809 100644 --- a/src/modules/foods/providers/category.service.ts +++ b/src/modules/foods/providers/category.service.ts @@ -7,11 +7,13 @@ import { RequiredEntityData } from '@mikro-orm/core'; import { Category } from '../entities/category.entity'; import { CategoryMessage, RestMessage } from 'src/common/enums/message.enum'; import { Restaurant } from 'src/modules/restaurants/entities/restaurant.entity'; +import { RestaurantsService } from 'src/modules/restaurants/providers/restaurants.service'; @Injectable() export class CategoryService { constructor( private readonly categoryRepository: CategoryRepository, + private readonly restaurantsService: RestaurantsService, private readonly em: EntityManager, ) { } @@ -33,10 +35,8 @@ export class CategoryService { } async findAllByRestaurant(slug: string): Promise { - const restaurant = await this.em.findOne(Restaurant, { slug }); - if (!restaurant || !restaurant.id) { - throw new NotFoundException(RestMessage.NOT_FOUND); - } + const restaurant = await this.restaurantsService.findBySlug(slug) + return this.categoryRepository.find( { restaurant: restaurant, isActive: true }, { orderBy: { order: 'ASC' } }); } @@ -47,34 +47,22 @@ export class CategoryService { { orderBy: { order: 'asc' } }); } - async findOne(restId: string, id: string): Promise { - const cat = await this.categoryRepository.findOne({ id, restaurant: { id: restId } }, { populate: ['foods'] }); + async findOneOrFail(restId: string, id: string): Promise { + const cat = await this.categoryRepository.findOne({ id, restaurant: { id: restId } }); if (!cat) throw new NotFoundException(CategoryMessage.NOT_FOUND); - - return { - id: cat.id, - title: cat.title, - isActive: cat.isActive, - restaurant: cat.restaurant, - avatarUrl: cat.avatarUrl, - createdAt: cat.createdAt, - updatedAt: cat.updatedAt, - foods: cat.foods.getItems().map(f => ({ id: f.id, title: f.title })), - } as unknown as Category; + return cat } async update(restId: string, id: string, dto: UpdateCategoryDto): Promise { - const cat = await this.categoryRepository.findOne({ id, restaurant: { id: restId } }); - if (!cat) throw new NotFoundException(CategoryMessage.NOT_FOUND); + const cat = await this.findOneOrFail(restId, id) this.em.assign(cat, dto); await this.em.persistAndFlush(cat); return cat; } async remove(restId: string, id: string): Promise { - const cat = await this.categoryRepository.findOne({ id, restaurant: { id: restId } }); - if (!cat) throw new NotFoundException(CategoryMessage.NOT_FOUND); + const cat = await this.findOneOrFail(restId, id) cat.deletedAt = new Date(); await this.em.persistAndFlush(cat); } diff --git a/src/modules/foods/providers/food.service.ts b/src/modules/foods/providers/food.service.ts index 2972052..f741a95 100644 --- a/src/modules/foods/providers/food.service.ts +++ b/src/modules/foods/providers/food.service.ts @@ -7,27 +7,24 @@ import { EntityManager } from '@mikro-orm/postgresql'; import { RequiredEntityData, FilterQuery } from '@mikro-orm/core'; import { Food } from '../entities/food.entity'; import { CategoryMessage, FoodMessage, RestMessage } from 'src/common/enums/message.enum'; -import { RestRepository } from 'src/modules/restaurants/repositories/rest.repository'; -import { CacheService } from '../../utils/cache.service'; import { Favorite } from '../entities/favorite.entity'; import { MealType } from '../interface/food.interface'; import { Inventory } from 'src/modules/inventory/entities/inventory.entity'; +import { RestaurantsService } from 'src/modules/restaurants/providers/restaurants.service'; @Injectable() export class FoodService { constructor( private readonly foodRepository: FoodRepository, private readonly categoryRepository: CategoryRepository, - private readonly restRepository: RestRepository, + private readonly restService: RestaurantsService, private readonly em: EntityManager, ) { } async create(restId: string, createFoodDto: CreateFoodDto) { const { categoryId, dailyStock = 0, ...rest } = createFoodDto; - const restaurant = await this.restRepository.findOne({ id: restId }); - if (!restaurant) { - throw new NotFoundException(RestMessage.NOT_FOUND); - } + const restaurant = await this.restService.findOneOrFail(restId); + const category = await this.categoryRepository.findOne({ id: categoryId, restaurant: { id: restId } }); if (!category) { throw new NotFoundException(CategoryMessage.NOT_FOUND); @@ -84,7 +81,7 @@ export class FoodService { /** * Public food detail (only active foods are visible). */ - async findPublicById(foodId: string, userId?: string): Promise { + async findByIdPublic(foodId: string, userId?: string): Promise { const food = await this.foodRepository.findOne({ id: foodId, isActive: true }, { populate: ['category', 'inventory'] }); if (!food) throw new NotFoundException(FoodMessage.NOT_FOUND); let isFavorite = false; @@ -101,7 +98,8 @@ export class FoodService { * Admin food detail (scoped to the authenticated restaurant). */ async findAdminById(restId: string, id: string): Promise { - const food = await this.foodRepository.findOne({ id, restaurant: { id: restId } }, { populate: ['category', 'inventory'] }); + const food = await this.foodRepository.findOne({ id, restaurant: { id: restId } }, + { populate: ['category', 'inventory'] }); if (!food) throw new NotFoundException(FoodMessage.NOT_FOUND); return food; @@ -113,11 +111,6 @@ export class FoodService { * @returns Array of active foods matching current day and meal time */ async findByWeekDateAndMealType(slug: string): Promise { - const restaurant = await this.restRepository.findOne({ slug }); - if (!restaurant) { - throw new NotFoundException(RestMessage.NOT_FOUND); - } - const { iranWeekDay, mealType } = this.getCurrentIranTimeContext(); const queryFilter: FilterQuery = { @@ -263,11 +256,6 @@ export class FoodService { async getMyFavorites(userId: string, restId: string) { return this.em.find(Favorite, { user: userId, food: { restaurant: { id: restId } } }, { populate: ['food'] }); } - /** - * Invalidate cache for restaurant foods - */ - // private async invalidateRestaurantFoodsCache(slug: string): Promise { - // const cacheKey = `${this.FOODS_BY_RESTAURANT_CACHE_KEY_PREFIX}${slug}`; - // await this.cacheService.del(cacheKey); - // } + + }