All files / modules/33-auth-settings/pages/subscriptions/overview SubscriptionUtils.tsx

98.51% Statements 66/67
82% Branches 82/100
100% Functions 11/11
98.44% Lines 63/64

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 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327              2x 2x 2x 2x   2x 2x 2x               2x                                         18x 5x     13x             2x 21x 21x                   2x 2x 2x               2x             11x 11x 11x                     2x         18x   18x       18x                   2x                             18x 18x             18x   18x     18x                                       17x   2x 2x 2x 2x                       3x   3x     3x                 4x 4x 4x 4x   4x       4x                 4x   4x     4x                 4x         2x         17x 17x 17x               2x 3x 3x                             2x 1x 1x   1x               1x                   2x                                     17x                                        
/*
 * 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 React from 'react'
import { capitalize } from 'lodash-es'
import { Text, Layout, Icon } from '@wings-software/uicore'
import { Color } from '@harness/design-system'
import type { StringsMap } from 'stringTypes'
import { useStrings } from 'framework/strings'
import { Editions, ModuleLicenseType, CD_LICENSE_TYPE } from '@common/constants/SubscriptionTypes'
import { ModuleName } from 'framework/types/ModuleName'
import type {
  CDModuleLicenseDTO,
  CEModuleLicenseDTO,
  CFModuleLicenseDTO,
  CIModuleLicenseDTO,
  ModuleLicenseDTO
} from 'services/cd-ng'
import css from './SubscriptionDetailsCard.module.scss'
 
interface SubscriptionDetailsCardExpiryDateProps {
  isTrial: boolean
  isFreeOrCommunity: boolean
  expiryDate?: string
}
 
function getExpiryMsg({
  isFreeOrCommunity,
  isExpired,
  expiredDays,
  licenseType,
  days
}: {
  isFreeOrCommunity: boolean
  isExpired: boolean
  expiredDays?: number
  licenseType?: ModuleLicenseDTO['licenseType']
  days?: number
}): React.ReactElement | undefined {
  if (isFreeOrCommunity) {
    return undefined
  }
 
  return isExpired ? (
    <ExpiredMessage expiredDays={expiredDays} />
  ) : (
    <ExpiryCountdownMessage licenseType={licenseType} days={days} />
  )
}
 
const AccountName = ({ accountName }: { accountName?: string }): React.ReactElement => {
  const { getString } = useStrings()
  return (
    <React.Fragment key="account">
      <Text color={Color.GREY_600}>{getString('common.accountName')}</Text>
      <Text color={Color.BLACK} font={{ weight: 'semi-bold' }}>
        {accountName}
      </Text>
    </React.Fragment>
  )
}
 
const ExpiredMessage = ({ expiredDays }: { expiredDays?: number }): React.ReactElement => {
  const { getString } = useStrings()
  return (
    <Layout.Horizontal spacing="small">
      <Icon className={css.expiryIcon} size={12} name={'warning-sign'} color={Color.RED_700} />
      <Text color={Color.RED_700}>{getString('common.subscriptions.expired', { days: expiredDays })}</Text>
    </Layout.Horizontal>
  )
}
 
const ExpiryCountdownMessage = ({
  licenseType,
  days
}: {
  licenseType?: ModuleLicenseDTO['licenseType']
  days?: number
}): React.ReactElement | null => {
  const { getString } = useStrings()
  Eif (licenseType !== ModuleLicenseType.PAID || (days && days < 14)) {
    return (
      <Layout.Horizontal spacing="small">
        <Icon className={css.expiryIcon} size={12} name={'warning-sign'} color={Color.ORANGE_800} />
        <Text color={Color.ORANGE_800}>{getString('common.subscriptions.expiryCountdown', { days })}</Text>
      </Layout.Horizontal>
    )
  }
 
  return null
}
 
const SubscriptionDetailsCardExpiryDate = ({
  isTrial,
  isFreeOrCommunity,
  expiryDate
}: SubscriptionDetailsCardExpiryDateProps): React.ReactElement => {
  const { getString } = useStrings()
 
  const expiryStr = isTrial
    ? getString('common.subscriptions.overview.trialExpiry')
    : getString('common.extendTrial.expiryDate')
 
  return (
    <React.Fragment key="expiry">
      <Text color={Color.GREY_600}>{expiryStr}</Text>
      <Text color={Color.BLACK} font={{ weight: 'semi-bold' }}>
        {isFreeOrCommunity ? getString('common.subscriptions.overview.freeExpiry') : expiryDate}
      </Text>
    </React.Fragment>
  )
}
 
const SubscriptionDetailsCardPlan = ({
  isFreeOrCommunity,
  isExpired,
  expiredDays,
  licenseType,
  days,
  edition
}: {
  isFreeOrCommunity: boolean
  isExpired: boolean
  expiredDays?: number
  licenseType?: ModuleLicenseDTO['licenseType']
  days?: number
  edition: Editions
}): React.ReactElement => {
  const { getString } = useStrings()
  const expiryMessage = getExpiryMsg({
    isFreeOrCommunity,
    isExpired,
    expiredDays,
    licenseType,
    days
  })
  const editionStr = capitalize(edition)
  const planMessage =
    licenseType === ModuleLicenseType.PAID || isFreeOrCommunity
      ? getString('common.subscriptions.paid', { edition: editionStr })
      : getString('common.subscriptions.trial', { edition: editionStr })
  return (
    <React.Fragment key="plan">
      <Text color={Color.GREY_600}>{getString('common.subscriptions.overview.plan')}</Text>
      <Layout.Vertical>
        <Text color={Color.BLACK} font={{ weight: 'semi-bold' }}>
          {planMessage}
        </Text>
        {expiryMessage}
      </Layout.Vertical>
    </React.Fragment>
  )
}
 
function getLicenseCountByModule({
  licenseData,
  getString
}: {
  licenseData?: ModuleLicenseDTO
  getString: (key: keyof StringsMap, vars?: Record<string, any> | undefined) => string
}): React.ReactElement | undefined {
  switch (licenseData?.moduleType) {
    case ModuleName.CF: {
      const cfModuleLicenseDTO = licenseData as CFModuleLicenseDTO
      const featureFlagUsers = cfModuleLicenseDTO?.numberOfUsers?.toLocaleString()
      const monthlyActiveUsers = cfModuleLicenseDTO?.numberOfClientMAUs?.toLocaleString()
      return (
        <Layout.Vertical spacing="medium">
          <Text color={Color.BLACK} font={{ weight: 'semi-bold' }} margin={{ bottom: 5 }}>
            {getString('common.subscriptions.featureFlags.users', { users: featureFlagUsers })}
          </Text>
          <Text color={Color.BLACK} font={{ weight: 'semi-bold' }}>
            {getString('common.subscriptions.featureFlags.mau', { maus: monthlyActiveUsers })}
          </Text>
        </Layout.Vertical>
      )
    }
    case ModuleName.CI: {
      const ciModuleLicenseDTO = licenseData as CIModuleLicenseDTO
      const committers =
        ciModuleLicenseDTO?.numberOfCommitters === -1
          ? getString('common.unlimited')
          : ciModuleLicenseDTO?.numberOfCommitters?.toLocaleString()
      return (
        <Layout.Vertical spacing="medium">
          <Text color={Color.BLACK} font={{ weight: 'semi-bold' }} margin={{ bottom: 5 }}>
            {getString('common.subscriptions.ci.developers', { developers: committers })}
          </Text>
        </Layout.Vertical>
      )
    }
    case ModuleName.CD: {
      const cdModuleLicenseDTO = licenseData as CDModuleLicenseDTO
      const workloads = cdModuleLicenseDTO?.workloads?.toLocaleString()
      const serviceInstances = cdModuleLicenseDTO?.serviceInstances?.toLocaleString()
      const cdLicenseType = cdModuleLicenseDTO?.cdLicenseType
      const serviceStr =
        cdLicenseType === CD_LICENSE_TYPE.SERVICES
          ? getString('common.subscriptions.cd.services', { workloads: workloads })
          : getString('common.subscriptions.cd.serviceInstances', { workloads: serviceInstances })
 
      return (
        <Layout.Vertical spacing="medium">
          <Text color={Color.BLACK} font={{ weight: 'semi-bold' }} margin={{ bottom: 5 }}>
            {serviceStr}
          </Text>
        </Layout.Vertical>
      )
    }
    case ModuleName.CE: {
      const ceModuleLicenseDTO = licenseData as CEModuleLicenseDTO
      const spendLimit =
        ceModuleLicenseDTO?.spendLimit === -1
          ? getString('common.unlimited')
          : `$${ceModuleLicenseDTO?.spendLimit?.toLocaleString()}`
      return (
        <Layout.Vertical spacing="medium">
          <Text color={Color.BLACK} font={{ weight: 'semi-bold' }} margin={{ bottom: 5 }}>
            {getString('common.subscriptions.ccm.cloudSpend', { spendLimit: spendLimit })}
          </Text>
        </Layout.Vertical>
      )
    }
    default: {
      return undefined
    }
  }
}
 
const SubscriptionDetailsCardLicenseCount = ({
  licenseData
}: {
  licenseData?: ModuleLicenseDTO
}): React.ReactElement => {
  const { getString } = useStrings()
  const licenseCount = getLicenseCountByModule({ licenseData, getString })
  return (
    <React.Fragment key="licenseCount">
      <Text color={Color.GREY_600}>{getString('common.account.licenseCount')}</Text>
      {licenseCount}
    </React.Fragment>
  )
}
 
export const NoSubscriptionDetailsCardInfo = ({ accountName }: { accountName?: string }): React.ReactElement => {
  const { getString } = useStrings()
  return (
    <div className={css.detailFields}>
      <AccountName accountName={accountName} />
      <React.Fragment key="plan">
        <Text color={Color.GREY_600}>{getString('common.subscriptions.overview.plan')}</Text>
        <Layout.Vertical>
          <Text color={Color.BLACK} font={{ weight: 'semi-bold' }}>
            {getString('common.subscriptions.noActiveSubscription')}
          </Text>
        </Layout.Vertical>
      </React.Fragment>
    </div>
  )
}
 
export const CommunitySubscriptionDetailsCardInfo = ({ accountName }: { accountName?: string }): React.ReactElement => {
  const { getString } = useStrings()
  const serviceType = getString('authSettings.onprem')
  const serviceTypeElement = (
    <React.Fragment key="service-type">
      <Text color={Color.GREY_600}>{getString('common.serviceType')}</Text>
      <Text color={Color.BLACK} font={{ weight: 'semi-bold' }}>
        {serviceType}
      </Text>
    </React.Fragment>
  )
 
  return (
    <div className={css.detailFields}>
      <AccountName accountName={accountName} />
      {serviceTypeElement}
      <SubscriptionDetailsCardPlan isFreeOrCommunity isExpired={false} edition={Editions.COMMUNITY} />
      <SubscriptionDetailsCardExpiryDate isTrial={false} isFreeOrCommunity />
    </div>
  )
}
 
export const SubscriptionDetailsCardInfo = ({
  accountName,
  isFreeOrCommunity,
  isExpired,
  expiredDays,
  days,
  edition,
  licenseData,
  expiryDate
}: {
  accountName?: string
  isFreeOrCommunity: boolean
  isExpired: boolean
  expiredDays?: number
  days?: number
  edition: Editions
  licenseData?: ModuleLicenseDTO
  expiryDate: string
}): React.ReactElement => {
  return (
    <div className={css.detailFields}>
      <AccountName accountName={accountName} />
      <SubscriptionDetailsCardPlan
        isFreeOrCommunity={isFreeOrCommunity}
        isExpired={isExpired}
        expiredDays={expiredDays}
        licenseType={licenseData?.licenseType}
        days={days}
        edition={edition}
      />
      <SubscriptionDetailsCardLicenseCount licenseData={licenseData} />
      <SubscriptionDetailsCardExpiryDate
        isTrial={licenseData?.licenseType === ModuleLicenseType.TRIAL}
        isFreeOrCommunity={isFreeOrCommunity}
        expiryDate={expiryDate}
      />
    </div>
  )
}