All files / modules/75-cd/utils renderMessageUtils.ts

100% Statements 41/41
82.98% Branches 78/94
100% Functions 5/5
100% Lines 40/40

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              1x 1x                           1x             9x   9x 9x 9x   9x 1x       8x 1x     7x 1x     6x 1x           9x 4x 4x           5x 5x 5x 5x 5x 1x     4x 1x     3x 1x           5x 3x 3x         2x 2x 2x 1x   2x 1x 1x             1x 1x        
/*
 * 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 { BannerType } from '@common/layouts/Constants'
import {
  getActiveUsageNumber,
  isFeatureLimitBreached,
  isFeatureOveruseActive,
  isFeatureWarningActive
} from '@common/layouts/FeatureBanner'
import type { CheckFeatureReturn } from 'framework/featureStore/featureStoreUtil'
import type { UseStringsReturn } from 'framework/strings'
 
interface BannerText {
  message: () => string
  bannerType: BannerType
}
 
export const getBannerText = (
  getString: UseStringsReturn['getString'],
  additionalLicenseProps: Record<string, unknown>,
  serviceFeatureDetail?: CheckFeatureReturn,
  dpmFeatureDetail?: CheckFeatureReturn,
  initialDeploymentsFeatureDetail?: CheckFeatureReturn
): BannerText => {
  const { isFreeEdition: isCDFree, isTeamEdition: isCDTeam } = additionalLicenseProps
  // Check for limit breach
  const isServiceLimitBreached = isFeatureLimitBreached(serviceFeatureDetail)
  const isDpmLimitBreached = isFeatureLimitBreached(dpmFeatureDetail)
  let limitBreachMessageString = ''
 
  if (isServiceLimitBreached && isDpmLimitBreached) {
    limitBreachMessageString = getString('cd.featureRestriction.banners.serviceAndDeploymentsLevelUp', {
      deploymentsLimit: dpmFeatureDetail?.featureDetail?.limit,
      serviceLimit: serviceFeatureDetail?.featureDetail?.limit
    })
  } else if (isServiceLimitBreached && isCDFree) {
    limitBreachMessageString = getString('cd.featureRestriction.banners.serviceLevelUp', {
      serviceLimit: serviceFeatureDetail?.featureDetail?.limit
    })
  } else if (isServiceLimitBreached && isCDTeam) {
    limitBreachMessageString = getString('cd.featureRestriction.banners.serviceLevelUpTeamEnterprise', {
      serviceLimit: serviceFeatureDetail?.featureDetail?.limit
    })
  } else if (isDpmLimitBreached) {
    limitBreachMessageString = getString('cd.featureRestriction.banners.deploymentsPerMonthLevelUp', {
      count: dpmFeatureDetail?.featureDetail?.count,
      deploymentsLimit: dpmFeatureDetail?.featureDetail?.limit
    })
  }
 
  if (limitBreachMessageString) {
    return {
      message: () => limitBreachMessageString,
      bannerType: BannerType.LEVEL_UP
    }
  }
 
  // Checking for limit usage warning
  let warningMessageString = ''
  const isServiceWarningActive = isFeatureWarningActive(serviceFeatureDetail)
  const isDpmWarningActive = isFeatureWarningActive(dpmFeatureDetail)
  const isInitialDeplWarningActive = isFeatureWarningActive(initialDeploymentsFeatureDetail)
  if (isInitialDeplWarningActive) {
    warningMessageString = getString('cd.featureRestriction.banners.initialDeploymentsWarningActive', {
      warningLimit: getActiveUsageNumber(initialDeploymentsFeatureDetail)
    })
  } else if (isServiceWarningActive) {
    warningMessageString = getString('cd.featureRestriction.banners.serviceWarningActive', {
      warningLimit: getActiveUsageNumber(serviceFeatureDetail)
    })
  } else if (isDpmWarningActive) {
    warningMessageString = getString('cd.featureRestriction.banners.dpmWarningActive', {
      count: dpmFeatureDetail?.featureDetail?.count,
      warningLimit: dpmFeatureDetail?.featureDetail?.limit
    })
  }
 
  if (warningMessageString) {
    return {
      message: () => warningMessageString,
      bannerType: BannerType.INFO
    }
  }
 
  let overuseMessageString = ''
  const isServiceOveruseActive = isFeatureOveruseActive(serviceFeatureDetail)
  if (isServiceOveruseActive && isCDTeam) {
    overuseMessageString = getString('cd.featureRestriction.banners.serviceOveruseTeamEnterprise')
  }
  if (overuseMessageString) {
    return {
      message: () => overuseMessageString,
      bannerType: BannerType.OVERUSE
    }
  }
 
  // If neither of limit breach/ warning/ overuse needs to be shown, return with an empty string.
  // This will ensure no banner is shown
  return {
    message: () => '',
    bannerType: BannerType.LEVEL_UP
  }
}