diff --git a/blocks/targeted-block/qraphql.js b/blocks/targeted-block/graphql.js similarity index 50% rename from blocks/targeted-block/qraphql.js rename to blocks/targeted-block/graphql.js index df26707693..28889eb87a 100644 --- a/blocks/targeted-block/qraphql.js +++ b/blocks/targeted-block/graphql.js @@ -1,17 +1,55 @@ import { fetchGraphQl, setFetchGraphQlHeaders } from '@dropins/tools/fetch-graphql.js'; import { getHeaders } from '../../scripts/configs.js'; -export const getActiveRules = async (cartId) => { +const getCustomerGroups = async () => { try { - setFetchGraphQlHeaders(await getHeaders('cart')); + // setFetchGraphQlHeaders(await getHeaders('cart')); const response = await fetchGraphQl( - `query CUSTOMER_SEGMENTS($cartId: String!){ - customerSegments(cartId: $cartId) { - name - } + `query { customerGroup { name } + } + `, + { + method: 'GET', + }, + ); + return response.data; + } catch (error) { + console.error('Could not retrieve customer groups', error); + } + return []; +}; + +const getCustomerSegments = async () => { + try { + // setFetchGraphQlHeaders(await getHeaders('cart')); + const response = await fetchGraphQl( + `query { + customer { + segments { + name + } + } + } + `, + { + method: 'GET', + }, + ); + return response.data; + } catch (error) { + console.error('Could not retrieve customer segments', error); + } + return []; +}; + +const getCartRules = async (cartId) => { + try { + // setFetchGraphQlHeaders(await getHeaders('cart')); + const response = await fetchGraphQl( + `query CART_RULES($cartId: String!){ cart(cart_id: $cartId) { rules { name @@ -31,7 +69,7 @@ export const getActiveRules = async (cartId) => { return []; }; -export const getCatalogPriceRules = async (sku) => { +const getCatalogPriceRules = async (sku) => { try { const query = `query CATALOG_PRICE_RULES($sku: String!) { products(filter: { @@ -48,7 +86,7 @@ export const getCatalogPriceRules = async (sku) => { } } `; - setFetchGraphQlHeaders(await getHeaders('cart')); + // setFetchGraphQlHeaders(await getHeaders('cart')); const response = await fetchGraphQl( query, { @@ -62,3 +100,10 @@ export const getCatalogPriceRules = async (sku) => { } return []; }; + +export { + getCustomerGroups, + getCustomerSegments, + getCartRules, + getCatalogPriceRules, +}; diff --git a/blocks/targeted-block/targeted-block.js b/blocks/targeted-block/targeted-block.js index f5fd407709..bb28522686 100644 --- a/blocks/targeted-block/targeted-block.js +++ b/blocks/targeted-block/targeted-block.js @@ -1,6 +1,11 @@ import { events } from '@dropins/tools/event-bus.js'; import * as Cart from '@dropins/storefront-cart/api.js'; -import { getActiveRules, getCatalogPriceRules } from './qraphql.js'; +import { + getCustomerGroups, + getCustomerSegments, + getCartRules, + getCatalogPriceRules, +} from './graphql.js'; import conditionsMatched from './condition-matcher.js'; import { readBlockConfig } from '../../scripts/aem.js'; import { loadFragment } from '../fragment/fragment.js'; @@ -9,18 +14,21 @@ const blocks = []; const displayedBlockTypes = []; const updateTargetedBlocksVisibility = async () => { - const activeRules = (Cart.getCartDataFromCache() === null) ? { - customerSegments: [], - customerGroup: [], + const activeRules = { + customerSegments: await getCustomerSegments(), + customerGroup: await getCustomerGroups(), cart: { rules: [], }, catalogPriceRules: [], - } : await getActiveRules(Cart.getCartDataFromCache().id); + }; + + if (Cart.getCartDataFromCache() !== null) { + activeRules.cart = await getCartRules(Cart.getCartDataFromCache().id); + } // eslint-disable-next-line no-underscore-dangle const productData = events._lastEvent?.['pdp/data']?.payload ?? null; - if (productData?.sku) { activeRules.catalogPriceRules = await getCatalogPriceRules(productData.sku); }