From ca2e9aab95db2779c3e6ef7ba9d77fa3932a75c7 Mon Sep 17 00:00:00 2001 From: morteza-mortezai Date: Fri, 19 Dec 2025 23:30:04 +0330 Subject: [PATCH] foods --- .../foods/controllers/food.controller.ts | 2 +- src/modules/foods/interface/food.interface.ts | 1 + src/modules/foods/providers/food.service.ts | 32 +++++++++++++++++-- 3 files changed, 31 insertions(+), 4 deletions(-) diff --git a/src/modules/foods/controllers/food.controller.ts b/src/modules/foods/controllers/food.controller.ts index 77d40e9..4d9c1dd 100644 --- a/src/modules/foods/controllers/food.controller.ts +++ b/src/modules/foods/controllers/food.controller.ts @@ -32,7 +32,7 @@ export class FoodController { @ApiParam({ name: 'slug', required: true, description: 'Restaurant Slug' }) @ApiOkResponse({ description: 'List of all foods for the restaurant' }) findAllByRestaurant(@Param('slug') slug: string) { - return this.foodsService.findAllByRestaurant(slug); + return this.foodsService.findByWeekDateAndMealType(slug); } @Get('public/foods/:foodId') diff --git a/src/modules/foods/interface/food.interface.ts b/src/modules/foods/interface/food.interface.ts index 8001b5c..033e3ca 100644 --- a/src/modules/foods/interface/food.interface.ts +++ b/src/modules/foods/interface/food.interface.ts @@ -2,4 +2,5 @@ export enum MealType { BREAKFAST = 'breakfast', LUNCH = 'lunch', DINNER = 'dinner', + SNACK = 'snack', } diff --git a/src/modules/foods/providers/food.service.ts b/src/modules/foods/providers/food.service.ts index fafa860..5b2ec27 100644 --- a/src/modules/foods/providers/food.service.ts +++ b/src/modules/foods/providers/food.service.ts @@ -4,12 +4,13 @@ import { FindFoodsDto } from '../dto/find-foods.dto'; import { FoodRepository } from '../repositories/food.repository'; import { CategoryRepository } from '../repositories/category.repository'; import { EntityManager } from '@mikro-orm/postgresql'; -import { RequiredEntityData } from '@mikro-orm/core'; +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'; @Injectable() export class FoodService { @@ -100,7 +101,7 @@ export class FoodService { return food; } - async findAllByRestaurant(slug: string): Promise { + async findByWeekDateAndMealType(slug: string): Promise { // const cacheKey = `${this.FOODS_BY_RESTAURANT_CACHE_KEY_PREFIX}${slug}`; // Try to get from cache first @@ -121,8 +122,33 @@ export class FoodService { if (!restaurant) { throw new NotFoundException(RestMessage.NOT_FOUND); } + const nowInIran = new Date(new Date().toLocaleString('en-US', { timeZone: 'Asia/Tehran' })); - const foods = await this.foodRepository.find({ restaurant: { slug }, isActive: true }, { populate: ['category'] }); + const weekDay = nowInIran.getDay(); // 0 = Sunday, 6 = Saturday + const currentHour = nowInIran.getHours(); + + const iranWeekDay = (weekDay + 1) % 7; + + let mealType: string | null = null; + if (currentHour >= 6 && currentHour < 11) { + mealType = MealType.BREAKFAST; + } else if (currentHour >= 11 && currentHour < 15) { + mealType = MealType.LUNCH; + } else if (currentHour >= 15 && currentHour < 19) { + mealType = MealType.SNACK; + } else if (currentHour >= 19 && currentHour < 24) { + mealType = MealType.DINNER; + } + const foods = await this.foodRepository.find( + // cast to FilterQuery because MikroORM types don't accept a numeric $contains value for jsonb number[] + { + restaurant: { slug }, + isActive: true, + ...(mealType ? { mealTypes: { $contains: mealType } } : {}), + weekDays: { $contains: iranWeekDay }, + } as unknown as FilterQuery, + { populate: ['category'] }, + ); // Cache the result // await this.cacheService.set(cacheKey, JSON.stringify(foods), this.FOODS_CACHE_TTL);