Skip to content

Commit

Permalink
fes-19-refactor-all-the-end-point-with-general-response (#27)
Browse files Browse the repository at this point in the history
* clear flag
Co-authored-by: NHT <[email protected]>
  • Loading branch information
nfesta2023 authored Jan 2, 2024
1 parent ea90d73 commit 62fadab
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 191 deletions.
127 changes: 14 additions & 113 deletions src/feature/category/category.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,51 +28,13 @@ export class CategoryService {
) {}

async getCategories(): Promise<any> {
if (
this.flagService.isFeatureEnabled(
'fes-19-refactor-all-the-end-point-with-general-response',
)
) {
const response = new GeneralResponse(200, '');
const categoriesDTO: SysCategoryDTO[] = [];
const categories = await this.sysCategoryRepo.find({
where: {
is_active: TRUE,
},
});
for (const category of categories) {
const categoryDTO = new SysCategoryDTO();
categoryDTO.sys_category_id = category.sys_category_id;
categoryDTO.type = category.type;
categoryDTO.image_url = category.image_obj.url;
category.extension.forEach((ext) => {
categoryDTO.name.push({
ISO_language_code: ext.ISO_language_code,
text: ext.name,
});
categoryDTO.description.push({
ISO_language_code: ext.ISO_language_code,
text: ext.description,
});
});
categoriesDTO.push(categoryDTO);
}

//Build the response
response.statusCode = 200;
response.message = 'Get all categories successfully';
response.data = categoriesDTO;

return response;
}
//CURRENT LOGIC
const response = new GeneralResponse(200, '');
const categoriesDTO: SysCategoryDTO[] = [];
const categories = await this.sysCategoryRepo.find({
where: {
is_active: TRUE,
},
});

for (const category of categories) {
const categoryDTO = new SysCategoryDTO();
categoryDTO.sys_category_id = category.sys_category_id;
Expand All @@ -88,87 +50,21 @@ export class CategoryService {
text: ext.description,
});
});

categoriesDTO.push(categoryDTO);
}

return categoriesDTO;
//Build the response
response.statusCode = 200;
response.message = 'Get all categories successfully';
response.data = categoriesDTO;

return response;
}

async searchFoodAndRestaurantByCategory(
data: SearchByCategory,
): Promise<any> {
if (
this.flagService.isFeatureEnabled(
'fes-19-refactor-all-the-end-point-with-general-response',
)
) {
const response = new GeneralResponse(200, '');
const { category_id, lat, long } = data;
const searchResult = new SearchResult();

//Get list of menu_item_id
const menuItemIds = (
await this.sysCatMenuItemRepo.find({
select: {
menu_item_id: true,
},
where: {
sys_category_id: category_id,
},
})
).map((item) => item.menu_item_id);

//Check if there is no menu_item -> return []
if (!menuItemIds || menuItemIds.length === 0) {
return searchResult;
}

const foodList =
await this.foodService.getFoodsWithListOfMenuItem(menuItemIds);

const restaurants =
await this.restaurantService.getDeliveryRestaurantByListOfId(
foodList.map((food) => food.restaurant_id),
lat,
long,
);

//Fill up Food data
for (const food of foodList) {
const restaurant = restaurants.find(
(res) => res.restaurant_id === food.restaurant_id,
);
const foodDTO = await this.commonService.convertIntoFoodDTO(
food,
restaurant,
);
searchResult.byFoods.push(foodDTO);
}

//Fill up Restaurant data
for (const restaurant of restaurants) {
const menuItems = await restaurant.menu_items;
const priceRange: PriceRange =
await this.foodService.getPriceRangeByMenuItem(
menuItems.map((item) => item.menu_item_id),
);
const restaurantDTO =
await this.restaurantService.convertIntoRestaurantDTO(
restaurant,
priceRange,
);
searchResult.byRestaurants.push(restaurantDTO);
}

//Build the response
response.statusCode = 200;
response.message = 'Search food and restaurant by category successfully';
response.data = searchResult;

return response;
}
//CURRENT LOGIC
const response = new GeneralResponse(200, '');
const { category_id, lat, long } = data;
const searchResult = new SearchResult();

Expand Down Expand Up @@ -226,6 +122,11 @@ export class CategoryService {
searchResult.byRestaurants.push(restaurantDTO);
}

return searchResult;
//Build the response
response.statusCode = 200;
response.message = 'Search food and restaurant by category successfully';
response.data = searchResult;

return response;
}
}
91 changes: 13 additions & 78 deletions src/feature/recommendation/recommendation.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,46 +20,7 @@ export class RecommendationService {
) {}

async getGeneralFoodRecomendation(lat: number, long: number): Promise<any> {
if (
this.flagService.isFeatureEnabled(
'fes-19-refactor-all-the-end-point-with-general-response',
)
) {
const response = new GeneralResponse(200, '');
const foodDTOs: FoodDTO[] = [];
const restaurants = await this.restaurantService.getRestaurantByRadius(
lat,
long,
5000, //meter
);
const restauranIds = restaurants.map(
(restaurant) => restaurant.restaurant_id,
);

const foods =
await this.foodService.getFoodsWithListOfRestaurants(restauranIds);

for (const food of foods) {
const restaurant = restaurants.find(
(res) => res.restaurant_id === food.restaurant_id,
);

const foodDTO = await this.commonService.convertIntoFoodDTO(
food,
restaurant,
);

foodDTOs.push(foodDTO);
}

// Build response
response.statusCode = 200;
response.message = 'Get food recommendation successfully';
response.data = foodDTOs;

return response;
}

const response = new GeneralResponse(200, '');
const foodDTOs: FoodDTO[] = [];
const restaurants = await this.restaurantService.getRestaurantByRadius(
lat,
Expand All @@ -86,51 +47,21 @@ export class RecommendationService {
foodDTOs.push(foodDTO);
}

return foodDTOs;
// Build response
response.statusCode = 200;
response.message = 'Get food recommendation successfully';
response.data = foodDTOs;

return response;
}

async getGeneralRestaurantRecomendation(
lat,
long,
lang: string = 'vie',
): Promise<any> {
if (
this.flagService.isFeatureEnabled(
'fes-19-refactor-all-the-end-point-with-general-response',
)
) {
const response = new GeneralResponse(200, '');

const restaurantList: RestaurantDTO[] = [];
const restaurants = await this.restaurantService.getRestaurantByRadius(
lat,
long,
5000,
);

for (const restaurant of restaurants) {
const menuItems = await restaurant.menu_items;
const priceRange: PriceRange =
await this.foodService.getPriceRangeByMenuItem(
menuItems.map((item) => item.menu_item_id),
);
const restaurantDTO =
await this.restaurantService.convertIntoRestaurantDTO(
restaurant,
priceRange,
);

restaurantList.push(restaurantDTO);
}

//Build response
response.statusCode = 200;
response.message = 'Get restaurant recommendation successfully';
response.data = restaurantList;
return response;
}
const response = new GeneralResponse(200, '');

// CURRENT LOGIC
const restaurantList: RestaurantDTO[] = [];
const restaurants = await this.restaurantService.getRestaurantByRadius(
lat,
Expand All @@ -153,6 +84,10 @@ export class RecommendationService {
restaurantList.push(restaurantDTO);
}

return restaurantList;
//Build response
response.statusCode = 200;
response.message = 'Get restaurant recommendation successfully';
response.data = restaurantList;
return response;
}
}

0 comments on commit 62fadab

Please sign in to comment.