Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MWPW-149504 [MILO][MEP] Move entitlements object to the same JSON file used by the library #3047

Merged
merged 13 commits into from
Oct 23, 2024
Merged
4 changes: 2 additions & 2 deletions libs/blocks/library-config/library-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ async function loadList(type, content, list) {
case 'assets':
loadAssets(content, list);
break;
case 'personalization_tags':
case 'MEP_personalization':
loadPersonalization(content, list);
break;
default:
Expand Down Expand Up @@ -153,7 +153,7 @@ async function combineLibraries(base, supplied) {
blocks: base.blocks.data,
templates: base.templates?.data,
icons: base.icons?.data,
personalization_tags: base.personalization?.data,
MEP_personalization: base.personalization?.data,
placeholders: base.placeholders?.data,
};

Expand Down
32 changes: 20 additions & 12 deletions libs/blocks/library-config/lists/personalization.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
import { createTag } from '../../../utils/utils.js';
import createCopy from '../library-utils.js';

const fetchTags = async (path) => {
const resp = await fetch(path);
if (!resp.ok) return [];
const json = await resp.json();
return json.data || [];
};

const categorize = (tagData) => tagData
const categorize = (tagData, category) => tagData
.reduce((tags, tag) => {
tags[tag.category] ??= [];
tags[tag.category].push({
const tagCategory = tag.category || category;
tags[tagCategory] ??= [];
tags[tagCategory].push({
tagname: tag.tagname,
description: tag.description,
});
return tags;
}, {});

const fetchTags = async (path, category) => {
const resp = await fetch(path);
if (!resp.ok) return [];
const json = await resp.json();
return categorize(json.data, category);
};

const getCopyBtn = (tagName) => {
const copy = createTag('button', { class: 'copy' });
copy.id = `${tagName}-tag-copy`;
Expand All @@ -32,8 +33,15 @@ const getCopyBtn = (tagName) => {
};

export default async function loadPersonalization(content, list) {
const tagData = await fetchTags(content[0].path);
const tagsObj = categorize(tagData);
let tagsObj = {};
for (const item of content) {
const { category, path } = item;
tagsObj = {
...tagsObj,
...await fetchTags(path, category),
};
}

list.textContent = '';

Object.entries(tagsObj).forEach(([category, tags]) => {
Expand Down
62 changes: 0 additions & 62 deletions libs/features/personalization/entitlements.js

This file was deleted.

39 changes: 38 additions & 1 deletion libs/features/personalization/personalization.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
/* eslint-disable no-console */

import { createTag, getConfig, loadLink, loadScript, localizeLink } from '../../utils/utils.js';
import { getEntitlementMap } from './entitlements.js';

/* c8 ignore start */
const PHONE_SIZE = window.screen.width < 550 || window.screen.height < 550;
Expand Down Expand Up @@ -66,6 +65,14 @@
return path;
}

if (path.includes('main--federal--adobecom')) {
const { origin } = new URL(window.location);
if (origin.includes('.hlx.live')) path = path.replace('.hlx.live', '.hlx.page');
else if (origin.includes('stage.adobe.com')) path = path.replace('main--federal--adobecom.hlx.page', 'www.stage.adobe.com');
Fixed Show fixed Hide fixed
else if (origin.includes('adobe.com')) path = path.replace('main--federal--adobecom.hlx.page', 'www.adobe.com');
Fixed Show fixed Hide fixed
return path;
}

const config = getConfig();

if (path.startsWith(config.codeRoot)
Expand Down Expand Up @@ -702,6 +709,36 @@
}, { allNames: [] });
}

export const getEntitlementMap = async () => {
const config = getConfig();
if (config.mep?.entitlementMap) return config.mep.entitlementMap;
const sheet = config.env?.name === 'prod' ? 'prod' : 'stage';
const entitlementUrl = `https://main--federal--adobecom.hlx.page/federal/assets/data/mep-entitlement-tags.json?sheet=${sheet}`;
const fetchedData = await fetchData(entitlementUrl, DATA_TYPE.JSON);
if (!fetchedData) return config.consumerEntitlements || {};
const entitlements = {};
fetchedData?.data?.forEach((ent) => {
const { id, tagname } = ent;
entitlements[id] = tagname;
});
config.mep ??= {};
config.mep.entitlementMap = { ...config.consumerEntitlements, ...entitlements };
return config.mep.entitlementMap;
};

export const getEntitlements = async (data) => {
const entitlementMap = await getEntitlementMap();

return data.flatMap((destination) => {
const ents = destination.segments?.flatMap((segment) => {
const entMatch = entitlementMap[segment.id];
return entMatch ? [entMatch] : [];
});

return ents || [];
});
};

async function getPersonalizationVariant(manifestPath, variantNames = [], variantLabel = null) {
const config = getConfig();
if (config.mep?.variantOverride?.[manifestPath]) {
Expand Down
6 changes: 3 additions & 3 deletions libs/martech/martech.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,8 @@ export const getTargetPersonalization = async () => {

const setupEntitlementCallback = () => {
const setEntitlements = async (destinations) => {
const { default: parseEntitlements } = await import('../features/personalization/entitlements.js');
return parseEntitlements(destinations);
const { getEntitlements } = await import('../features/personalization/personalization.js');
return getEntitlements(destinations);
};

const getEntitlements = (resolve) => {
Expand All @@ -173,7 +173,7 @@ const setupEntitlementCallback = () => {
getEntitlements(resolveEnt);

loadLink(
`${miloLibs || codeRoot}/features/personalization/entitlements.js`,
`${miloLibs || codeRoot}/features/personalization/personalization.js`,
{ as: 'script', rel: 'modulepreload' },
);
};
Expand Down
Loading