All files / modules/75-cf/pages/feature-flags-detail/targeting-rules-tab/utils PatchFeatureFlagUtils.ts

100% Statements 84/84
82% Branches 41/50
100% Functions 46/46
100% Lines 63/63

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 232 233 234 235 236            5x   5x 5x                                                                   5x       14x 16x     14x 17x     14x 16x     14x 17x     14x   14x   14x   14x   5x   5x   5x 3x       14x   5x   5x   5x 3x       14x   4x   4x   4x     14x   4x   4x   4x     14x 7x   7x           14x 2x   14x 2x   14x 2x   14x         2x   4x                                 14x 2x 4x       14x 2x     14x 2x         14x       1x                                       14x 7x 1x   1x     1x 1x                     14x 1x     14x                                          
/*
 * Copyright 2022 Harness Inc. All rights reserved.
 * Use of this source code is governed by the PolyForm Shield 1.0.0 license
 * that can be found in the licenses directory at the root of this repository, also available at
 * https://polyformproject.org/wp-content/uploads/2020/06/PolyForm-Shield-1.0.0.txt.
 */
import isEqual from 'lodash-es/isEqual'
import type { FeatureState } from 'services/cf'
import patch from '@cf/utils/instructions'
import {
  FormVariationMap,
  VariationTargetGroup,
  TargetingRuleItemType,
  TargetingRulesFormValues,
  VariationPercentageRollout
} from '../types'
 
// Utils class to help encapsulate the complexity around patch instruction creation and hide this from the components.
interface PatchFeatureFlagUtilsReturn {
  hasFlagStateChanged: () => boolean
  hasDefaultOnVariationChanged: () => boolean
  hasDefaultOffVariationChanged: () => boolean
  addedTargetGroups: (variationIdentifier: string) => VariationTargetGroup[]
  removedTargetGroups: (variationIdentifier: string) => VariationTargetGroup[]
  addedTargets: (variationIdentifier: string) => string[]
  removedTargets: (variationIdentifier: string) => string[]
  updatedPercentageRollouts: () => VariationPercentageRollout[]
  createUpdateFlagStateInstruction: () => void
  createDefaultServeOnInstruction: () => void
  createDefaultServeOffInstruction: () => void
  createAddTargetGroupInstructions: (
    variationIdentifier: string,
    targetGroups: VariationTargetGroup[],
    position: number
  ) => void
  createRemoveTargetGroupsInstructions: (targetGroups: VariationTargetGroup[]) => void
  createAddTargetsInstructions: (variationIdentifier: string, targetIds: string[]) => void
  createRemoveTargetsInstructions: (variationIdentifier: string, targetIds: string[]) => void
  createAddPercentageRolloutInstructions: (percentageRollout: VariationPercentageRollout, index: number) => void
  createUpdatePercentageRolloutInstructions: (percentageRollouts: VariationPercentageRollout[]) => void
  createRemovePercentageRolloutInstruction: (percentageRollout: VariationPercentageRollout) => void
}
 
