Skip to content

Commit

Permalink
FES-77-implement-packaging-change (#63)
Browse files Browse the repository at this point in the history
* update logic, entities and dtos
Co-authored-by: NHT <[email protected]>
  • Loading branch information
nfesta2023 authored Mar 7, 2024
1 parent 2ed72a6 commit 47b2176
Show file tree
Hide file tree
Showing 18 changed files with 184 additions and 13 deletions.
2 changes: 2 additions & 0 deletions src/dto/food-detail.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,11 @@ class BasicCustomization {
description: TextByLang[];
}
interface PackagingInfo {
packaging_id: number;
image_url: string;
name: TextByLang[];
description: TextByLang[];
price: number;
currency: string;
is_default: boolean;
}
11 changes: 11 additions & 0 deletions src/entity/cart-item.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@ import {
Column,
CreateDateColumn,
Entity,
JoinColumn,
ManyToOne,
PrimaryGeneratedColumn,
} from 'typeorm';
import { Packaging } from './packaging.entity';

@Entity('Cart_Item')
export class CartItem {
Expand Down Expand Up @@ -40,11 +43,19 @@ export class CartItem {
@Column({ type: 'int', unique: false, nullable: true })
public restaurant_id: number;

@Column({ type: 'int', unique: false, nullable: true })
public packaging_id: 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;
}
3 changes: 3 additions & 0 deletions src/entity/menuitem-packaging.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ export class MenuItemPackaging {
@Column({ type: 'int', nullable: false })
public image: number;

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

@CreateDateColumn({
type: 'datetime',
nullable: false,
Expand Down
10 changes: 0 additions & 10 deletions src/entity/packaging.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,8 @@ import {
CreateDateColumn,
PrimaryGeneratedColumn,
Column,
ManyToOne,
JoinColumn,
OneToMany,
} from 'typeorm';
import { MenuItem } from './menu-item.entity';
import { PackagingExt } from './packaging-ext.entity';
import { Media } from './media.entity';

Expand Down Expand Up @@ -38,13 +35,6 @@ export class Packaging {

//RELATIONSHIPS

// @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[];

Expand Down
17 changes: 17 additions & 0 deletions src/feature/cart/cart.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ export class CartController {
advanced_taste_customization_obj,
basic_taste_customization_obj,
notes,
lang,
packaging_id,
} = data;
const res = new AddToCartResponse(200, '');
try {
Expand All @@ -46,6 +48,7 @@ export class CartController {
advanced_taste_customization_obj,
basic_taste_customization_obj,
notes,
packaging_id,
);

let restaurant: RestaurantBasicInfo = {
Expand Down Expand Up @@ -138,6 +141,7 @@ export class CartController {
basic_taste_customization_obj,
notes,
lang = 'vie',
packaging_id = null,
} = data;
const res = new UpdateCartAdvancedResponse(200, '');
try {
Expand All @@ -150,6 +154,7 @@ export class CartController {
advanced_taste_customization_obj,
basic_taste_customization_obj,
notes,
packaging_id,
lang,
);
let restaurant: RestaurantBasicInfo = {
Expand Down Expand Up @@ -365,13 +370,25 @@ export class CartController {
return res;
}

//Get standard package for menu_item_id
let packaging_id = null;
const packaging =
await this.commonService.getStandardPackagingByMenuItem(menu_item_id);
if (packaging) {
packaging_id = packaging.packaging_id;
}

const qtyOrdered = 1;

try {
const cart = await this.cartService.addCartItem(
customer_id,
sku.sku_id,
qtyOrdered,
[],
[],
'',
packaging_id,
);

let restaurant: RestaurantBasicInfo = {
Expand Down
71 changes: 68 additions & 3 deletions src/feature/cart/cart.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { CommonService } from '../common/common.service';
import { SKU } from 'src/entity/sku.entity';
import {
BasicTasteSelection,
CartPackagingInfo,
DayShift,
FullCartItem,
OptionSelection,
Expand All @@ -18,6 +19,7 @@ import {
import { DAY_ID, DAY_NAME } from 'src/constant';
import { Shift } from 'src/enum';
import { AhamoveService } from 'src/dependency/ahamove/ahamove.service';
import { MenuItemPackaging } from 'src/entity/menuitem-packaging.entity';

@Injectable()
export class CartService {
Expand All @@ -34,6 +36,7 @@ export class CartService {
advanced_taste_customization_obj: OptionSelection[] = [],
basic_taste_customization_obj: BasicTasteSelection[] = [],
notes: string = '',
packaging_id: number = null,
lang: string = 'vie',
): Promise<FullCartItem[]> {
const advanced_taste_customization_obj_txt =
Expand All @@ -55,6 +58,16 @@ export class CartService {
throw new HttpException('SKU does not exist', 404);
}

//Check the package info does belongs to the SKU
if (packaging_id) {
if (!(await this.checkIfThePackageBelongsToSKU(packaging_id, sku_id))) {
throw new HttpException(
'The package info does belongs to the SKU',
400,
);
}
}

// Check if the advanced_taste_customization_obj is all available to this SKU
const advancedTasteCustomizationValidation =
advanced_taste_customization_obj.length > 0
Expand Down Expand Up @@ -120,6 +133,7 @@ export class CartService {
basic_taste_customization_obj_txt,
notes,
sku.menu_item.restaurant_id,
packaging_id,
);
return await this.getCart(customer_id);
}
Expand All @@ -141,7 +155,8 @@ export class CartService {
i.advanced_taste_customization_obj ==
advanced_taste_customization_obj_txt &&
i.basic_taste_customization_obj == basic_taste_customization_obj_txt &&
i.notes == notes,
i.notes == notes &&
i.packaging_info.packaging_id == packaging_id,
);
if (existingItem) {
//The item does exist => increase the quantity
Expand All @@ -157,6 +172,7 @@ export class CartService {
existingItem.basic_taste_customization_obj,
existingItem.notes,
existingItem.restaurant_id,
existingItem.packaging_info.packaging_id,
);
return await this.getCart(customer_id);
}
Expand All @@ -172,6 +188,7 @@ export class CartService {
basic_taste_customization_obj_txt,
notes,
sku.menu_item.restaurant_id,
packaging_id,
);
return await this.getCart(customer_id);
} // end of addCartItem
Expand All @@ -180,6 +197,8 @@ export class CartService {
const fullCart: FullCartItem[] = [];
const cartItems = await this.entityManager
.createQueryBuilder(CartItem, 'cart')
.leftJoinAndSelect('cart.packaging_obj', 'packaging')
.leftJoinAndSelect('packaging.packaging_ext_obj', 'packagingExt')
.where('cart.customer_id = :customer_id', { customer_id })
.getMany();

Expand All @@ -192,6 +211,17 @@ export class CartService {
const additionalInfoForSku = additionalInfoForSkus.find(
(i) => i.sku_id == item.sku_id,
);
const packagingInfo: CartPackagingInfo = {
packaging_id: item.packaging_id,
name: [],
price: item.packaging_obj?.price,
};
item.packaging_obj?.packaging_ext_obj.forEach((ext) => {
packagingInfo.name.push({
ISO_language_code: ext.ISO_language_code,
text: ext.name,
});
});
const fullItem: FullCartItem = {
item_id: item.item_id,
item_name: additionalInfoForSku.sku_name,
Expand All @@ -209,6 +239,7 @@ export class CartService {
basic_taste_customization_obj: item.basic_taste_customization_obj,
notes: item.notes,
restaurant_id: item.restaurant_id,
packaging_info: packagingInfo,
};
fullCart.push(fullItem);
}
Expand All @@ -226,6 +257,7 @@ export class CartService {
basic_taste_customization_obj: string,
notes: string,
restaurant_id: number,
packaging_id: number,
): Promise<void> {
await this.entityManager
.createQueryBuilder()
Expand All @@ -242,6 +274,7 @@ export class CartService {
basic_taste_customization_obj: basic_taste_customization_obj,
notes: notes,
restaurant_id: restaurant_id,
packaging_id: packaging_id,
})
.execute();
} // end of insertCart
Expand All @@ -258,6 +291,7 @@ export class CartService {
basic_taste_customization_obj: string,
notes: string,
restaurant_id: number,
packaging_id: number,
): Promise<void> {
await this.entityManager
.createQueryBuilder()
Expand All @@ -273,6 +307,7 @@ export class CartService {
basic_taste_customization_obj: basic_taste_customization_obj,
notes: notes,
restaurant_id: restaurant_id,
packaging_id: packaging_id,
})
.where('item_id = :item_id', { item_id })
.execute();
Expand All @@ -286,6 +321,7 @@ export class CartService {
advanced_taste_customization_obj: OptionSelection[],
basic_taste_customization_obj: BasicTasteSelection[],
notes: string,
packaging_id: number,
lang: string,
): Promise<FullCartItem[]> {
// https://n-festa.atlassian.net/browse/FES-28
Expand All @@ -301,6 +337,13 @@ export class CartService {
);
}

// Check if the package does belong to SKU
if (packaging_id) {
if (!(await this.checkIfThePackageBelongsToSKU(packaging_id, sku_id))) {
throw new HttpException('The package does not belong to SKU', 400);
}
}

const advanced_taste_customization_obj_txt = JSON.stringify(
advanced_taste_customization_obj,
);
Expand All @@ -316,7 +359,8 @@ export class CartService {
mentionedCartItem.advanced_taste_customization_obj &&
basic_taste_customization_obj_txt ==
mentionedCartItem.basic_taste_customization_obj &&
notes == mentionedCartItem.notes
notes == mentionedCartItem.notes &&
packaging_id == mentionedCartItem.packaging_id
) {
throw new HttpException('No new data for updating', 400);
}
Expand All @@ -331,7 +375,7 @@ export class CartService {
throw new HttpException('SKU does not exist', 404);
}

// If there is any cart item which is IDENTICAL to the updated item
// If there is any other cart item which is IDENTICAL to the updated item
const identicalCartItem = await this.entityManager
.createQueryBuilder(CartItem, 'cart')
.where('cart.customer_id = :customer_id', { customer_id })
Expand All @@ -349,6 +393,7 @@ export class CartService {
{ basic_taste_customization_obj: basic_taste_customization_obj_txt },
)
.andWhere('cart.notes = :notes', { notes })
.andWhere('cart.packaging_id = :packaging_id', { packaging_id })
.getOne();

if (identicalCartItem) {
Expand Down Expand Up @@ -484,6 +529,7 @@ export class CartService {
basic_taste_customization_obj_txt,
notes,
mentionedCartItem.restaurant_id,
packaging_id,
);

return await this.getCart(customer_id);
Expand Down Expand Up @@ -913,4 +959,23 @@ export class CartService {
}
return timeSlots;
} // end of getAvailableDeliveryTimeFromEndPoint

async checkIfThePackageBelongsToSKU(
packaging_id: number,
sku_id: number,
): Promise<boolean> {
let result = false;

const record = await this.entityManager
.createQueryBuilder(MenuItemPackaging, 'packaging')
.leftJoinAndSelect('packaging.menu_item_obj', 'menuItem')
.leftJoinAndSelect('menuItem.skus', 'sku')
.where('packaging.packaging_id = :packaging_id', { packaging_id })
.andWhere('sku.sku_id = :sku_id', { sku_id })
.getOne();

result = record ? true : false;

return result;
}
}
1 change: 1 addition & 0 deletions src/feature/cart/dto/add-to-cart-request.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export class AddToCartRequest {
basic_taste_customization_obj: BasicTasteSelection[];
notes: string;
lang?: string;
packaging_id?: number;
}

interface OptionSelection {
Expand Down
7 changes: 7 additions & 0 deletions src/feature/cart/dto/add-to-cart-response.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,11 @@ interface FullCartItem {
basic_taste_customization_obj: string;
notes: string;
restaurant_id: number;
packaging_info: CartPackagingInfo;
}

interface CartPackagingInfo {
packaging_id: number;
name: TextByLang[];
price: number;
}
7 changes: 7 additions & 0 deletions src/feature/cart/dto/delete-cart-item-response.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,11 @@ interface FullCartItem {
basic_taste_customization_obj: string;
notes: string;
restaurant_id: number;
packaging_info: CartPackagingInfo;
}

interface CartPackagingInfo {
packaging_id: number;
name: TextByLang[];
price: number;
}
Loading

0 comments on commit 47b2176

Please sign in to comment.