ice.fo.utils
Version:
152 lines (118 loc) • 4.15 kB
JavaScript
/* eslint-disable vue/max-len */
import dayjs from 'dayjs';
import getValue from '../Getter/getValue';
import { DEVICE_RENDER_CHANNEL } from '../constants/enums';
function getExposureTargetValue({ values, field, $nuxt }) {
if (field == 'lastWeekProductRead') {
const lastWeekProductRead = values.lastWeekProductRead || {};
const productId = $nuxt.$route.params.id || $nuxt.$route.query.productId;
const key = Object.keys(lastWeekProductRead).find((i) => i.endsWith(`::${productId}`));
return lastWeekProductRead[key] || 0;
}
return values[field];
}
export default function checkExposureRules({ rule, $nuxt }) {
if (!rule) {
return true;
}
const exposureType = getValue(rule.exposureType);
// [Exposure] Check visible by Locales
const currentLocale = $nuxt.$i18n && $nuxt.$i18n.locale;
const displayLocales = rule && (rule.locale || []).map((i) => i.value);
if (currentLocale && displayLocales && displayLocales.length && !displayLocales.includes(currentLocale)) {
return false;
}
// [Exposure] Check visible by Device Mode
const displayChannel = rule && (rule.channel || []).map((i) => i.value);
if (displayChannel && displayChannel.length) {
const visibleForMobile = displayChannel.includes(DEVICE_RENDER_CHANNEL.MOBILE) && $nuxt.$$isMobile;
const visibleForDesktop = displayChannel.includes(DEVICE_RENDER_CHANNEL.DESKTOP) && $nuxt.$$isDesktop;
const visibleForApp = displayChannel.includes(DEVICE_RENDER_CHANNEL.APP) && $nuxt.$$isApp;
if (!visibleForMobile && !visibleForDesktop && !visibleForApp) {
return false;
}
}
// [Exposure] Check visible by User Signed Session
const visibleForSigned = rule.signed;
const isLoggedIn = $nuxt.$isSigned;
if (visibleForSigned && !isLoggedIn) {
return false;
}
// [Exposure] Check visible by Period
const useByPeriod = rule.exposurePeriod;
const periodFrom = rule.startDate && dayjs(rule.startDate, 'YYYYMMDDHHmmss');
const periodTo = rule.endDate && dayjs(rule.endDate, 'YYYYMMDDHHmmss');
if (useByPeriod && periodFrom && periodTo) {
const now = dayjs();
if (periodFrom.isAfter(now) || periodTo.isBefore(now)) {
return false;
}
}
// [Exposure] Check visible by Schedule
if (exposureType == 'schedule') {
const schedules = rule.schedule || [];
const visibleForSchedule = schedules.some((schedule) => {
const days = (schedule.days || []).map((i) => getValue(i));
const startTime = schedule.startTime && dayjs(schedule.startTime, 'HH:mm:ss');
const endTime = schedule.endTime && dayjs(schedule.endTime, 'HH:mm:ss');
const now = dayjs();
const currentWeekday = now.weekday();
if (!days.some((x) => x == currentWeekday)) {
return false;
}
if (startTime && now.isBefore(startTime)) {
return false;
}
if (endTime && now.isAfter(endTime)) {
return false;
}
return true;
});
if (!visibleForSchedule) {
return false;
}
}
// [Exposure] Check visible by Target
const targeting = rule.targeting || [];
const visibleByTarget = targeting.every((i) => {
const field = getValue(i, '', 'field');
const method = getValue(i, '', 'method');
const value = getValue(i, '', 'value');
const isNot = i.isNot;
const contextValues = {
...$nuxt.$store.state.session.customerTargetValues,
};
const targetValue = getExposureTargetValue({ values: contextValues, field, $nuxt });
let result;
switch (method) {
case 'above':
result = targetValue >= value;
break;
case 'below':
result = targetValue <= value;
break;
case 'equals':
result = targetValue == value;
break;
case 'contains':
break;
case 'startsWith':
break;
case 'endsWith':
break;
case 'fromto':
break;
case 'isEmpty':
break;
case 'el':
break;
default:
}
return isNot ? !result : result;
});
if (!visibleByTarget) {
return false;
}
// [Exposure] Check complete
return true;
}