-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* setup cart module, controller & service, complete the add-to-cart route * add entity CartItem; interpret advanced taste customization --------- Co-authored-by: NHT <[email protected]>
- Loading branch information
1 parent
af6bae8
commit 7218533
Showing
10 changed files
with
304 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { | ||
Column, | ||
CreateDateColumn, | ||
Entity, | ||
PrimaryGeneratedColumn, | ||
} from 'typeorm'; | ||
|
||
@Entity('Cart_Item') | ||
export class CartItem { | ||
@PrimaryGeneratedColumn() | ||
public item_id: number; | ||
|
||
@Column({ type: 'int', unique: false, nullable: false }) | ||
public customer_id: number; | ||
|
||
@Column({ type: 'int', unique: false, nullable: false }) | ||
public sku_id: number; | ||
|
||
@Column({ type: 'int', unique: false, nullable: true }) | ||
public qty_ordered: number; | ||
|
||
@Column({ type: 'varchar', length: 255, unique: false, nullable: true }) | ||
public advanced_taste_customization: string; | ||
|
||
@Column({ type: 'varchar', length: 255, unique: false, nullable: true }) | ||
public basic_taste_customization: string; | ||
|
||
@Column({ type: 'varchar', length: 255, unique: false, nullable: true }) | ||
public portion_customization: string; | ||
|
||
@Column({ type: 'text', unique: false, nullable: true }) | ||
public advanced_taste_customization_obj: string; | ||
|
||
@Column({ type: 'text', unique: false, nullable: true }) | ||
public basic_taste_customization_obj: string; | ||
|
||
@Column({ type: 'text', unique: false, nullable: true }) | ||
public notes: string; | ||
|
||
@Column({ type: 'int', unique: false, nullable: true }) | ||
public restaurant_id: number; | ||
|
||
@CreateDateColumn({ | ||
type: 'datetime', | ||
nullable: false, | ||
unique: false, | ||
default: () => 'CURRENT_TIMESTAMP', | ||
}) | ||
public created_at: Date; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { Controller, Inject } from '@nestjs/common'; | ||
import { CartService } from './cart.service'; | ||
import { FlagsmithService } from 'src/dependency/flagsmith/flagsmith.service'; | ||
import { MessagePattern } from '@nestjs/microservices'; | ||
import { AddToCartRequest } from './dto/add-to-cart-request.dto'; | ||
import { AddToCartResponse } from './dto/add-to-cart-response.dto'; | ||
|
||
@Controller() | ||
export class CartController { | ||
constructor( | ||
@Inject('FLAGSMITH_SERVICE') private readonly flagService: FlagsmithService, | ||
private readonly cartService: CartService, | ||
) {} | ||
@MessagePattern({ cmd: 'add_cart_item' }) | ||
async addCartItem(data: AddToCartRequest): Promise<AddToCartResponse> { | ||
if (this.flagService.isFeatureEnabled('fes-24-add-to-cart')) { | ||
return await this.cartService.addCartItem(data); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { CartController } from './cart.controller'; | ||
import { CartService } from './cart.service'; | ||
import { TypeOrmModule } from '@nestjs/typeorm'; | ||
import { CartItem } from 'src/entity/cart-item.entity'; | ||
|
||
@Module({ | ||
imports: [TypeOrmModule.forFeature([CartItem])], | ||
controllers: [CartController], | ||
providers: [CartService], | ||
exports: [CartService], | ||
}) | ||
export class CartModule {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import { Inject, Injectable } from '@nestjs/common'; | ||
import { InjectEntityManager } from '@nestjs/typeorm'; | ||
import { FlagsmithService } from 'src/dependency/flagsmith/flagsmith.service'; | ||
import { EntityManager } from 'typeorm'; | ||
import { AddToCartResponse } from './dto/add-to-cart-response.dto'; | ||
import { AddToCartRequest } from './dto/add-to-cart-request.dto'; | ||
import { CartItem } from 'src/entity/cart-item.entity'; | ||
import { CommonService } from '../common/common.service'; | ||
|
||
@Injectable() | ||
export class CartService { | ||
constructor( | ||
@Inject('FLAGSMITH_SERVICE') private readonly flagService: FlagsmithService, | ||
@InjectEntityManager() private readonly entityManager: EntityManager, | ||
private readonly commonService: CommonService, | ||
) {} | ||
async addCartItem(inputData: AddToCartRequest): Promise<AddToCartResponse> { | ||
if (this.flagService.isFeatureEnabled('fes-24-add-to-cart')) { | ||
const res = new AddToCartResponse(200, ''); | ||
|
||
const { | ||
customer_id, | ||
sku_id, | ||
qty_ordered, | ||
advanced_taste_customization_obj, | ||
basic_taste_customization_obj, | ||
notes, | ||
} = inputData; | ||
|
||
//Get the current cart | ||
const cart = await this.entityManager | ||
.createQueryBuilder(CartItem, 'cart') | ||
.where('cart.customer_id = :customer_id', { customer_id }) | ||
.getMany(); | ||
|
||
//Interpret Advance Taste Customization | ||
const advanced_taste_customization = | ||
await this.commonService.interpretAdvanceTaseCustomization( | ||
advanced_taste_customization_obj, | ||
); | ||
console.log('advanced_taste_customization', advanced_taste_customization); | ||
|
||
//If cart is empty, create a new cart | ||
// if (cart.length === 0) { | ||
// const item = await this.entityManager | ||
// .createQueryBuilder() | ||
// .insert() | ||
// .into(CartItem) | ||
// .values({ | ||
// customer_id: customer_id, | ||
// sku_id: sku_id, | ||
// qty_ordered: qty_ordered, | ||
// advanced_taste_customization: '', | ||
// basic_taste_customization: '', | ||
// portion_customization: '', | ||
// advanced_taste_customization_obj: JSON.stringify( | ||
// advanced_taste_customization_obj, | ||
// ), | ||
// basic_taste_customization_obj: JSON.stringify( | ||
// basic_taste_customization_obj, | ||
// ), | ||
// notes: notes, | ||
// restaurant_id: null, | ||
// }) | ||
// .execute(); | ||
// } | ||
|
||
//success | ||
res.statusCode = 200; | ||
// res.message = 'Add to cart successfully'; | ||
res.message = cart; | ||
res.data = null; | ||
|
||
return res; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
export class AddToCartRequest { | ||
customer_id: number; | ||
sku_id: number; | ||
qty_ordered: number; | ||
advanced_taste_customization_obj: OptionSelection[]; | ||
basic_taste_customization_obj: OptionSelection[]; | ||
notes: string; | ||
} | ||
|
||
interface OptionSelection { | ||
option_id: string; | ||
value_id: string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { GeneralResponse } from 'src/dto/general-response.dto'; | ||
|
||
export class AddToCartResponse extends GeneralResponse { | ||
data: AddToCartResponseData; | ||
} | ||
|
||
interface AddToCartResponseData { | ||
restaurant_id: number; | ||
customer_id: number; | ||
cart_info: CartItem[]; | ||
} | ||
|
||
interface CartItem { | ||
item_id: number; | ||
sku_id: number; | ||
customer_id: number; | ||
qty_ordered: number; | ||
advanced_taste_customization: string; | ||
basic_taste_customization: string; | ||
portion_customization: string; | ||
advanced_taste_customization_obj: OptionSelection[]; | ||
basic_taste_customization_obj: OptionSelection[]; | ||
notes: string; | ||
restaurant_id: number; | ||
created_at: Date; | ||
} | ||
|
||
interface OptionSelection { | ||
option_id: string; | ||
value_id: string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters