Skip to content

Commit

Permalink
FES-79-update-packaging-db-change (#61)
Browse files Browse the repository at this point in the history
- adjust dto, entity and logic to get data
Co-authored-by: NHT <[email protected]>
  • Loading branch information
nfesta2023 authored Mar 2, 2024
1 parent 14bad8c commit 148ef1c
Show file tree
Hide file tree
Showing 10 changed files with 189 additions and 50 deletions.
4 changes: 2 additions & 2 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"singleQuote": true,
"trailingComma": "all",
"printWidth": 200
}
"printWidth": 80
}
9 changes: 8 additions & 1 deletion src/dto/food-detail.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export class FoodDetailDTO {
units_sold: number;
review_number: number;
promotion: string;
packaging_info: TextByLang[];
packaging_info: PackagingInfo[];
cutoff_time: string;
ingredients: Ingredient[];
description: TextByLang[];
Expand All @@ -31,3 +31,10 @@ class BasicCustomization {
no_adding_id: string;
description: TextByLang[];
}
interface PackagingInfo {
image_url: string;
name: TextByLang[];
description: TextByLang[];
price: number;
currency: string;
}
7 changes: 7 additions & 0 deletions src/entity/media.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@ import {
Column,
CreateDateColumn,
Entity,
JoinColumn,
ManyToOne,
OneToMany,
PrimaryGeneratedColumn,
} from 'typeorm';
import { MenuItem } from './menu-item.entity';
import { Packaging } from './packaging.entity';

@Entity('Media')
export class Media {
Expand Down Expand Up @@ -51,4 +54,8 @@ export class Media {

@OneToMany(() => MenuItem, (menuItem) => menuItem.image_obj)
public menu_item_obj: MenuItem[];

@ManyToOne(() => Packaging, (packaging) => packaging.media_obj)
@JoinColumn({ name: 'packaging_id', referencedColumnName: 'packaging_id' })
public packaging_obj: Packaging;
}
12 changes: 9 additions & 3 deletions src/entity/menu-item.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { MenuItemExt } from './menu-item-ext.entity';
import { SKU } from './sku.entity';
import { Media } from './media.entity';
import { Recipe } from './recipe.entity';
import { Packaging } from './packaging.entity';
import { MenuItemPackaging } from './menuitem-packaging.entity';

@Entity('Menu_Item')
export class MenuItem {
Expand Down Expand Up @@ -113,6 +113,12 @@ export class MenuItem {
@OneToMany(() => Recipe, (recipe) => recipe.menu_item)
public recipe: Promise<Recipe[]>;

@OneToMany(() => Packaging, (packaging) => packaging.menu_item_obj)
public packaging_obj: Packaging[];
// @OneToMany(() => Packaging, (packaging) => packaging.menu_item_obj)
// public packaging_obj: Packaging[];

@OneToMany(
() => MenuItemPackaging,
(menuItemPackaging) => menuItemPackaging.menu_item_obj,
)
public menuItemPackaging_obj: MenuItemPackaging[];
}
54 changes: 54 additions & 0 deletions src/entity/menuitem-packaging.entity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import {
Entity,
CreateDateColumn,
PrimaryColumn,
Column,
JoinColumn,
ManyToOne,
OneToOne,
} from 'typeorm';
import { Packaging } from './packaging.entity';
import { MenuItem } from './menu-item.entity';
import { Media } from './media.entity';

@Entity('MenuItem_Packaging')
export class MenuItemPackaging {
@PrimaryColumn({ type: 'int', nullable: false })
public packaging_id: number;

@PrimaryColumn({ type: 'int', nullable: false })
public menu_item_id: number;

@Column({ type: 'int', nullable: false })
public image: number;

@CreateDateColumn({
type: 'datetime',
nullable: false,
unique: false,
default: () => 'CURRENT_TIMESTAMP',
})
public created_at: Date;

//RELATIONSHIP
@ManyToOne(() => Packaging)
@JoinColumn({
name: 'packaging_id',
referencedColumnName: 'packaging_id',
})
public packaging_obj: Packaging;

@ManyToOne(() => MenuItem)
@JoinColumn({
name: 'menu_item_id',
referencedColumnName: 'menu_item_id',
})
public menu_item_obj: MenuItem;

@OneToOne(() => Media)
@JoinColumn({
name: 'image',
referencedColumnName: 'media_id',
})
image_obj: Media;
}
12 changes: 4 additions & 8 deletions src/entity/packaging-ext.entity.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,4 @@
import {
Entity,
CreateDateColumn,
PrimaryColumn,
Column,
ManyToOne,
JoinColumn,
} from 'typeorm';
import { Entity, CreateDateColumn, PrimaryColumn, Column, ManyToOne, JoinColumn } from 'typeorm';
import { Packaging } from './packaging.entity';

@Entity('Packaging_Ext')
Expand All @@ -15,6 +8,9 @@ export class PackagingExt {
@PrimaryColumn()
public ISO_language_code: string;

@Column({ type: 'varchar', length: 64, nullable: true, unique: false })
public name: string;

@Column({ type: 'varchar', length: 255, nullable: true, unique: false })
public description: string;

Expand Down
25 changes: 16 additions & 9 deletions src/entity/packaging.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,24 @@ import {
} from 'typeorm';
import { MenuItem } from './menu-item.entity';
import { PackagingExt } from './packaging-ext.entity';
import { Media } from './media.entity';

@Entity('Packaging')
export class Packaging {
@PrimaryGeneratedColumn()
public packaging_id: number;

// @Column({ type: 'int', nullable: false, unique: false })
// public menu_item_id: number;

@Column({ type: 'int', nullable: false, unique: false })
public menu_item_id: number;
public restaurant_id: number;

@Column({ type: 'int', nullable: false, unique: false })
public price: number;

@Column({ type: 'int', nullable: false, unique: false })
public currency: number;
// @Column({ type: 'int', nullable: false, unique: false })
// public currency: number;

@CreateDateColumn({
type: 'datetime',
Expand All @@ -34,13 +38,16 @@ export class Packaging {

//RELATIONSHIPS

@ManyToOne(() => MenuItem, (menuItem) => menuItem.packaging_obj)
@JoinColumn({
name: 'menu_item_id',
referencedColumnName: 'menu_item_id',
})
public menu_item_obj: MenuItem;
// @ManyToOne(() => MenuItem, (menuItem) => menuItem.packaging_obj)
// @JoinColumn({
// name: 'menu_item_id',
// referencedColumnName: 'menu_item_id',
// })
// public menu_item_obj: MenuItem;

@OneToMany(() => PackagingExt, (packagingExt) => packagingExt.packaging_obj)
public packaging_ext_obj: PackagingExt[];

@OneToMany(() => Media, (media) => media.packaging_obj)
public media_obj: Media[];
}
10 changes: 9 additions & 1 deletion src/feature/food/dto/get-food-detail-response.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ interface FoodDetail {
units_sold: number;
review_number: number;
promotion: string;
packaging_info: TextByLang[];
packaging_info: PackagingInfo[];
cutoff_time: string;
ingredients: Ingredient[];
description: TextByLang[];
Expand Down Expand Up @@ -64,3 +64,11 @@ interface TextByLang {
ISO_language_code: string;
text: string;
}

interface PackagingInfo {
image_url: string;
name: TextByLang[];
description: TextByLang[];
price: number;
currency: string;
}
98 changes: 72 additions & 26 deletions src/feature/food/food.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
DayShift,
Option,
OptionValue,
PackagingInfo,
PriceRange,
RatingStatistic,
Review,
Expand All @@ -32,6 +33,7 @@ import { DayName, Shift } from 'src/enum';
import { FoodDTO } from 'src/dto/food.dto';
import { GetFoodDetailResponse } from './dto/get-food-detail-response.dto';
import { readFileSync } from 'fs';
import { MenuItemPackaging } from 'src/entity/menuitem-packaging.entity';

@Injectable()
export class FoodService {
Expand Down Expand Up @@ -213,7 +215,10 @@ export class FoodService {
units_sold: foods[0].units_sold,
review_number: ratingStatistic.total_count,
promotion: foods[0].promotion,
packaging_info: await this.generatePackageSentenceByLang(packaging),
packaging_info: await this.generatePackagingInfoForMenuItem(
packaging,
menuItemId,
),
cutoff_time: foods[0].cutoff_time,
ingredients: recipe.map((item) => {
return {
Expand Down Expand Up @@ -289,33 +294,33 @@ export class FoodService {
// Get data from table Media
// - image profile
// - other iamge with menu_item_id
// - package image with packaging_id

//Get list of packaging_id
const packaging_ids = (
await this.entityManager
.createQueryBuilder(Packaging, 'packaging')
.where('packaging.menu_item_id = :menu_item_id', { menu_item_id })
.select(['packaging.packaging_id'])
.getMany()
).map((item) => item.packaging_id);

//Select medias
const data = await this.entityManager
.createQueryBuilder(Media, 'media')
.leftJoin('media.menu_item_obj', 'menuItem')
.where('menuItem.menu_item_id = :menu_item_id', { menu_item_id })
.orWhere('media.menu_item_id = :menu_item_id', { menu_item_id })
.orWhere('media.packaging_id IN (:...packaging_ids)', { packaging_ids })
.getMany();
return data;
} // end of getAllMediaByMenuItemId

async getPackagingByMenuItemId(menu_item_id: number): Promise<Packaging[]> {
async getPackagingByMenuItemId(
menu_item_id: number,
): Promise<MenuItemPackaging[]> {
// const data = await this.entityManager
// .createQueryBuilder(Packaging, 'packaging')
// .leftJoinAndSelect('packaging.packaging_ext_obj', 'ext')
// .where('packaging.menu_item_id = :menu_item_id', { menu_item_id })
// .getMany();

const data = await this.entityManager
.createQueryBuilder(Packaging, 'packaging')
.leftJoinAndSelect('packaging.packaging_ext_obj', 'ext')
.where('packaging.menu_item_id = :menu_item_id', { menu_item_id })
.createQueryBuilder(MenuItemPackaging, 'menuItemPackaging')
.leftJoinAndSelect('menuItemPackaging.image_obj', 'itemImage')
.leftJoinAndSelect('menuItemPackaging.packaging_obj', 'packaging')
.leftJoinAndSelect('packaging.packaging_ext_obj', 'packageExt')
.leftJoinAndSelect('packaging.media_obj', 'media')
.where('menuItemPackaging.menu_item_id = :menu_item_id', { menu_item_id })
.getMany();
return data;
} // end of getPackagingByMenuItemId
Expand Down Expand Up @@ -344,19 +349,49 @@ export class FoodService {
return data;
} // end of getPortionCustomizationByMenuItemId

async generatePackageSentenceByLang(packageInfo: Packaging[]) {
const sentenceByLang: TextByLang[] = [];
for (const item of packageInfo) {
item.packaging_ext_obj.forEach((element) => {
const sentence: TextByLang = {
ISO_language_code: element.ISO_language_code,
text: `${element.description} (+${item.price})`,
async generatePackagingInfoForMenuItem(
menuItemPackagings: MenuItemPackaging[],
menu_item_id: number,
): Promise<PackagingInfo[]> {
const currency = await this.getCurrencyOfMenuItem(menu_item_id);
const packagingInfos: PackagingInfo[] = [];
for (const menuItemPackaging of menuItemPackagings) {
//packaging image
let image_url = '';
if (menuItemPackaging.image_obj) {
image_url = menuItemPackaging.image_obj.url;
} else if (menuItemPackaging.packaging_obj.media_obj.length > 0) {
image_url = menuItemPackaging.packaging_obj.media_obj[0].url;
} else {
image_url = null;
}
//packaging name & description
const name = [];
const description = [];
menuItemPackaging.packaging_obj.packaging_ext_obj.forEach((e) => {
const nameExt: TextByLang = {
ISO_language_code: e.ISO_language_code,
text: e.name,
};
sentenceByLang.push(sentence);
name.push(nameExt);
const descriptionExt: TextByLang = {
ISO_language_code: e.ISO_language_code,
text: e.description,
};
description.push(descriptionExt);
});

const packagingInfo: PackagingInfo = {
image_url: image_url,
name: name,
description: description,
price: menuItemPackaging.packaging_obj.price,
currency: currency.symbol,
};
packagingInfos.push(packagingInfo);
}
return sentenceByLang;
} // end of generatePackageSentenceByLang
return packagingInfos;
} // end of generatePackagingInfoForMenuItem

async convertPortionCustomization(
portionCustomization: MenuItemAttribute[],
Expand Down Expand Up @@ -771,4 +806,15 @@ export class FoodService {
}
return foods;
} // end of getAvailableFoodByRestaurantFromEndPoint

async getCurrencyOfMenuItem(menu_item_id: number): Promise<Unit> {
const menuItem = await this.entityManager
.createQueryBuilder(MenuItem, 'menuItem')
.leftJoinAndSelect('menuItem.restaurant', 'restaurant')
.leftJoinAndSelect('restaurant.unit_obj', 'unit')
.where('menuItem.menu_item_id = :menu_item_id', { menu_item_id })
.getOne();
return menuItem.restaurant.unit_obj;
}
//end of getCurrencyOfMenuItem
}
8 changes: 8 additions & 0 deletions src/type/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,3 +173,11 @@ export interface PriceUnitByMenuItem {
menu_item_id: number;
price_unit: string;
}

export interface PackagingInfo {
image_url: string;
name: TextByLang[];
description: TextByLang[];
price: number;
currency: string;
}

0 comments on commit 148ef1c

Please sign in to comment.