UNPKG

ice.fo.utils

Version:

149 lines (116 loc) 4.04 kB
import dayjs from 'dayjs' import { getValue } from 'ice.fo.utils/GetterUtils' import { DEVICE_RENDER_CHANNEL } from 'ice.fo.utils/constants/enums' function getExposureTargetValue ({ values, field, $route }) { if (field == 'lastWeekProductRead') { const lastWeekProductRead = values.lastWeekProductRead || {} const productId = $route.params.id || $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 } return isNot ? !result : result }) if (!visibleByTarget) { return false } // [Exposure] Check complete return true }