Skip to content

Commit

Permalink
LYNX-759: Separate requests for customer group and segment
Browse files Browse the repository at this point in the history
  • Loading branch information
bl4de committed Feb 13, 2025
1 parent 37d4167 commit 52775cc
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -1,17 +1,55 @@
import { fetchGraphQl, setFetchGraphQlHeaders } from '@dropins/tools/fetch-graphql.js';

Check failure on line 1 in blocks/targeted-block/graphql.js

View workflow job for this annotation

GitHub Actions / build

'setFetchGraphQlHeaders' is defined but never used. Allowed unused vars must match /^_/u
import { getHeaders } from '../../scripts/configs.js';

Check failure on line 2 in blocks/targeted-block/graphql.js

View workflow job for this annotation

GitHub Actions / build

'getHeaders' is defined but never used. Allowed unused vars must match /^_/u

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
Expand All @@ -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: {
Expand All @@ -48,7 +86,7 @@ export const getCatalogPriceRules = async (sku) => {
}
}
`;
setFetchGraphQlHeaders(await getHeaders('cart'));
// setFetchGraphQlHeaders(await getHeaders('cart'));
const response = await fetchGraphQl(
query,
{
Expand All @@ -62,3 +100,10 @@ export const getCatalogPriceRules = async (sku) => {
}
return [];
};

export {
getCustomerGroups,
getCustomerSegments,
getCartRules,
getCatalogPriceRules,
};
20 changes: 14 additions & 6 deletions blocks/targeted-block/targeted-block.js
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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);
}
Expand Down

0 comments on commit 52775cc

Please sign in to comment.