UNPKG

@contentstack/cli-variants

Version:

Variants plugin

92 lines (91 loc) 5.7 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.lookUpAudiences = void 0; const cli_utilities_1 = require("@contentstack/cli-utilities"); /** * function for substituting an old audience UID with a new one or deleting the audience information if it does not exist * @param audiences - {audiences} list of audience * @param audiencesUid - {audiencesUid} audiences mapper data in format {<old-uid>: <new-uid>} */ function updateAudiences(audiences, audiencesUid) { cli_utilities_1.log.debug(`Updating ${audiences.length} audiences`); for (let audienceIndex = audiences.length - 1; audienceIndex >= 0; audienceIndex--) { const audienceUid = audiences[audienceIndex]; if (audiencesUid.hasOwnProperty(audienceUid) && audiencesUid[audienceUid]) { const newAudienceUid = audiencesUid[audienceUid]; cli_utilities_1.log.debug(`Mapping audience: ${audienceUid} -> ${newAudienceUid}`); audiences[audienceIndex] = newAudienceUid; } else { cli_utilities_1.log.warn(`Audience not found in mapping: ${audienceUid}. Removing from list.`); audiences.splice(audienceIndex, 1); } } cli_utilities_1.log.debug(`Updated audiences count: ${audiences.length}`); } /** * function to either modify the UID or eliminate it if the audience is not created in the target project * @param experience - experience object * @param audiencesUid - {audiencesUid} audiences mapper data in format {<old-uid>: <new-uid>} * @returns */ const lookUpAudiences = (experience, audiencesUid) => { var _a, _b, _c, _d, _e, _f, _g, _h; cli_utilities_1.log.debug('Starting audience lookup for experience'); cli_utilities_1.log.debug(`Available audience mappings: ${(_a = Object.keys(audiencesUid)) === null || _a === void 0 ? void 0 : _a.length}`); // Update experience variations if ((_b = experience === null || experience === void 0 ? void 0 : experience.variations) === null || _b === void 0 ? void 0 : _b.length) { cli_utilities_1.log.debug(`Processing ${experience.variations.length} experience variations`); for (let index = experience.variations.length - 1; index >= 0; index--) { const expVariations = experience.variations[index]; cli_utilities_1.log.debug(`Processing variation ${index + 1}/${experience.variations.length} of type: ${expVariations['__type']}`); if (expVariations['__type'] === 'AudienceBasedVariation' && ((_c = expVariations === null || expVariations === void 0 ? void 0 : expVariations.audiences) === null || _c === void 0 ? void 0 : _c.length)) { cli_utilities_1.log.debug(`Found ${expVariations.audiences.length} audiences in AudienceBasedVariation`); updateAudiences(expVariations.audiences, audiencesUid); if (!expVariations.audiences.length) { cli_utilities_1.log.warn('No audiences remaining after mapping. Removing variation.'); experience.variations.splice(index, 1); } } else { cli_utilities_1.log.debug(`Skipping variation of type: ${expVariations['__type']}`); } } } else if (experience.variants) { cli_utilities_1.log.debug(`Processing ${experience.variants.length} experience variants`); for (let index = experience.variants.length - 1; index >= 0; index--) { const expVariations = experience.variants[index]; cli_utilities_1.log.debug(`Processing variant ${index + 1}/${experience.variants.length} of type: ${expVariations['__type']}`); if (expVariations['__type'] === 'SegmentedVariant' && ((_d = expVariations === null || expVariations === void 0 ? void 0 : expVariations.audiences) === null || _d === void 0 ? void 0 : _d.length)) { cli_utilities_1.log.debug(`Found ${expVariations.audiences.length} audiences in SegmentedVariant`); updateAudiences(expVariations.audiences, audiencesUid); if (!expVariations.audiences.length) { cli_utilities_1.log.warn('No audiences remaining after mapping. Removing variant.'); experience.variants.splice(index, 1); } } else { cli_utilities_1.log.debug(`Skipping variant of type: ${expVariations['__type']}`); } } } else { cli_utilities_1.log.debug('No variations or variants found in experience'); } if (((_e = experience === null || experience === void 0 ? void 0 : experience.targeting) === null || _e === void 0 ? void 0 : _e.hasOwnProperty('audience')) && ((_h = (_g = (_f = experience === null || experience === void 0 ? void 0 : experience.targeting) === null || _f === void 0 ? void 0 : _f.audience) === null || _g === void 0 ? void 0 : _g.audiences) === null || _h === void 0 ? void 0 : _h.length)) { cli_utilities_1.log.debug(`Processing ${experience.targeting.audience.audiences.length} targeting audiences`); // Update targeting audiences updateAudiences(experience.targeting.audience.audiences, audiencesUid); if (!experience.targeting.audience.audiences.length) { cli_utilities_1.log.warn('No targeting audiences remaining after mapping. Removing targeting.'); experience.targeting = {}; } } else { cli_utilities_1.log.debug('No targeting audiences found in experience'); } cli_utilities_1.log.debug('Audience lookup completed for experience'); return experience; }; exports.lookUpAudiences = lookUpAudiences;