From 188723aee7b5fd85579880d81b4012b0d14320cf Mon Sep 17 00:00:00 2001 From: nicekid1 <86746988+nicekid1@users.noreply.github.com> Date: Wed, 8 Jan 2025 12:30:15 +0330 Subject: [PATCH] Refactor checkout route structure for better clarity and performance --- src/cart/cart.controller.ts | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/src/cart/cart.controller.ts b/src/cart/cart.controller.ts index a8a09d5..2d123c4 100644 --- a/src/cart/cart.controller.ts +++ b/src/cart/cart.controller.ts @@ -45,14 +45,13 @@ export class CartController { message: "Product removed from cart successfully", }; } - - @Post(":userId/checkout") - async processOrder(@Param("userId") userId: number, @Body("totalAmount") totalAmount: number): Promise<{ message: string; invoice: Invoice }> { - if (!totalAmount || totalAmount <= 0) { - throw new HttpException("Invalid total amount.", HttpStatus.BAD_REQUEST); - } - + + @UseGuards(JwtAuthGuard) + @Get("checkout") + async processOrder(@Request() req: any): Promise<{ message: string; invoice: Invoice }> { + const userId = req.user.id; try { + const totalAmount = (await this.cartService.getUserCart(userId)).totalPrice const result = await this.cartService.processOrder(userId, totalAmount); return result; } catch (error) {