Debug issue with getting user cart

master
nicekid1 2 months ago
parent 188723aee7
commit 72665cebda
  1. 6
      src/cart/cart.controller.ts
  2. 6
      src/cart/cart.service.ts
  3. 2
      src/invoice/invoice.service.ts

@ -20,9 +20,9 @@ export class CartController {
@UseGuards(JwtAuthGuard) @UseGuards(JwtAuthGuard)
@Get() @Get()
async getUserCart(@Request() req: any): Promise<{ cartItems: Cart[]; totalPrice: number }> { async getUserOpenCart(@Request() req: any): Promise<{ cartItems: Cart[]; totalPrice: number }> {
const userId = req.user.id; const userId = req.user.id;
return this.cartService.getUserCart(userId); return this.cartService.getUserOpenCart(userId);
} }
@UseGuards(JwtAuthGuard) @UseGuards(JwtAuthGuard)
@ -51,7 +51,7 @@ export class CartController {
async processOrder(@Request() req: any): Promise<{ message: string; invoice: Invoice }> { async processOrder(@Request() req: any): Promise<{ message: string; invoice: Invoice }> {
const userId = req.user.id; const userId = req.user.id;
try { try {
const totalAmount = (await this.cartService.getUserCart(userId)).totalPrice const totalAmount = (await this.cartService.getUserOpenCart(userId)).totalPrice
const result = await this.cartService.processOrder(userId, totalAmount); const result = await this.cartService.processOrder(userId, totalAmount);
return result; return result;
} catch (error) { } catch (error) {

@ -59,15 +59,14 @@ export class CartService {
cartItem: cart, cartItem: cart,
}; };
} }
// Get user's cart // Get user's cart
async getUserCart(userId: number): Promise<{ cartItems: Cart[]; totalPrice: number }> { async getUserOpenCart(userId: number): Promise<{ cartItems: Cart[]; totalPrice: number }> {
if (!userId) { if (!userId) {
throw new HttpException("User ID is required.", HttpStatus.BAD_REQUEST); throw new HttpException("User ID is required.", HttpStatus.BAD_REQUEST);
} }
const cartItems = await this.cartModel.findAll({ const cartItems = await this.cartModel.findAll({
where: { userId }, where: { userId,status:"open" },
include: [ include: [
{ {
model: Product, model: Product,
@ -120,7 +119,6 @@ export class CartService {
async processOrder(userId: number, totalAmount: number): Promise<{ message: string; invoice: Invoice }> { async processOrder(userId: number, totalAmount: number): Promise<{ message: string; invoice: Invoice }> {
try { try {
const carts = await this.cartModel.findAll({ where: { userId, status: "open" } }); const carts = await this.cartModel.findAll({ where: { userId, status: "open" } });
if (!carts || carts.length === 0) { if (!carts || carts.length === 0) {
throw new HttpException("No open carts found for this user.", HttpStatus.NOT_FOUND); throw new HttpException("No open carts found for this user.", HttpStatus.NOT_FOUND);
} }

@ -29,7 +29,7 @@ export class InvoiceService {
throw new HttpException("User not found", HttpStatus.NOT_FOUND); throw new HttpException("User not found", HttpStatus.NOT_FOUND);
} }
const userCartItems = await this.cartService.getUserCart(userId); const userCartItems = await this.cartService.getUserOpenCart(userId);
if (!userCartItems || !userCartItems.cartItems || userCartItems.cartItems.length === 0) { if (!userCartItems || !userCartItems.cartItems || userCartItems.cartItems.length === 0) {
throw new HttpException("Cart is empty", HttpStatus.BAD_REQUEST); throw new HttpException("Cart is empty", HttpStatus.BAD_REQUEST);
} }

Loading…
Cancel
Save