Skip to content

Commit

Permalink
Fes 23 get side dishes (#29)
Browse files Browse the repository at this point in the history
* add route and build dto object

* create enity for side dish, get data from side dish table

---------

Co-authored-by: NHT <[email protected]>
  • Loading branch information
nfesta2023 and hoangtuan910 authored Jan 7, 2024
1 parent 2b5d46d commit 3bf3e60
Show file tree
Hide file tree
Showing 8 changed files with 209 additions and 1 deletion.
18 changes: 18 additions & 0 deletions src/entity/main-side-dish.entity.ts
Original file line number Diff line number Diff line change
@@ -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;
}
18 changes: 18 additions & 0 deletions src/enum/index.ts
Original file line number Diff line number Diff line change
@@ -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',
}
4 changes: 4 additions & 0 deletions src/feature/food/dto/get-side-dish-request.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export class GetSideDishRequest {
menu_item_id: number;
timestamp: number;
}
33 changes: 33 additions & 0 deletions src/feature/food/dto/get-side-dish-response.dto.ts
Original file line number Diff line number Diff line change
@@ -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;
}
8 changes: 8 additions & 0 deletions src/feature/food/food.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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);
}
}
}
2 changes: 2 additions & 0 deletions src/feature/food/food.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: [
Expand All @@ -44,6 +45,7 @@ import { NoAddingExt } from 'src/entity/no-adding-ext.entity';
SkuMenuItemVariant,
BasicCustomization,
NoAddingExt,
MainSideDish,
]),
],
controllers: [FoodController],
Expand Down
109 changes: 108 additions & 1 deletion src/feature/food/food.service.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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';
Expand All @@ -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 {
Expand Down Expand Up @@ -534,4 +541,104 @@ export class FoodService {
.getOne();
return unit.symbol;
}

async getSideDishByMenuItemId(
inputData: GetSideDishRequest,
): Promise<GetSideDishResonse> {
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;
}
}
}
18 changes: 18 additions & 0 deletions src/type/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

0 comments on commit 3bf3e60

Please sign in to comment.