This commit is contained in:
2025-12-19 23:30:04 +03:30
parent af5616225e
commit ca2e9aab95
3 changed files with 31 additions and 4 deletions
@@ -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')
@@ -2,4 +2,5 @@ export enum MealType {
BREAKFAST = 'breakfast',
LUNCH = 'lunch',
DINNER = 'dinner',
SNACK = 'snack',
}
+29 -3
View File
@@ -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<Food[]> {
async findByWeekDateAndMealType(slug: string): Promise<Food[]> {
// 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<Food> 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<Food>,
{ populate: ['category'] },
);
// Cache the result
// await this.cacheService.set(cacheKey, JSON.stringify(foods), this.FOODS_CACHE_TTL);