export const PatchFeatureFlagUtils = (
  submittedValues: TargetingRulesFormValues,
  initialValues: TargetingRulesFormValues
): PatchFeatureFlagUtilsReturn => {
  const initialPercentageRollouts = initialValues.targetingRuleItems.filter(
    targetingRule => targetingRule.type === TargetingRuleItemType.PERCENTAGE_ROLLOUT
  ) as VariationPercentageRollout[]
 
  const submittedPercentageRollouts = submittedValues.targetingRuleItems.filter(
    targetingRule => targetingRule.type === TargetingRuleItemType.PERCENTAGE_ROLLOUT
  ) as VariationPercentageRollout[]
 
  const initialVariations = initialValues.targetingRuleItems.filter(
    targetingRule => targetingRule.type === TargetingRuleItemType.VARIATION
  ) as FormVariationMap[]
 
  const submittedVariations = submittedValues.targetingRuleItems.filter(
    targetingRule => targetingRule.type === TargetingRuleItemType.VARIATION
  ) as FormVariationMap[]
 
  const hasFlagStateChanged = (): boolean => submittedValues.state !== initialValues.state
 
  const hasDefaultOnVariationChanged = (): boolean => submittedValues.onVariation !== initialValues.onVariation
 
  const hasDefaultOffVariationChanged = (): boolean => submittedValues.offVariation !== initialValues.offVariation
 
  const addedTargetGroups = (variationIdentifier: string): VariationTargetGroup[] => {
    const initialTargetGroups: VariationTargetGroup[] =
      initialVariations.find(x => x.variationIdentifier === variationIdentifier)?.targetGroups || []
    const submittedTargetGroups: VariationTargetGroup[] =
      submittedVariations.find(x => x.variationIdentifier === variationIdentifier)?.targetGroups || []
 
    return submittedTargetGroups.filter(
      submittedTargetGroup => !initialTargetGroups.map(({ label }) => label).includes(submittedTargetGroup.label)
    )
  }
 
  const removedTargetGroups = (variationIdentifier: string): VariationTargetGroup[] => {
    const initialTargetGroups: VariationTargetGroup[] =
      initialVariations.find(x => x.variationIdentifier === variationIdentifier)?.targetGroups || []
    const submittedTargetGroups: VariationTargetGroup[] =
      submittedVariations.find(x => x.variationIdentifier === variationIdentifier)?.targetGroups || []
 
    return initialTargetGroups.filter(
      targetGroup => !submittedTargetGroups.map(({ label }) => label).includes(targetGroup.label)
    )
  }
 
  const addedTargets = (variationIdentifier: string): string[] => {
    const initialTargetIds: string[] =
      initialVariations.find(x => x.variationIdentifier === variationIdentifier)?.targets.map(x => x.value) || []
    const submittedTargetIds: string[] =
      submittedVariations.find(x => x.variationIdentifier === variationIdentifier)?.targets.map(x => x.value) || []
 
    return submittedTargetIds.filter(id => !initialTargetIds.includes(id))
  }
 
  const removedTargets = (variationIdentifier: string): string[] => {
    const initialTargetIds: string[] =
      initialVariations.find(x => x.variationIdentifier === variationIdentifier)?.targets.map(x => x.value) || []
    const submittedTargetIds: string[] =
      submittedVariations.find(x => x.variationIdentifier === variationIdentifier)?.targets.map(x => x.value) || []
 
    return initialTargetIds.filter(id => !submittedTargetIds.includes(id))
  }
 
  const updatedPercentageRollouts = (): VariationPercentageRollout[] => {
    return submittedPercentageRollouts.length === initialPercentageRollouts.length
      ? submittedPercentageRollouts.filter(
          initial => !initialPercentageRollouts.some(submitted => isEqual(initial, submitted))
        )
      : []
  }
 
  // INSTRUCTIONS SECTION
  const createUpdateFlagStateInstruction = (): void =>
    patch.feature.addInstruction(patch.creators.setFeatureFlagState(submittedValues.state as FeatureState))
 
  const createDefaultServeOnInstruction = (): void =>
    patch.feature.addInstruction(patch.creators.updateDefaultServeByVariation(submittedValues.onVariation))
 
  const createDefaultServeOffInstruction = (): void =>
    patch.feature.addInstruction(patch.creators.updateOffVariation(submittedValues.offVariation))
 
  const createAddTargetGroupInstructions = (
    variationIdentifier: string,
    targetGroups: VariationTargetGroup[],
    priority: number
  ): void => {
    patch.feature.addAllInstructions(
      targetGroups.map((targetGroup, index) =>
        patch.creators.addRule({
          uuid: targetGroup.ruleId,
          priority: priority + index + 1,
          serve: {
            variation: variationIdentifier
          },
          clauses: [
            {
              op: 'segmentMatch',
              values: [targetGroup.value]
            }
          ]
        })
      )
    )
  }
 
  const createRemoveTargetGroupsInstructions = (targetGroups: VariationTargetGroup[]): void => {
    patch.feature.addAllInstructions(
      targetGroups.map(targetGroup => patch.creators.removeRule(targetGroup.ruleId as string))
    )
  }
 
  const createAddTargetsInstructions = (variationIdentifier: string, targets: string[]): void => {
    patch.feature.addInstruction(patch.creators.addTargetsToVariationTargetMap(variationIdentifier, targets))
  }
 
  const createRemoveTargetsInstructions = (variationIdentifier: string, removedTargetIds: string[]): void => {
    patch.feature.addInstruction(
      patch.creators.removeTargetsToVariationTargetMap(variationIdentifier, removedTargetIds)
    )
  }
 
  const createAddPercentageRolloutInstructions = (
    percentageRollout: VariationPercentageRollout,
    index: number
  ): void => {
    patch.feature.addInstruction(
      patch.creators.addRule({
        uuid: percentageRollout.ruleId,
        priority: index + 1,
        serve: {
          distribution: {
            bucketBy: percentageRollout.bucketBy,
            variations: percentageRollout.variations
          }
        },
        clauses: [
          {
            op: 'segmentMatch',
            values: percentageRollout.clauses[0].values
          }
        ]
      })
    )
  }
 
  const createUpdatePercentageRolloutInstructions = (percentageRollouts: VariationPercentageRollout[]): void => {
    percentageRollouts.forEach(percentageRollout => {
      const { bucketBy, variations } = percentageRollout
 
      patch.feature.addInstruction(
        patch.creators.updateRuleVariation(percentageRollout.ruleId as string, { bucketBy, variations })
      )
      const { attribute, negate, op, id, values } = percentageRollout.clauses[0]
      patch.feature.addInstruction(
        patch.creators.updateClause(percentageRollout.ruleId as string, id as string, {
          attribute,
          negate,
          op,
          values
        })
      )
    })
  }
 
  const createRemovePercentageRolloutInstruction = (percentageRollout: VariationPercentageRollout): void => {
    patch.feature.addInstruction(patch.creators.removeRule(percentageRollout.ruleId as string))
  }
 
  return {
    hasFlagStateChanged,
    hasDefaultOnVariationChanged,
    hasDefaultOffVariationChanged,
    createUpdateFlagStateInstruction,
    addedTargetGroups,
    removedTargetGroups,
    addedTargets,
    removedTargets,
    updatedPercentageRollouts,
    createDefaultServeOnInstruction,
    createDefaultServeOffInstruction,
    createAddTargetGroupInstructions,
    createRemoveTargetGroupsInstructions,
    createAddTargetsInstructions,
    createRemoveTargetsInstructions,
    createAddPercentageRolloutInstructions,
    createUpdatePercentageRolloutInstructions,
    createRemovePercentageRolloutInstruction
  }
}