Skip to content

Commit

Permalink
fes-37-delete-some-of-cart-items (#42)
Browse files Browse the repository at this point in the history
* add dto, add logic

* add comment to fix the merge issue

* fix merge issue 2

---------

Co-authored-by: NHT <[email protected]>
  • Loading branch information
nfesta2023 and hoangtuan910 authored Jan 25, 2024
1 parent d3e271b commit b11ea18
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 2 deletions.
36 changes: 35 additions & 1 deletion src/feature/cart/cart.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import { GetCartDetailResponse } from './dto/get-cart-detail-response.dto';
import { CartItem } from 'src/entity/cart-item.entity';
import { UpdateCartBasicRequest } from './dto/update-cart-basic-request.dto';
import { UpdateCartBasicResponse } from './dto/update-cart-basic-response.dto';
import { DeleteCartItemRequest } from './dto/delete-cart-item-request.dto';
import { DeleteCartItemResponse } from './dto/delete-cart-item-response.dto';
import { GeneralResponse } from 'src/dto/general-response.dto';

@Controller()
Expand Down Expand Up @@ -177,6 +179,39 @@ export class CartController {
}
} // end of updateCartBasic

@MessagePattern({ cmd: 'delete_cart_items' })
async deleteCartItems(
requestData: DeleteCartItemRequest,
): Promise<DeleteCartItemResponse> {
if (this.flagService.isFeatureEnabled('fes-37-delete-some-of-cart-items')) {
const { customer_id, cart_items } = requestData;
const res = new DeleteCartItemResponse(200, '');
try {
const cart = await this.cartService.deleteCartItemsFromEndPoint(
customer_id,
cart_items,
);
res.statusCode = 200;
res.message = 'Delete cart items successfully';
res.data = {
customer_id: customer_id,
cart_info: cart,
};
} catch (error) {
if (error instanceof HttpException) {
res.statusCode = error.getStatus();
res.message = error.getResponse();
res.data = null;
} else {
res.statusCode = 500;
res.message = error.toString();
res.data = null;
}
}
return res;
}
} // end of deleteCartItems

@MessagePattern({ cmd: 'delete_all_cart_item' })
async deleteAllCartItem(customer_id: number): Promise<GeneralResponse> {
if (this.flagService.isFeatureEnabled('fes-36-delete-whole-cart')) {
Expand All @@ -197,7 +232,6 @@ export class CartController {
res.data = null;
}
}

return res;
}
} // end of deleteAllCartItem
Expand Down
37 changes: 36 additions & 1 deletion src/feature/cart/cart.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -574,7 +574,6 @@ export class CartService {
);
}
} //end of massUpdateCartItemWithQuantity

async deleteAllCartItem(customer_id: number) {
if (this.flagService.isFeatureEnabled('fes-36-delete-whole-cart')) {
await this.entityManager
Expand All @@ -585,4 +584,40 @@ export class CartService {
.execute();
}
} // end of deleteAllCartItem

async deleteCartItemsFromEndPoint(
customer_id: number,
item_ids: number[],
): Promise<CartItem[]> {
if (this.flagService.isFeatureEnabled('fes-37-delete-some-of-cart-items')) {
//Check if the item_ids belongs to the customers
const mentionedCartItems = await this.getCartByItemId(
item_ids,
customer_id,
);
if (mentionedCartItems.length != item_ids.length) {
throw new HttpException(
'Some of cart items do not belong to the customer or not exist',
404,
);
}

//Delete the cart items
await this.deleteCartItems(item_ids);

//Get the cart again after deleting the cart items
return await this.getCart(customer_id);
}
} // end of deleteCartItemsFromEndPoint

async deleteCartItems(item_ids: number[]): Promise<void> {
if (this.flagService.isFeatureEnabled('fes-37-delete-some-of-cart-items')) {
await this.entityManager
.createQueryBuilder()
.delete()
.from(CartItem)
.whereInIds(item_ids)
.execute();
}
} // end of deleteCartItems
}
4 changes: 4 additions & 0 deletions src/feature/cart/dto/delete-cart-item-request.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export class DeleteCartItemRequest {
customer_id: number;
cart_items: number[];
}
24 changes: 24 additions & 0 deletions src/feature/cart/dto/delete-cart-item-response.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { GeneralResponse } from 'src/dto/general-response.dto';

export class DeleteCartItemResponse extends GeneralResponse {
data: Data;
}
interface Data {
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: string;
basic_taste_customization_obj: string;
notes: string;
restaurant_id: number;
created_at: Date;
}

0 comments on commit b11ea18

Please sign in to comment.