mirror of
https://github.com/vercel/commerce.git
synced 2025-05-19 07:56:59 +00:00
reconcile carts on add-item if needed
Signed-off-by: Loan Laux <loan@outgrow.io>
This commit is contained in:
parent
e5d410eb91
commit
ea9cfe9c02
@ -4,6 +4,7 @@ import {
|
|||||||
createCartMutation,
|
createCartMutation,
|
||||||
} from '@framework/utils/mutations'
|
} from '@framework/utils/mutations'
|
||||||
import getCartCookie from '@framework/api/utils/get-cart-cookie'
|
import getCartCookie from '@framework/api/utils/get-cart-cookie'
|
||||||
|
import reconcileCarts from '@framework/api/utils/reconcile-carts'
|
||||||
import {
|
import {
|
||||||
REACTION_ANONYMOUS_CART_TOKEN_COOKIE,
|
REACTION_ANONYMOUS_CART_TOKEN_COOKIE,
|
||||||
REACTION_CART_ID_COOKIE,
|
REACTION_CART_ID_COOKIE,
|
||||||
@ -11,13 +12,7 @@ import {
|
|||||||
} from '@framework/const'
|
} from '@framework/const'
|
||||||
|
|
||||||
const addItem: CartHandlers['addItem'] = async ({
|
const addItem: CartHandlers['addItem'] = async ({
|
||||||
req: {
|
req: { cookies },
|
||||||
cookies: {
|
|
||||||
[REACTION_ANONYMOUS_CART_TOKEN_COOKIE]: anonymousCartToken,
|
|
||||||
[REACTION_CART_ID_COOKIE]: cartId,
|
|
||||||
[REACTION_CUSTOMER_TOKEN_COOKIE]: reactionCustomerToken,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
res,
|
res,
|
||||||
body: { item },
|
body: { item },
|
||||||
config,
|
config,
|
||||||
@ -25,6 +20,13 @@ const addItem: CartHandlers['addItem'] = async ({
|
|||||||
console.log('add-item API', item.productId)
|
console.log('add-item API', item.productId)
|
||||||
console.log('variantId', item.variantId)
|
console.log('variantId', item.variantId)
|
||||||
|
|
||||||
|
const {
|
||||||
|
[REACTION_ANONYMOUS_CART_TOKEN_COOKIE]: anonymousCartToken,
|
||||||
|
[REACTION_CUSTOMER_TOKEN_COOKIE]: reactionCustomerToken,
|
||||||
|
} = cookies
|
||||||
|
|
||||||
|
let { [REACTION_CART_ID_COOKIE]: cartId } = cookies
|
||||||
|
|
||||||
if (!cartId) {
|
if (!cartId) {
|
||||||
return res.status(400).json({
|
return res.status(400).json({
|
||||||
data: null,
|
data: null,
|
||||||
@ -74,6 +76,7 @@ const addItem: CartHandlers['addItem'] = async ({
|
|||||||
999
|
999
|
||||||
),
|
),
|
||||||
])
|
])
|
||||||
|
|
||||||
return res.status(200).json(createdCart.data)
|
return res.status(200).json(createdCart.data)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -90,6 +93,23 @@ const addItem: CartHandlers['addItem'] = async ({
|
|||||||
] = `Bearer ${reactionCustomerToken}`
|
] = `Bearer ${reactionCustomerToken}`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (anonymousCartToken && reactionCustomerToken) {
|
||||||
|
console.log('reconciliating carts')(
|
||||||
|
({ _id: cartId } = await reconcileCarts({
|
||||||
|
config,
|
||||||
|
anonymousCartId: cartId,
|
||||||
|
cartToken: anonymousCartToken,
|
||||||
|
reactionCustomerToken,
|
||||||
|
}))
|
||||||
|
)
|
||||||
|
|
||||||
|
// Clear the anonymous cart token cookie and update cart ID cookie
|
||||||
|
res.setHeader('Set-Cookie', [
|
||||||
|
getCartCookie(config.anonymousCartTokenCookie),
|
||||||
|
getCartCookie(config.cartIdCookie, cartId, 999),
|
||||||
|
])
|
||||||
|
}
|
||||||
|
|
||||||
const updatedCart = await config.fetch(
|
const updatedCart = await config.fetch(
|
||||||
addCartItemsMutation,
|
addCartItemsMutation,
|
||||||
{
|
{
|
||||||
|
@ -2,8 +2,8 @@ import type { Cart } from '../../../types'
|
|||||||
import type { CartHandlers } from '../'
|
import type { CartHandlers } from '../'
|
||||||
import getAnomymousCartQuery from '@framework/utils/queries/get-anonymous-cart'
|
import getAnomymousCartQuery from '@framework/utils/queries/get-anonymous-cart'
|
||||||
import accountCartByAccountIdQuery from '@framework/utils/queries/account-cart-by-account-id'
|
import accountCartByAccountIdQuery from '@framework/utils/queries/account-cart-by-account-id'
|
||||||
import reconcileCartsMutation from '@framework/utils/mutations/reconcile-carts'
|
|
||||||
import getCartCookie from '@framework/api/utils/get-cart-cookie'
|
import getCartCookie from '@framework/api/utils/get-cart-cookie'
|
||||||
|
import reconcileCarts from '@framework/api/utils/reconcile-carts'
|
||||||
import getViewerId from '@framework/customer/get-viewer-id'
|
import getViewerId from '@framework/customer/get-viewer-id'
|
||||||
import {
|
import {
|
||||||
REACTION_ANONYMOUS_CART_TOKEN_COOKIE,
|
REACTION_ANONYMOUS_CART_TOKEN_COOKIE,
|
||||||
@ -25,27 +25,12 @@ const getCart: CartHandlers['getCart'] = async ({ req, res, config }) => {
|
|||||||
let normalizedCart
|
let normalizedCart
|
||||||
|
|
||||||
if (cartId && anonymousCartToken && reactionCustomerToken) {
|
if (cartId && anonymousCartToken && reactionCustomerToken) {
|
||||||
const {
|
const rawReconciledCart = await reconcileCarts({
|
||||||
data: {
|
config,
|
||||||
reconcileCarts: { cart: rawReconciledCart },
|
anonymousCartId,
|
||||||
},
|
cartToken,
|
||||||
} = await config.fetch(
|
reactionCustomerToken,
|
||||||
reconcileCartsMutation,
|
})
|
||||||
{
|
|
||||||
variables: {
|
|
||||||
input: {
|
|
||||||
anonymousCartId: cartId,
|
|
||||||
cartToken: anonymousCartToken,
|
|
||||||
shopId: config.shopId,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
headers: {
|
|
||||||
Authorization: `Bearer ${reactionCustomerToken}`,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
)
|
|
||||||
|
|
||||||
normalizedCart = normalizeCart(rawReconciledCart)
|
normalizedCart = normalizeCart(rawReconciledCart)
|
||||||
|
|
||||||
|
34
framework/reactioncommerce/api/utils/reconcile-carts.ts
Normal file
34
framework/reactioncommerce/api/utils/reconcile-carts.ts
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
import reconcileCartsMutation from '@framework/utils/mutations/reconcile-carts'
|
||||||
|
|
||||||
|
async function reconcileCarts({
|
||||||
|
config,
|
||||||
|
anonymousCartId,
|
||||||
|
cartToken,
|
||||||
|
reactionCustomerToken,
|
||||||
|
}) {
|
||||||
|
const {
|
||||||
|
data: {
|
||||||
|
reconcileCarts: { cart: rawReconciledCart },
|
||||||
|
},
|
||||||
|
} = await config.fetch(
|
||||||
|
reconcileCartsMutation,
|
||||||
|
{
|
||||||
|
variables: {
|
||||||
|
input: {
|
||||||
|
anonymousCartId: cartId,
|
||||||
|
cartToken: anonymousCartToken,
|
||||||
|
shopId: config.shopId,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
headers: {
|
||||||
|
Authorization: `Bearer ${reactionCustomerToken}`,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
return rawReconciledCart
|
||||||
|
}
|
||||||
|
|
||||||
|
export default reconcileCarts
|
Loading…
x
Reference in New Issue
Block a user