All files / src/transformers core_transformer.js

92.31% Statements 48/52
86.11% Branches 31/36
100% Functions 5/5
92.16% Lines 47/51

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231                                              4x                                                                                                   4x 11x 3x     8x 8x 8x 13x 13x 13x 13x               13x 13x 13x 16x   13x                             13x   13x 16x             13x 10x             13x   13x 9x 9x   9x 13x   13x 4x 10x     4x 2x     2x     2x   9x     11x               11x 1x         1x     10x 2x                   8x                     10x     6x                   4x                     10x     5x                
import {logger} from '../logger'
import {isValidFlagConfig} from '../validators/core_validator'
import Population, {
  RULE_TYPE_STRING,
  RULE_TYPE_INT,
  RULE_TYPE_FLOAT,
  RULE_TYPE_BOOLEAN,
  RULE_TYPE_DATE,
  RULE_TYPE_DATETIME,
  RULE_OPERATOR_IS,
  RULE_OPERATOR_IS_NOT,
  RULE_OPERATOR_IN,
  RULE_OPERATOR_NOT_IN,
  RULE_OPERATOR_LT,
  RULE_OPERATOR_LTE,
  RULE_OPERATOR_GT,
  RULE_OPERATOR_GTE,
  RULE_OPERATOR_FROM,
  RULE_OPERATOR_UNTIL,
  RULE_OPERATOR_AFTER,
  RULE_OPERATOR_BEFORE
} from '../population'
 
const RULE_TYPE_TO_ALLOWED_OPERATORS = {
  [RULE_TYPE_STRING]: new Set([
    RULE_OPERATOR_IS,
    RULE_OPERATOR_IS_NOT,
    RULE_OPERATOR_IN,
    RULE_OPERATOR_NOT_IN
  ]),
  [RULE_TYPE_INT]: new Set([
    RULE_OPERATOR_IS,
    RULE_OPERATOR_IS_NOT,
    RULE_OPERATOR_IN,
    RULE_OPERATOR_NOT_IN,
    RULE_OPERATOR_LT,
    RULE_OPERATOR_LTE,
    RULE_OPERATOR_GT,
    RULE_OPERATOR_GTE
  ]),
  [RULE_TYPE_FLOAT]: new Set([
    RULE_OPERATOR_IS,
    RULE_OPERATOR_IS_NOT,
    RULE_OPERATOR_IN,
    RULE_OPERATOR_NOT_IN,
    RULE_OPERATOR_LT,
    RULE_OPERATOR_LTE,
    RULE_OPERATOR_GT,
    RULE_OPERATOR_GTE
  ]),
  [RULE_TYPE_BOOLEAN]: new Set([RULE_OPERATOR_IS, RULE_OPERATOR_IS_NOT]),
  [RULE_TYPE_DATE]: new Set([
    RULE_OPERATOR_IS,
    RULE_OPERATOR_IS_NOT,
    RULE_OPERATOR_IN,
    RULE_OPERATOR_NOT_IN,
    RULE_OPERATOR_FROM,
    RULE_OPERATOR_UNTIL,
    RULE_OPERATOR_AFTER,
    RULE_OPERATOR_BEFORE
  ]),
  [RULE_TYPE_DATETIME]: new Set([
    RULE_OPERATOR_IS,
    RULE_OPERATOR_IS_NOT,
    RULE_OPERATOR_IN,
    RULE_OPERATOR_NOT_IN,
    RULE_OPERATOR_FROM,
    RULE_OPERATOR_UNTIL,
    RULE_OPERATOR_AFTER,
    RULE_OPERATOR_BEFORE
  ])
}
 
export const transformFlagConfig = flagConfig => {
  if (!isValidFlagConfig(flagConfig)) {
    return null
  }
 
  const flags = []
  const keys = Object.keys(flagConfig)
  for (let i = 0; i < keys.length; i++) {
    const key = keys[i]
    const config = flagConfig[key]
    const active = config.active !== undefined ? config.active : true
    const flagInfo = {
      flagType: 'basic',
      hashKey: key,
      isPaused: !active,
      isWebAccessible: __BROWSER__, // eslint-disable-line no-undef
      codename: key,
      flagStatus: 'operational'
    }
    const whitelist = config.whitelist || []
    const blacklist = config.blacklist || []
    const blacklistSet = new Set(blacklist)
    const filteredWhitelist = whitelist.filter(i => !blacklistSet.has(i))
 
    flagInfo.treatments = [
      {
        treatmentId: 'off-treatment',
        codename: 'off',
        isControl: false,
        isOffTreatment: true
      },
      {
        treatmentId: 'on-treatment',
        codename: 'on',
        isControl: false,
        isOffTreatment: false
      }
    ]
 
    flagInfo.overrides = []
 
    flagInfo.overrides = flagInfo.overrides.concat(
      filteredWhitelist.map(i => ({
        treatmentId: 'on-treatment',
        entityType: 'User',
        entityId: i.toString()
      }))
    )
 
    flagInfo.overrides = flagInfo.overrides.concat(
      blacklist.map(i => ({
        treatmentId: 'off-treatment',
        entityType: 'User',
        entityId: i.toString()
      }))
    )
 
    flagInfo.splits = [{treatmentId: 'on-treatment', percentage: 1}]
 
    if (config.population) {
      const rules = config.population || []
      const ruleInfos = []
 
      for (let j = 0; j < rules.length; j++) {
        const r = rules[j]
        let type
        if (Array.isArray(r.value)) {
          const types = new Set(
            r.value.map(v => Population.categorizeValueType(v))
          )
 
          if (types.size != 1) {
            logger(
              "Population's filter criteria each should have a singular value type. In other words, do not mix strings with numbers in the same array, for example."
            )
            return null
          }
 
          type = Array.from(types)[0]
        } else {
          type = Population.categorizeValueType(r.value)
        }
 
        const rInfo = {
          attributeName: r.attribute,
          attributeType: type,
          operator: r.operator,
          value: !Array.isArray(r.value) ? r.value : null,
          valueList: !Array.isArray(r.value) ? null : r.value
        }
 
        if (!RULE_TYPE_TO_ALLOWED_OPERATORS[type].has(r.operator)) {
          logger(
            `Population's filter operator \`${
              r.operator
            }\` is not allowed for filter type \`${type}\``
          )
          return null
        }
 
        if (rInfo.valueList) {
          Iif (
            rInfo.operator !== RULE_OPERATOR_IN &&
            rInfo.operator !== RULE_OPERATOR_NOT_IN
          ) {
            logger(
              "Population's filter operator must be `in` or `not_in` if the value is an array."
            )
            return null
          }
        } else {
          Iif (
            rInfo.operator === RULE_OPERATOR_IN &&
            rInfo.operator === RULE_OPERATOR_NOT_IN
          ) {
            logger(
              "Population's filter operator must not be `in` or `not_in` if the value is a number, boolean or string."
            )
            return null
          }
        }
 
        ruleInfos.push(rInfo)
      }
 
      flagInfo.populations = [
        {
          hashKey: 'population-1',
          entityType: 'User',
          percentage: config.sample || 0.0,
          rules: ruleInfos,
          universes: []
        }
      ]
    } else {
      flagInfo.populations = [
        {
          hashKey: 'population-1',
          entityType: 'User',
          percentage: config.sample || 0.0,
          rules: [],
          universes: []
        }
      ]
    }
 
    flags.push(flagInfo)
  }
 
  return {
    flags: flags,
    env: {
      hashKey: 'env-1',
      envKey: null
    }
  }
}