All files / modules/70-pipeline/factories/FeatureRestrictionBannersFactory FeatureRestrictionBannersFactoryUtils.ts

100% Statements 21/21
71.43% Branches 40/56
100% Functions 3/3
100% Lines 19/19

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              1x     1x                                           1x                 24x 22x 22x     29x         10x       14x     1x                         24x 24x 11x                 11x 7x   7x             7x 7x                     17x    
/*
 * Copyright 2021 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 { isEmpty } from 'lodash-es'
import type { StringsMap } from 'stringTypes'
import type { CheckFeatureReturn, FeatureDetail } from 'framework/featureStore/featureStoreUtil'
import { FeatureIdentifier } from 'framework/featureStore/FeatureIdentifier'
import type {
  FeatureWarningTheme,
  DisplayBanner,
  RedirectButton
} from '@common/components/FeatureWarning/FeatureWarningCommonBannerUtils'
import type { UseStringsReturn } from 'framework/strings'
 
interface Dependency {
  [key: string]: { enabled?: boolean }
}
 
export type ModuleToFeatureMapValue = {
  theme: FeatureWarningTheme
  bannerKey: string
  limitCrossedMessage: keyof StringsMap
  limit?: number
  limitPercent?: number
  dependency?: Dependency
  redirectButtons?: RedirectButton[]
}
 
export const getBannerDependencyMet = ({
  features,
  featureName,
  dependency
}: {
  featureName?: FeatureIdentifier
  features: Map<FeatureIdentifier, CheckFeatureReturn>
  dependency?: Dependency
}): boolean => {
  if (dependency) {
    const dependencyKeys = Object.entries(dependency)
    if (
      dependencyKeys?.some(
        ([key, value]) =>
          features.get(key as FeatureIdentifier)?.enabled !== value.enabled ||
          (featureName === FeatureIdentifier.ACTIVE_COMMITTERS &&
            isEmpty(features.get(key as FeatureIdentifier)?.featureDetail || {}))
      )
    ) {
      return false
    }
  }
 
  return true
}
 
export const getQualifiedEnforcedBanner = ({
  dependencyMet,
  featureDetail,
  uiDisplayBanner,
  getString,
  featureName
}: {
  dependencyMet: boolean
  featureDetail?: FeatureDetail
  uiDisplayBanner: ModuleToFeatureMapValue
  getString: UseStringsReturn['getString']
  featureName: FeatureIdentifier
}): DisplayBanner | undefined => {
  let _isLimitBreached = false
  if (dependencyMet && typeof featureDetail?.count !== 'undefined' && featureDetail.limit) {
    _isLimitBreached =
      typeof uiDisplayBanner?.limit !== 'undefined'
        ? featureDetail.count >= uiDisplayBanner.limit
        : uiDisplayBanner.limitPercent
        ? featureDetail.featureName === FeatureIdentifier.ACTIVE_COMMITTERS
          ? (featureDetail.count / featureDetail.limit) * 100 > uiDisplayBanner.limitPercent
          : (featureDetail.count / featureDetail.limit) * 100 >= uiDisplayBanner.limitPercent
        : false
 
    if (_isLimitBreached) {
      const usagePercent = Math.min(Math.floor((featureDetail.count / featureDetail.limit) * 100), 100)
      const messageString =
        uiDisplayBanner?.limitCrossedMessage &&
        getString(uiDisplayBanner.limitCrossedMessage, {
          usagePercent,
          limit: featureDetail.limit,
          count: featureDetail.count
        })
 
      Eif (messageString && featureDetail?.enabled) {
        return {
          featureName,
          isFeatureRestrictionAllowedForModule: featureDetail.enabled,
          theme: uiDisplayBanner.theme,
          redirectButtons: uiDisplayBanner.redirectButtons,
          bannerKey: uiDisplayBanner.bannerKey,
          messageString
        }
      }
    }
  }
  return undefined
}