From a2b62ae2f92a1be93803def75e154d357d5bc80d Mon Sep 17 00:00:00 2001 From: Marco Montalbano Date: Wed, 6 Nov 2024 11:10:31 +0100 Subject: [PATCH] fix: filter customer cart by the market in scope --- packages/docs/stories/assets/constants.ts | 4 ++-- packages/drop-in/src/apis/commercelayer/cart.ts | 8 ++++++++ packages/drop-in/src/index.html | 2 +- 3 files changed, 11 insertions(+), 3 deletions(-) diff --git a/packages/docs/stories/assets/constants.ts b/packages/docs/stories/assets/constants.ts index 658098ca..eceb8282 100644 --- a/packages/docs/stories/assets/constants.ts +++ b/packages/docs/stories/assets/constants.ts @@ -14,9 +14,9 @@ export const codes = { // // stg // // eslint-disable-next-line @typescript-eslint/explicit-function-return-type // export const createConfig = (scope: string) => ({ -// clientId: '2oh-47l4PN_yb8p4mp7AxUsb-Ue3e7tBqteoZcQM-nU', +// clientId: 'gQMSINLyMm2TrZo0UGEEdubC7uSgm9-', // slug: 'drop-in-js-stg', -// scope: 'market:545', +// scope, // debug: 'all', // domain: 'commercelayer.co' // }) diff --git a/packages/drop-in/src/apis/commercelayer/cart.ts b/packages/drop-in/src/apis/commercelayer/cart.ts index 23145f31..5ad89242 100644 --- a/packages/drop-in/src/apis/commercelayer/cart.ts +++ b/packages/drop-in/src/apis/commercelayer/cart.ts @@ -8,6 +8,7 @@ import type { TriggerHostedCartUpdate } from '#apis/types' import { pDebounce } from '#utils/debounce' +import { jwtDecode } from '@commercelayer/js-auth' import type { Order, QueryParamsRetrieve } from '@commercelayer/sdk' import Cookies from 'js-cookie' import memoize from 'lodash/memoize' @@ -133,9 +134,16 @@ export async function _getCart(): Promise { return order } + const jwt = jwtDecode(token.accessToken) + + if (!('market' in jwt.payload) || jwt.payload.market?.id == null) { + return null + } + const [order = null] = await client.customers.orders(token.customerId, { ...orderParams, filters: { + market_id_in: jwt.payload.market.id.join(','), guest_false: true, editable_true: true, status_eq: 'pending' diff --git a/packages/drop-in/src/index.html b/packages/drop-in/src/index.html index 2b7f3938..e1d5802f 100644 --- a/packages/drop-in/src/index.html +++ b/packages/drop-in/src/index.html @@ -51,7 +51,7 @@ // domain: 'commercelayer.co', // clientId: '', // slug: 'drop-in-js-stg', - // scope: 'market:545', + // scope: 'market:code:usa', // debug: 'all', // orderReturnUrl: 'https://example.com' // // validScopes: ['market:545', 'market:1234', ''],