@contentstack/cli-variants
Version:
Variants plugin
41 lines (40 loc) • 2.36 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.lookUpEvents = void 0;
const cli_utilities_1 = require("@contentstack/cli-utilities");
/**
* function to either modify the UID or eliminate it if the event is not created in the target project
* @param experience - experience object
* @param eventsUid - {eventsUid} events mapper data in format {<old-uid>: <new-uid>}
* @returns
*/
const lookUpEvents = (experience, eventsUid) => {
var _a, _b, _c;
cli_utilities_1.log.debug('Starting event lookup for experience');
cli_utilities_1.log.debug(`Available event mappings: ${(_a = Object.keys(eventsUid)) === null || _a === void 0 ? void 0 : _a.length}`);
// Update events uid in experience metrics
if ((_b = experience === null || experience === void 0 ? void 0 : experience.metrics) === null || _b === void 0 ? void 0 : _b.length) {
cli_utilities_1.log.debug(`Processing ${experience.metrics.length} experience metrics`);
for (let metricIndex = ((_c = experience === null || experience === void 0 ? void 0 : experience.metrics) === null || _c === void 0 ? void 0 : _c.length) - 1; metricIndex >= 0; metricIndex--) {
const metric = experience === null || experience === void 0 ? void 0 : experience.metrics[metricIndex];
const eventUid = metric.event;
cli_utilities_1.log.debug(`Processing metric ${metricIndex + 1}/${experience.metrics.length} with event: ${eventUid}`);
if (eventsUid.hasOwnProperty(eventUid) && eventsUid[eventUid]) {
const newEventUid = eventsUid[eventUid];
cli_utilities_1.log.debug(`Mapping event: ${eventUid} -> ${newEventUid}`);
experience.metrics[metricIndex].event = newEventUid;
}
else {
cli_utilities_1.log.warn(`Event not found in mapping: ${eventUid}. Removing metric.`);
experience === null || experience === void 0 ? void 0 : experience.metrics.splice(metricIndex, 1);
}
}
cli_utilities_1.log.debug(`Final metrics count: ${experience.metrics.length}`);
}
else {
cli_utilities_1.log.debug('No metrics found in experience');
}
cli_utilities_1.log.debug('Event lookup completed for experience');
return experience;
};
exports.lookUpEvents = lookUpEvents;