Skip to content

Commit

Permalink
fes-16-get-list-of-skus (#25)
Browse files Browse the repository at this point in the history
* clear flag

* add sku_id

---------

Co-authored-by: NHT <[email protected]>
  • Loading branch information
nfesta2023 and hoangtuan910 authored Jan 2, 2024
1 parent 427752c commit dae468b
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 79 deletions.
1 change: 1 addition & 0 deletions src/dto/sku.dto.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export class SkuDTO {
sku_id: number;
price: number;
price_after_discount: number;
unit: string;
Expand Down
5 changes: 1 addition & 4 deletions src/feature/food/food.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,6 @@ export class FoodController {

@MessagePattern({ cmd: 'get_list_of_sku_by_id' })
async getListOfSkuById(id: number) {
if (this.flagService.isFeatureEnabled('fes-16-get-list-of-skus')) {
return await this.foodService.getListOfSkuById(id);
}
//CURRENT LOGIC
return await this.foodService.getListOfSkuById(id);
}
}
142 changes: 67 additions & 75 deletions src/feature/food/food.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -456,90 +456,82 @@ export class FoodService {
}

async getListOfSkuById(id: number) {
if (this.flagService.isFeatureEnabled('fes-16-get-list-of-skus')) {
const result = new GeneralResponse(200, '');

const data: SkuDTO[] = [];

const rawSKUs = await this.entityManager
.createQueryBuilder(SKU, 'sku')
.leftJoinAndSelect('sku.menu_item_variants', 'variant')
.leftJoinAndSelect('variant.attribute', 'attribute')
.where('sku.menu_item_id = :id', { id })
.andWhere('sku.is_active = :active', { active: TRUE })
.getMany();

if (rawSKUs.length === 0) {
result.statusCode = 404;
result.message = 'SKUs not found';
return result;
}
const result = new GeneralResponse(200, '');

const priceUnit = await this.getMenuItemPriceUnit(id);

for (const rawSKU of rawSKUs) {
const sku: SkuDTO = {
price: rawSKU.price,
price_after_discount:
await this.commonService.getAvailableDiscountPrice(rawSKU),
unit: priceUnit,
is_standard: Boolean(rawSKU.is_standard),
calorie_kcal: rawSKU.calorie_kcal,
carb_g: rawSKU.carbohydrate_g,
protein_g: rawSKU.protein_g,
fat_g: rawSKU.fat_g,
portion_customization: rawSKU.menu_item_variants
.filter((i) => i.attribute.type == 'portion')
.map((e) => {
return {
option_id: e.variant.toString(),
value_id: e.option.toString(),
};
}),
};
const data: SkuDTO[] = [];

data.push(sku);
}
const rawSKUs = await this.entityManager
.createQueryBuilder(SKU, 'sku')
.leftJoinAndSelect('sku.menu_item_variants', 'variant')
.leftJoinAndSelect('variant.attribute', 'attribute')
.where('sku.menu_item_id = :id', { id })
.andWhere('sku.is_active = :active', { active: TRUE })
.getMany();

result.statusCode = 200;
result.message = 'Getting List Of SKUs Successfully';
result.data = data;
if (rawSKUs.length === 0) {
result.statusCode = 404;
result.message = 'SKUs not found';
return result;
}
//CURRENT LOGIC

const priceUnit = await this.getMenuItemPriceUnit(id);

for (const rawSKU of rawSKUs) {
const sku: SkuDTO = {
sku_id: rawSKU.sku_id,
price: rawSKU.price,
price_after_discount:
await this.commonService.getAvailableDiscountPrice(rawSKU),
unit: priceUnit,
is_standard: Boolean(rawSKU.is_standard),
calorie_kcal: rawSKU.calorie_kcal,
carb_g: rawSKU.carbohydrate_g,
protein_g: rawSKU.protein_g,
fat_g: rawSKU.fat_g,
portion_customization: rawSKU.menu_item_variants
.filter((i) => i.attribute.type == 'portion')
.map((e) => {
return {
option_id: e.variant.toString(),
value_id: e.option.toString(),
};
}),
};

data.push(sku);
}

result.statusCode = 200;
result.message = 'Getting List Of SKUs Successfully';
result.data = data;
return result;
}
async getSkuPriceUnit(sku_id: number) {
if (this.flagService.isFeatureEnabled('fes-16-get-list-of-skus')) {
const unit = await this.entityManager
.createQueryBuilder(Unit, 'unit')
.leftJoin(Restaurant, 'restaurant', 'restaurant.unit = unit.unit_id')
.leftJoin(
MenuItem,
'menuItem',
'menuItem.restaurant_id = restaurant.restaurant_id',
)
.leftJoin(SKU, 'sku', 'sku.menu_item_id = menuItem.menu_item_id')
.where('sku.sku_id = :sku_id', { sku_id })
.getOne();
return unit.symbol;
}
//CURRENT LOGIC
const unit = await this.entityManager
.createQueryBuilder(Unit, 'unit')
.leftJoin(Restaurant, 'restaurant', 'restaurant.unit = unit.unit_id')
.leftJoin(
MenuItem,
'menuItem',
'menuItem.restaurant_id = restaurant.restaurant_id',
)
.leftJoin(SKU, 'sku', 'sku.menu_item_id = menuItem.menu_item_id')
.where('sku.sku_id = :sku_id', { sku_id })
.getOne();
return unit.symbol;
}

async getMenuItemPriceUnit(menu_item_id: number) {
if (this.flagService.isFeatureEnabled('fes-16-get-list-of-skus')) {
const unit = await this.entityManager
.createQueryBuilder(Unit, 'unit')
.leftJoin(Restaurant, 'restaurant', 'restaurant.unit = unit.unit_id')
.leftJoin(
MenuItem,
'menuItem',
'menuItem.restaurant_id = restaurant.restaurant_id',
)
.where('menuItem.menu_item_id = :menu_item_id', { menu_item_id })
.getOne();
return unit.symbol;
}
//CURRENT LOGIC
const unit = await this.entityManager
.createQueryBuilder(Unit, 'unit')
.leftJoin(Restaurant, 'restaurant', 'restaurant.unit = unit.unit_id')
.leftJoin(
MenuItem,
'menuItem',
'menuItem.restaurant_id = restaurant.restaurant_id',
)
.where('menuItem.menu_item_id = :menu_item_id', { menu_item_id })
.getOne();
return unit.symbol;
}
}

0 comments on commit dae468b

Please sign in to comment.