All files / modules/75-cf/utils UsageLimitUtils.ts

100% Statements 23/23
100% Branches 23/23
100% Functions 1/1
100% Lines 22/22

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              1x 1x               1x           17x   17x 17x   17x 14x 14x   14x 14x   14x 7x 3x                 4x 2x                 9x 7x 3x               4x 2x               7x        
/*
 * 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 { BannerType } from '@common/layouts/Constants'
import { formatToCompactNumber } from '@cf/utils/CFUtils'
import type { UseStringsReturn } from 'framework/strings'
 
interface BannerText {
  message?: string
  bannerType: BannerType
}
 
export const getBannerText = (
  getString: UseStringsReturn['getString'],
  additionalLicenseProps: Record<string, any>,
  count?: number,
  limit?: number
): BannerText => {
  const { isEnterpriseEdition, isFreeEdition, isTeamEdition } = additionalLicenseProps
 
  const clientMauUsageCount = Number(count)
  const clientMauPlanLimit = Number(limit)
 
  if (!isNaN(clientMauPlanLimit) && !isNaN(clientMauUsageCount)) {
    const clientMauUsagePercentage = Math.trunc((clientMauUsageCount / clientMauPlanLimit) * 100)
    const clientMauPlanLimitFormatted = formatToCompactNumber(clientMauPlanLimit)
 
    const showInfoBanner = clientMauPlanLimit > 0 && clientMauUsagePercentage >= 90 && clientMauUsagePercentage < 100
    const showWarningBanner = clientMauPlanLimit > 0 && clientMauUsagePercentage >= 100
 
    if (isFreeEdition) {
      if (showInfoBanner) {
        return {
          message: getString('cf.planEnforcement.freePlan.approachingLimit', {
            clientMauUsagePercentage,
            clientMauPlanLimitFormatted
          }),
          bannerType: BannerType.INFO
        }
      }
 
      if (showWarningBanner) {
        return {
          message: getString('cf.planEnforcement.freePlan.upgradeRequired', {
            clientMauPlanLimitFormatted
          }),
          bannerType: BannerType.LEVEL_UP
        }
      }
    }
 
    if (isTeamEdition || isEnterpriseEdition) {
      if (showInfoBanner) {
        return {
          message: getString('cf.planEnforcement.teamEnterprisePlan.approachingLimit', {
            clientMauUsagePercentage
          }),
          bannerType: BannerType.INFO
        }
      }
 
      if (showWarningBanner) {
        return {
          message: getString('cf.planEnforcement.teamEnterprisePlan.upgradeRequired'),
          bannerType: BannerType.LEVEL_UP
        }
      }
    }
  }
 
  return {
    bannerType: BannerType.INFO
  }
}