The Vue Storefront Essentials Course is now available! Learn More
RemoveCartLineItem

RemoveCartLineItem

Implements RemoveCartLineItem Unified Method.

Source

/* eslint-disable max-statements */
import { defineApi, getNormalizerContext } from "@vsf-enterprise/unified-api-commercetools";
import type { Cart } from "@vsf-enterprise/commercetools-types";
import { getNormalizers } from "@vue-storefront/unified-data-model";

export const removeCartLineItem = defineApi.removeCartLineItem(async (context, args) => {
  if (args?.cartId != null) {
    throw { statusCode: 400, message: "Multiple carts feature is not available." };
  }
  const me = await context.api.getMe();
  const cart = me.data?.me.activeCart;

  if (!cart) {
    throw new Error("User has no active cart");
  }

  const lineItem = cart.lineItems.find(({ id }: { id: string }) => id === args.lineItemId);

  if (!lineItem) {
    throw new Error("The line item could not be found in the cart");
  }

  const updatedCart = await context.api.removeFromCart(
    {
      id: cart.id,
      version: cart.version,
    },
    {
      ...lineItem,
      id: args.lineItemId,
    },
  );

  const { normalizeCart } = getNormalizers(context);

  return normalizeCart(updatedCart.data?.cart as Cart, getNormalizerContext(context));
});