From 3bf3e604cf068d17589df0bff1a9ca58b464a56d Mon Sep 17 00:00:00 2001 From: nfesta2023 <142601504+nfesta2023@users.noreply.github.com> Date: Sun, 7 Jan 2024 10:40:14 +0700 Subject: [PATCH] Fes 23 get side dishes (#29) * add route and build dto object * create enity for side dish, get data from side dish table --------- Co-authored-by: NHT --- src/entity/main-side-dish.entity.ts | 18 +++ src/enum/index.ts | 18 +++ .../food/dto/get-side-dish-request.dto.ts | 4 + .../food/dto/get-side-dish-response.dto.ts | 33 ++++++ src/feature/food/food.controller.ts | 8 ++ src/feature/food/food.module.ts | 2 + src/feature/food/food.service.ts | 109 +++++++++++++++++- src/type/index.ts | 18 +++ 8 files changed, 209 insertions(+), 1 deletion(-) create mode 100644 src/entity/main-side-dish.entity.ts create mode 100644 src/enum/index.ts create mode 100644 src/feature/food/dto/get-side-dish-request.dto.ts create mode 100644 src/feature/food/dto/get-side-dish-response.dto.ts diff --git a/src/entity/main-side-dish.entity.ts b/src/entity/main-side-dish.entity.ts new file mode 100644 index 0000000..819436e --- /dev/null +++ b/src/entity/main-side-dish.entity.ts @@ -0,0 +1,18 @@ +import { Entity, CreateDateColumn, PrimaryColumn } from 'typeorm'; + +@Entity('Main_Side_Dish') +export class MainSideDish { + @PrimaryColumn() + public main_dish_id: number; + + @PrimaryColumn() + public side_dish_id: number; + + @CreateDateColumn({ + type: 'datetime', + nullable: false, + unique: false, + default: () => 'CURRENT_TIMESTAMP', + }) + public created_at: Date; +} diff --git a/src/enum/index.ts b/src/enum/index.ts new file mode 100644 index 0000000..680f106 --- /dev/null +++ b/src/enum/index.ts @@ -0,0 +1,18 @@ +export enum Shift { + MorningFrom = '06:00:00', + MorningTo = '13:59:59', + AfternoonFrom = '14:00:00', + AfternoonTo = '21:59:59', + NightFrom = '22:00:00', + NightTo = '05:59:59', +} + +export enum Day { + Sunday = 'Sun', + Monday = 'Mon', + Tuesday = 'Tue', + Wednesday = 'Wed', + Thursday = 'Thu', + Friday = 'Fri', + Saturday = 'Sat', +} diff --git a/src/feature/food/dto/get-side-dish-request.dto.ts b/src/feature/food/dto/get-side-dish-request.dto.ts new file mode 100644 index 0000000..b5df213 --- /dev/null +++ b/src/feature/food/dto/get-side-dish-request.dto.ts @@ -0,0 +1,4 @@ +export class GetSideDishRequest { + menu_item_id: number; + timestamp: number; +} diff --git a/src/feature/food/dto/get-side-dish-response.dto.ts b/src/feature/food/dto/get-side-dish-response.dto.ts new file mode 100644 index 0000000..ea8f748 --- /dev/null +++ b/src/feature/food/dto/get-side-dish-response.dto.ts @@ -0,0 +1,33 @@ +import { GeneralResponse } from 'src/dto/general-response.dto'; +import { TextByLang } from 'src/type'; + +export class GetSideDishResonse extends GeneralResponse { + data: FoodDTO[]; +} + +interface FoodDTO { + id: number; + image: string; + top_label: string; + bottom_label: string; + name: TextByLang[]; + restaurant_name: TextByLang[]; + restaurant_id: number; + calorie_kcal: number; + rating: number; + distance_km: number; + delivery_time_s: number; + main_cooking_method: TextByLang[]; + ingredient_brief_vie: string; + ingredient_brief_eng: string; + price: number; + price_after_discount: number; + promotion: string; + cutoff_time: string; + preparing_time_s: number; + cooking_time_s: number; + quantity_available: number; + is_vegetarian: boolean; + cooking_schedule: string; + units_sold: number; +} diff --git a/src/feature/food/food.controller.ts b/src/feature/food/food.controller.ts index 7064033..26ba737 100644 --- a/src/feature/food/food.controller.ts +++ b/src/feature/food/food.controller.ts @@ -2,6 +2,7 @@ import { Controller, Inject } from '@nestjs/common'; import { FoodService } from './food.service'; import { MessagePattern } from '@nestjs/microservices'; import { FlagsmithService } from 'src/dependency/flagsmith/flagsmith.service'; +import { GetSideDishRequest } from './dto/get-side-dish-request.dto'; @Controller() export class FoodController { @@ -19,4 +20,11 @@ export class FoodController { async getListOfSkuById(id: number) { return await this.foodService.getListOfSkuById(id); } + + @MessagePattern({ cmd: 'get_side_dish_by_menu_item_id' }) + async getSideDishByMenuItemId(data: GetSideDishRequest) { + if (this.flagService.isFeatureEnabled('fes-23-get-side-dishes')) { + return await this.foodService.getSideDishByMenuItemId(data); + } + } } diff --git a/src/feature/food/food.module.ts b/src/feature/food/food.module.ts index 33271dc..76ea8d9 100644 --- a/src/feature/food/food.module.ts +++ b/src/feature/food/food.module.ts @@ -21,6 +21,7 @@ import { TasteValueExt } from 'src/entity/taste-value-ext.entity'; import { SkuMenuItemVariant } from 'src/entity/sku-menu-item-variant.entity'; import { BasicCustomization } from 'src/entity/basic-customization.entity'; import { NoAddingExt } from 'src/entity/no-adding-ext.entity'; +import { MainSideDish } from 'src/entity/main-side-dish.entity'; @Module({ imports: [ @@ -44,6 +45,7 @@ import { NoAddingExt } from 'src/entity/no-adding-ext.entity'; SkuMenuItemVariant, BasicCustomization, NoAddingExt, + MainSideDish, ]), ], controllers: [FoodController], diff --git a/src/feature/food/food.service.ts b/src/feature/food/food.service.ts index a51f055..d014ede 100644 --- a/src/feature/food/food.service.ts +++ b/src/feature/food/food.service.ts @@ -1,4 +1,4 @@ -import { Inject, Injectable } from '@nestjs/common'; +import { Get, Inject, Injectable } from '@nestjs/common'; import { InjectEntityManager, InjectRepository } from '@nestjs/typeorm'; import { MenuItem } from 'src/entity/menu-item.entity'; import { @@ -8,12 +8,14 @@ import { Repository, } from 'typeorm'; import { + DayShift, DeliveryRestaurant, Option, OptionValue, PriceRange, RatingStatistic, Review, + Schedule, TextByLang, } from 'src/type'; import { SKU } from 'src/entity/sku.entity'; @@ -33,6 +35,11 @@ import { Unit } from 'src/entity/unit.entity'; import { Restaurant } from 'src/entity/restaurant.entity'; import { BasicCustomization } from 'src/entity/basic-customization.entity'; import { CommonService } from '../common/common.service'; +import { GetSideDishRequest } from './dto/get-side-dish-request.dto'; +import { GetSideDishResonse } from './dto/get-side-dish-response.dto'; +import { MainSideDish } from 'src/entity/main-side-dish.entity'; +import { from } from 'rxjs'; +import { Day } from 'src/enum'; @Injectable() export class FoodService { @@ -534,4 +541,104 @@ export class FoodService { .getOne(); return unit.symbol; } + + async getSideDishByMenuItemId( + inputData: GetSideDishRequest, + ): Promise { + if (this.flagService.isFeatureEnabled('fes-23-get-side-dishes')) { + const res = new GetSideDishResonse(200, ''); + + const { menu_item_id, timestamp } = inputData; + + //Get the list of menu_item_id (side dishes) from table 'Main_Side_Dish' + const sideDishes = ( + await this.entityManager + .createQueryBuilder(MainSideDish, 'mainSideDish') + .where('mainSideDish.main_dish_id = :main_dish_id', { + main_dish_id: menu_item_id, + }) + .select('mainSideDish.side_dish_id') + .getMany() + ).map((item) => item.side_dish_id); + + // Check if there is no sidedish for this main dish + if (sideDishes.length === 0) { + res.statusCode = 404; + res.message = 'No side dishes found'; + return res; + } + + //Get main dish schedule + const mainDishSchedule = JSON.parse( + ( + await this.entityManager + .createQueryBuilder(MenuItem, 'menuItem') + .where('menuItem.menu_item_id = :menu_item_id', { menu_item_id }) + .getOne() + )?.cooking_schedule || null, + ); + + //get the earlies available dayshift of main dish + const earliestDayShift = this.getEarliesAvailabeDayShift( + timestamp, + mainDishSchedule, + ); + + res.statusCode = 200; + // res.message = 'Get side dishes successfully'; + res.message = earliestDayShift; + res.data = []; + return res; + } + } + + getEarliesAvailabeDayShift( + timestamp: number, + calendar: Schedule[], + ): DayShift { + if (this.flagService.isFeatureEnabled('fes-23-get-side-dishes')) { + const now = new Date(timestamp); + const currentTimeString = `${now + .getHours() + .toString() + .padStart(2, '0')}:${now.getMinutes().toString().padStart(2, '0')}:${now + .getSeconds() + .toString() + .padStart(2, '0')}`; + const currentDayShift: DayShift = { + dayId: (now.getDay() + 1).toString(), + dayName: '', + from: ' ', + to: '', + }; + switch (currentDayShift.dayId) { + case '1': + currentDayShift.dayName = Day.Sunday; + break; + case '2': + currentDayShift.dayName = Day.Monday; + break; + case '3': + currentDayShift.dayName = Day.Tuesday; + break; + case '4': + currentDayShift.dayName = Day.Wednesday; + break; + case '5': + currentDayShift.dayName = Day.Thursday; + break; + case '6': + currentDayShift.dayName = Day.Friday; + break; + case '7': + currentDayShift.dayName = Day.Saturday; + break; + + default: + break; + } + + return currentDayShift; + } + } } diff --git a/src/type/index.ts b/src/type/index.ts index 218bfce..298f1e1 100644 --- a/src/type/index.ts +++ b/src/type/index.ts @@ -66,3 +66,21 @@ export interface StandardAddress { latitude: number; longitude: number; } + +export interface DayShift { + dayId: string; + dayName: string; + from: string; + to: string; +} + +export interface Schedule { + dayId: string; + dayName: string; + schedule: ScheduleItem[]; +} +interface ScheduleItem { + from: string; + to: string; + isAvailable: boolean; +}