ApplyCouponToCart
Implements ApplyCouponToCart
Unified Method.
Source
import { defineApi, getCartVersion } from "@vsf-enterprise/unified-api-commercetools";
import { getNormalizers } from "@vsf-enterprise/unified-api-commercetools/udl";
export const applyCouponToCart = defineApi.applyCouponToCart(async (context, args) => {
if (args?.cartId != null) {
throw { statusCode: 400, message: "Multiple carts feature is not available." };
}
const version = await getCartVersion(context);
if (!args.couponCode) {
throw new Error("The provided coupon code is empty and cannot be applied.");
}
const updatedCart = await context.api.applyCartCoupon(version, args.couponCode);
const { normalizeCart } = getNormalizers(context);
return normalizeCart(updatedCart.cart);
});