Skip to content

Commit

Permalink
[hotfix]Build failed (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
MinhhTien authored Jan 28, 2024
1 parent 7a1e9b7 commit 602305e
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 99 deletions.
28 changes: 14 additions & 14 deletions src/cart/controllers/cart.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
// }
}
170 changes: 85 additions & 85 deletions src/cart/services/cart.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
// }
}

0 comments on commit 602305e

Please sign in to comment.