diff --git a/src/feature/cart/cart.controller.ts b/src/feature/cart/cart.controller.ts index 64fb8c3..3484f76 100644 --- a/src/feature/cart/cart.controller.ts +++ b/src/feature/cart/cart.controller.ts @@ -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() @@ -177,6 +179,39 @@ export class CartController { } } // end of updateCartBasic + @MessagePattern({ cmd: 'delete_cart_items' }) + async deleteCartItems( + requestData: DeleteCartItemRequest, + ): Promise { + 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 { if (this.flagService.isFeatureEnabled('fes-36-delete-whole-cart')) { @@ -197,7 +232,6 @@ export class CartController { res.data = null; } } - return res; } } // end of deleteAllCartItem diff --git a/src/feature/cart/cart.service.ts b/src/feature/cart/cart.service.ts index 17f60a5..c547ea1 100644 --- a/src/feature/cart/cart.service.ts +++ b/src/feature/cart/cart.service.ts @@ -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 @@ -585,4 +584,40 @@ export class CartService { .execute(); } } // end of deleteAllCartItem + + async deleteCartItemsFromEndPoint( + customer_id: number, + item_ids: number[], + ): Promise { + 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 { + 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 } diff --git a/src/feature/cart/dto/delete-cart-item-request.dto.ts b/src/feature/cart/dto/delete-cart-item-request.dto.ts new file mode 100644 index 0000000..f77071b --- /dev/null +++ b/src/feature/cart/dto/delete-cart-item-request.dto.ts @@ -0,0 +1,4 @@ +export class DeleteCartItemRequest { + customer_id: number; + cart_items: number[]; +} diff --git a/src/feature/cart/dto/delete-cart-item-response.dto.ts b/src/feature/cart/dto/delete-cart-item-response.dto.ts new file mode 100644 index 0000000..8712720 --- /dev/null +++ b/src/feature/cart/dto/delete-cart-item-response.dto.ts @@ -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; +}