From 602305ea863f6fa3e288dce2e9da2d90c31b59d4 Mon Sep 17 00:00:00 2001 From: MinhhTien <92145479+MinhhTien@users.noreply.github.com> Date: Mon, 29 Jan 2024 00:57:42 +0700 Subject: [PATCH] [hotfix]Build failed (#17) --- src/cart/controllers/cart.controller.ts | 28 ++-- src/cart/services/cart.service.ts | 170 ++++++++++++------------ 2 files changed, 99 insertions(+), 99 deletions(-) diff --git a/src/cart/controllers/cart.controller.ts b/src/cart/controllers/cart.controller.ts index 178c565..e53069d 100644 --- a/src/cart/controllers/cart.controller.ts +++ b/src/cart/controllers/cart.controller.ts @@ -20,19 +20,19 @@ import { Cart } from '@cart/schemas/cart.schema' export class CartController { constructor(private readonly cartService: CartService) {} - @Post() - @ApiOkResponse({ type: SuccessDataResponse }) - @ApiBadRequestResponse({ type: ErrorResponse }) - async addToCart(@Req() req, @Body() addToCartDto: AddToCartDto) { - addToCartDto.customerId = _.get(req, 'user._id') - const result = await this.cartService.addToCart(addToCartDto) - return result - } + // @Post() + // @ApiOkResponse({ type: SuccessDataResponse }) + // @ApiBadRequestResponse({ type: ErrorResponse }) + // async addToCart(@Req() req, @Body() addToCartDto: AddToCartDto) { + // addToCartDto.customerId = _.get(req, 'user._id') + // const result = await this.cartService.addToCart(addToCartDto) + // return result + // } - @Get() - @ApiOkResponse({ type: DataResponse(Cart) }) - async getListCard(@Req() req) { - const customerId = _.get(req, 'user._id') - return await this.cartService.getListCard(customerId) - } + // @Get() + // @ApiOkResponse({ type: DataResponse(Cart) }) + // async getListCard(@Req() req) { + // const customerId = _.get(req, 'user._id') + // return await this.cartService.getListCard(customerId) + // } } diff --git a/src/cart/services/cart.service.ts b/src/cart/services/cart.service.ts index d508823..8c57d77 100644 --- a/src/cart/services/cart.service.ts +++ b/src/cart/services/cart.service.ts @@ -11,95 +11,95 @@ import { Types } from 'mongoose' export class CartService { constructor(private readonly cartRepository: CartRepository, private readonly productRepository: ProductRepository) {} - public async addToCart(addToCartDto: AddToCartDto) { - const { customerId, productId, quantity } = addToCartDto + // public async addToCart(addToCartDto: AddToCartDto) { + // const { customerId, productId, quantity } = addToCartDto - // 1.Check exists productId, implement after product module done - const product = await this.productRepository.findOne({ - conditions: { - _id: productId - } - }) - if (!product) throw new AppException(Errors.OBJECT_NOT_FOUND) + // // 1.Check exists productId, implement after product module done + // const product = await this.productRepository.findOne({ + // conditions: { + // _id: productId + // } + // }) + // if (!product) throw new AppException(Errors.OBJECT_NOT_FOUND) - // 2. Fetch cart - const cart = await this.cartRepository.findOne({ - conditions: { - customerId - } - }) + // // 2. Fetch cart + // const cart = await this.cartRepository.findOne({ + // conditions: { + // customerId + // } + // }) - const { quantity: remainQuantity, price } = product - const amount = price * quantity + // const { quantity: remainQuantity, price } = product + // const amount = price * quantity - if (!cart) { - // 3.1 Cart not existed - // Check inventory quantity <= product.quantity - if (quantity > remainQuantity) throw new AppException(Errors.NOT_ENOUGH_QUANTITY_IN_STOCK) - // Create new cart - await this.cartRepository.create({ - customerId, - items: [{ productId: product._id, quantity }], - totalAmount: amount - }) - } else { - const { _id, items } = cart - // 3.2 Cart existed, update cart - // Check existed item - const existedItemIndex = items.findIndex((item) => { - return item.productId == productId - }) + // if (!cart) { + // // 3.1 Cart not existed + // // Check inventory quantity <= product.quantity + // if (quantity > remainQuantity) throw new AppException(Errors.NOT_ENOUGH_QUANTITY_IN_STOCK) + // // Create new cart + // await this.cartRepository.create({ + // customerId, + // items: [{ productId: product._id, quantity }], + // totalAmount: amount + // }) + // } else { + // const { _id, items } = cart + // // 3.2 Cart existed, update cart + // // Check existed item + // const existedItemIndex = items.findIndex((item) => { + // return item.productId == productId + // }) - if (existedItemIndex === -1) { - // 3.2.1 Item not existed in cart - // Check inventory quantity <= product.quantity - if (quantity > remainQuantity) throw new AppException(Errors.NOT_ENOUGH_QUANTITY_IN_STOCK) - // Push new item in the first element - items.unshift({ productId: new Types.ObjectId(product._id), quantity }) - } else { - // 3.2.2 Item existed in cart - // Check inventory quantity + previousQuantity <= product.quantity - if (items[existedItemIndex].quantity + quantity > remainQuantity) - throw new AppException(Errors.NOT_ENOUGH_QUANTITY_IN_STOCK) - // Update quantity in existed item - items[existedItemIndex].quantity += quantity - } - const totalAmount = (cart.totalAmount += amount) - await this.cartRepository.findOneAndUpdate( - { - _id - }, - { - items, - totalAmount - } - ) - } - return new SuccessResponse(true) - } + // if (existedItemIndex === -1) { + // // 3.2.1 Item not existed in cart + // // Check inventory quantity <= product.quantity + // if (quantity > remainQuantity) throw new AppException(Errors.NOT_ENOUGH_QUANTITY_IN_STOCK) + // // Push new item in the first element + // items.unshift({ productId: new Types.ObjectId(product._id), quantity }) + // } else { + // // 3.2.2 Item existed in cart + // // Check inventory quantity + previousQuantity <= product.quantity + // if (items[existedItemIndex].quantity + quantity > remainQuantity) + // throw new AppException(Errors.NOT_ENOUGH_QUANTITY_IN_STOCK) + // // Update quantity in existed item + // items[existedItemIndex].quantity += quantity + // } + // const totalAmount = (cart.totalAmount += amount) + // await this.cartRepository.findOneAndUpdate( + // { + // _id + // }, + // { + // items, + // totalAmount + // } + // ) + // } + // return new SuccessResponse(true) + // } - public async getListCard(customerId: string) { - const cartList = await this.cartRepository.findOne({ - conditions: { customerId }, - projection: { - _id: 1, - items: 1, - totalAmount: 1 - }, - populates: [ - { - path: 'items.product' - } - ] - }) - if (!cartList) { - const newCartList = await this.cartRepository.create({ - customerId, - items: [], - totalAmount: 0 - }) - return { _id: newCartList._id, items: [], totalAmount: 0 } - } - return cartList - } + // public async getListCard(customerId: string) { + // const cartList = await this.cartRepository.findOne({ + // conditions: { customerId }, + // projection: { + // _id: 1, + // items: 1, + // totalAmount: 1 + // }, + // populates: [ + // { + // path: 'items.product' + // } + // ] + // }) + // if (!cartList) { + // const newCartList = await this.cartRepository.create({ + // customerId, + // items: [], + // totalAmount: 0 + // }) + // return { _id: newCartList._id, items: [], totalAmount: 0 } + // } + // return cartList + // } }