From e68fc34bb4623946a15a1a6c136a9a901ccecc22 Mon Sep 17 00:00:00 2001 From: nfesta2023 <142601504+nfesta2023@users.noreply.github.com> Date: Thu, 25 Jan 2024 11:09:47 +0700 Subject: [PATCH] fes-36-delete-whole-cart (#41) * add logic to delete the whole cart Co-authored-by: NHT --- src/feature/cart/cart.controller.ts | 26 ++++++++++++++++++++++++++ src/feature/cart/cart.service.ts | 11 +++++++++++ 2 files changed, 37 insertions(+) diff --git a/src/feature/cart/cart.controller.ts b/src/feature/cart/cart.controller.ts index f43e7f1..eddb5af 100644 --- a/src/feature/cart/cart.controller.ts +++ b/src/feature/cart/cart.controller.ts @@ -10,6 +10,7 @@ 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 { GeneralResponse } from 'src/dto/general-response.dto'; @Controller() export class CartController { @@ -175,4 +176,29 @@ export class CartController { return res; } } + + @MessagePattern({ cmd: 'delete_all_cart_item' }) + async deleteAllCartItem(customer_id: number): Promise { + if (this.flagService.isFeatureEnabled('fes-36-delete-whole-cart')) { + const res = new GeneralResponse(200, ''); + try { + await this.cartService.deleteAllCartItem(customer_id); + res.statusCode = 200; + res.message = 'Delete all cart items successfully'; + res.data = null; + } 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; + } + } } diff --git a/src/feature/cart/cart.service.ts b/src/feature/cart/cart.service.ts index 6f2aab0..85cdd78 100644 --- a/src/feature/cart/cart.service.ts +++ b/src/feature/cart/cart.service.ts @@ -573,4 +573,15 @@ export class CartService { ); } } + + async deleteAllCartItem(customer_id: number) { + if (this.flagService.isFeatureEnabled('fes-36-delete-whole-cart')) { + await this.entityManager + .createQueryBuilder() + .delete() + .from(CartItem) + .where('customer_id = :customer_id', { customer_id }) + .execute(); + } + } }