RemoveCartLineItem
Implements RemoveCartLineItem
Unified Method.
Source
/* eslint-disable max-statements */
import { getCartFromContext, defineApi } from "@vsf-enterprise/unified-api-bigcommerce";
import { findCartItemById, getNormalizedCart } from "@vsf-enterprise/unified-api-bigcommerce";
import { CartIncludeEnum } from "@vsf-enterprise/bigcommerce-api";
export const removeCartLineItem = defineApi.removeCartLineItem(async (context, args) => {
if (args?.cartId != null) {
throw { statusCode: 400, message: "Multiple carts feature is not available." };
}
const cart = await getCartFromContext(context);
const { lineItemId } = args;
const cartId = cart.id as string;
const isLineItemFound = findCartItemById(cart, lineItemId);
if (!isLineItemFound) {
throw new Error(`Line item with id ${lineItemId} not found in the cart`);
}
const { data: updatedCart } = await context.api.removeCartItem({
cartId,
itemId: lineItemId,
include: CartIncludeEnum.LineItemsPhysicalItemsOptions,
});
if (updatedCart) {
return await getNormalizedCart(context, updatedCart);
}
const createdCart = await context.api.createCart({
data: {
line_items: [],
},
});
return await getNormalizedCart(context, createdCart.data);
});