Hi Guys, I created an increment and decrement on ...
# orm-help
m
Hi Guys, I created an increment and decrement on the cartItems but I am trying to remove the item from the cart when the quantity is less than < 1. Here is the code for my mutation does anyone know how this can be achieved?
Copy code
async minusFromCart(parent, args, ctx, info) {
	    // 1. Make sure they are signed in
	    const { userId } = ctx.request;
	    if (!userId) {
	      throw new Error('You must be signed in soooon');
	    }

	    // 2. Query the users current cart
	    const [existingCartItem] = await ctx.db.query.cartItems({
	      where: {
	        user: { id: userId },
	        item: { id: args.id },
	      },
	    });
	    // 3. Check if that item is already in their cart and increment by 1 if it is
	    if (existingCartItem) {
	      console.log('This item is already in their cart');
	      return ctx.db.mutation.updateCartItem(
	        {
	          where: { id: existingCartItem.id },
	          data: { quantity: existingCartItem.quantity - 1 },
	        },
	        info
	      );
	    }

	    //1. Find the cart item
		const cartItem = await ctx.db.query.cartItem(
			{
			where: {
					id: args.id,
				},
			},
		`{ id, user { id }}`
		);

	    if (existingCartItem < 1) {
	      	//3. Delete that cart Item
			return ctx.db.mutation.deleteCartItem ({
				where: { id: args.id },
			}, info);
	    }
	},
@Harshit