|
| 1 | +import { Product } from './../../../schema.d' |
| 2 | +import { normalizeCart } from '../../../lib/normalize' |
| 3 | +import type { CartEndpoint } from '.' |
| 4 | +import addToCurrentCartMutation from '../../../api/mutations/addToCart-mutation' |
| 5 | + |
| 6 | +import { getProductQuery } from '../../../api/queries/get-product-query' |
| 7 | +import { getCartQuery } from '../../../api/queries/get-cart-query' |
| 8 | +import CookieHandler from '../../../api/utils/cookie-handler' |
| 9 | + |
| 10 | +const buildAddToCartVariables = ({ |
| 11 | + productId, |
| 12 | + variantId, |
| 13 | + quantity = 1, |
| 14 | + productResponse, |
| 15 | +}: { |
| 16 | + productId: string |
| 17 | + variantId: string |
| 18 | + quantity: number |
| 19 | + productResponse: any |
| 20 | +}) => { |
| 21 | + const { product } = productResponse.data |
| 22 | + |
| 23 | + const selectedOptions = product.variations?.find( |
| 24 | + (v: any) => v.productCode === variantId |
| 25 | + ).options |
| 26 | + |
| 27 | + let options: any[] = [] |
| 28 | + selectedOptions?.forEach((each: any) => { |
| 29 | + product?.options |
| 30 | + .filter((option: any) => { |
| 31 | + return option.attributeFQN == each.attributeFQN |
| 32 | + }) |
| 33 | + .forEach((po: any) => { |
| 34 | + options.push({ |
| 35 | + attributeFQN: po.attributeFQN, |
| 36 | + name: po.attributeDetail.name, |
| 37 | + value: po.values?.find((v: any) => v.value == each.value).value, |
| 38 | + }) |
| 39 | + }) |
| 40 | + }) |
| 41 | + |
| 42 | + return { |
| 43 | + productToAdd: { |
| 44 | + product: { |
| 45 | + productCode: productId, |
| 46 | + variationProductCode: variantId ? variantId : null, |
| 47 | + options, |
| 48 | + }, |
| 49 | + quantity, |
| 50 | + fulfillmentMethod: 'Ship', |
| 51 | + }, |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +const addItem: CartEndpoint['handlers']['addItem'] = async ({ |
| 56 | + req, |
| 57 | + res, |
| 58 | + body: { cartId, item }, |
| 59 | + config, |
| 60 | +}) => { |
| 61 | + if (!item) { |
| 62 | + return res.status(400).json({ |
| 63 | + data: null, |
| 64 | + errors: [{ message: 'Missing item' }], |
| 65 | + }) |
| 66 | + } |
| 67 | + if (!item.quantity) item.quantity = 1 |
| 68 | + |
| 69 | + const productResponse = await config.fetch(getProductQuery, { |
| 70 | + variables: { productCode: item?.productId }, |
| 71 | + }) |
| 72 | + |
| 73 | + const cookieHandler = new CookieHandler(config, req, res) |
| 74 | + let accessToken = null |
| 75 | + |
| 76 | + if (!cookieHandler.getAccessToken()) { |
| 77 | + let anonymousShopperTokenResponse = await cookieHandler.getAnonymousToken() |
| 78 | + accessToken = anonymousShopperTokenResponse.accessToken; |
| 79 | + } else { |
| 80 | + accessToken = cookieHandler.getAccessToken() |
| 81 | + } |
| 82 | + |
| 83 | + const addToCartResponse = await config.fetch( |
| 84 | + addToCurrentCartMutation, |
| 85 | + { |
| 86 | + variables: buildAddToCartVariables({ ...item, productResponse }), |
| 87 | + }, |
| 88 | + { headers: { 'x-vol-user-claims': accessToken } } |
| 89 | + ) |
| 90 | + let currentCart = null |
| 91 | + if (addToCartResponse.data.addItemToCurrentCart) { |
| 92 | + let result = await config.fetch( |
| 93 | + getCartQuery, |
| 94 | + {}, |
| 95 | + { headers: { 'x-vol-user-claims': accessToken } } |
| 96 | + ) |
| 97 | + currentCart = result?.data?.currentCart |
| 98 | + } |
| 99 | + res.status(200).json({ data: normalizeCart(currentCart) }) |
| 100 | +} |
| 101 | + |
| 102 | +export default addItem |
0 commit comments