foods
This commit is contained in:
@@ -32,7 +32,7 @@ export class FoodController {
|
|||||||
@ApiParam({ name: 'slug', required: true, description: 'Restaurant Slug' })
|
@ApiParam({ name: 'slug', required: true, description: 'Restaurant Slug' })
|
||||||
@ApiOkResponse({ description: 'List of all foods for the restaurant' })
|
@ApiOkResponse({ description: 'List of all foods for the restaurant' })
|
||||||
findAllByRestaurant(@Param('slug') slug: string) {
|
findAllByRestaurant(@Param('slug') slug: string) {
|
||||||
return this.foodsService.findAllByRestaurant(slug);
|
return this.foodsService.findByWeekDateAndMealType(slug);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Get('public/foods/:foodId')
|
@Get('public/foods/:foodId')
|
||||||
|
|||||||
@@ -2,4 +2,5 @@ export enum MealType {
|
|||||||
BREAKFAST = 'breakfast',
|
BREAKFAST = 'breakfast',
|
||||||
LUNCH = 'lunch',
|
LUNCH = 'lunch',
|
||||||
DINNER = 'dinner',
|
DINNER = 'dinner',
|
||||||
|
SNACK = 'snack',
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,12 +4,13 @@ import { FindFoodsDto } from '../dto/find-foods.dto';
|
|||||||
import { FoodRepository } from '../repositories/food.repository';
|
import { FoodRepository } from '../repositories/food.repository';
|
||||||
import { CategoryRepository } from '../repositories/category.repository';
|
import { CategoryRepository } from '../repositories/category.repository';
|
||||||
import { EntityManager } from '@mikro-orm/postgresql';
|
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 { Food } from '../entities/food.entity';
|
||||||
import { CategoryMessage, FoodMessage, RestMessage } from 'src/common/enums/message.enum';
|
import { CategoryMessage, FoodMessage, RestMessage } from 'src/common/enums/message.enum';
|
||||||
import { RestRepository } from 'src/modules/restaurants/repositories/rest.repository';
|
import { RestRepository } from 'src/modules/restaurants/repositories/rest.repository';
|
||||||
import { CacheService } from '../../utils/cache.service';
|
import { CacheService } from '../../utils/cache.service';
|
||||||
import { Favorite } from '../entities/favorite.entity';
|
import { Favorite } from '../entities/favorite.entity';
|
||||||
|
import { MealType } from '../interface/food.interface';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class FoodService {
|
export class FoodService {
|
||||||
@@ -100,7 +101,7 @@ export class FoodService {
|
|||||||
return food;
|
return food;
|
||||||
}
|
}
|
||||||
|
|
||||||
async findAllByRestaurant(slug: string): Promise<Food[]> {
|
async findByWeekDateAndMealType(slug: string): Promise<Food[]> {
|
||||||
// const cacheKey = `${this.FOODS_BY_RESTAURANT_CACHE_KEY_PREFIX}${slug}`;
|
// const cacheKey = `${this.FOODS_BY_RESTAURANT_CACHE_KEY_PREFIX}${slug}`;
|
||||||
|
|
||||||
// Try to get from cache first
|
// Try to get from cache first
|
||||||
@@ -121,8 +122,33 @@ export class FoodService {
|
|||||||
if (!restaurant) {
|
if (!restaurant) {
|
||||||
throw new NotFoundException(RestMessage.NOT_FOUND);
|
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
|
// Cache the result
|
||||||
// await this.cacheService.set(cacheKey, JSON.stringify(foods), this.FOODS_CACHE_TTL);
|
// await this.cacheService.set(cacheKey, JSON.stringify(foods), this.FOODS_CACHE_TTL);
|
||||||
|
|||||||
Reference in New Issue
Block a user