Add endpoint to clear user cart

master
nicekid1 1 month ago
parent 9e84860f48
commit 04716f6973
  1. 17
      src/cart/cart.controller.ts
  2. 7
      src/cart/cart.service.ts

@ -10,21 +10,21 @@ import { Invoice } from "src/invoice/entities/invoice.entity";
export class CartController {
constructor(private readonly cartService: CartService) {}
//create and a item to cart by user
@UseGuards(JwtAuthGuard)
@Post()
async createAndAddItemToCart(@Body() addToCartDto: AddToCartDto, @Request() req: any): Promise<{ message: string; cartItem: Cart }> {
const userId = req.user.id;
return this.cartService.createAndAddItemToCart({ ...addToCartDto, userId });
}
//get user cart items
@UseGuards(JwtAuthGuard)
@Get()
async getUserOpenCart(@Request() req: any): Promise<{ cartItems: Cart[]; totalPrice: number }> {
const userId = req.user.id;
return this.cartService.getUserOpenCart(userId);
}
//edit quantity an item in cart by user
@UseGuards(JwtAuthGuard)
@Patch(":productId")
async updateCart(@Param("productId") productId: number, @Body() updateCartDto: UpdateCartDto, @Request() req: any): Promise<{ message: string; updatedCart: Cart }> {
@ -35,14 +35,21 @@ export class CartController {
updatedCart,
};
}
//delete an item from cart by user
@UseGuards(JwtAuthGuard)
@Delete(":productId")
async removeFromCart(@Param("productId") productId: number, @Request() req: any) {
const userId = req.user.id;
return await this.cartService.removeFromCart(userId, productId);
}
//clear whole cart by user
@UseGuards(JwtAuthGuard)
@Get("clear-cart")
async clearCart(@Request() req: any) {
const userId = req.user.id;
return await this.cartService.clearCart(userId);
}
//get checkout process
@UseGuards(JwtAuthGuard)
@Get("checkout")
async processOrder(@Request() req: any): Promise<{ message: string; invoice: Invoice }> {

@ -125,11 +125,12 @@ export class CartService {
}
}
//delete whole cart
async clearCart(userId: number): Promise<void> {
//delete whole cart by user
async clearCart(userId: number) {
await this.cartModel.destroy({ where: { userId } });
return { message: "cart cleared successful" };
}
//order(clearCart disable)
async processOrder(userId: number, totalAmount: number): Promise<{ message: string; invoice: Invoice }> {
try {

Loading…
Cancel
Save