All files / modules/35-connectors/pages/connectors/utils RequestUtils.tsx

85.92% Statements 61/71
70% Branches 147/210
95.24% Functions 20/21
83.87% Lines 52/62

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                  10x                   10x 1x 1x 1x 1x 1x                             10x                           1x               1x 1x                                       10x   10x 5x 1x 4x 1x   2x     3x 1x 2x 1x   1x     10x                                           10x           1x       2x 2x       1x       2x 2x       1x         10x 2x     4x     10x 10x 10x 10x     10x 19x                 19x           10x       86x 44x   6x     6x 6x     42x 42x   9x 9x            
/*
 * 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 type { MultiSelectOption } from '@wings-software/uicore'
 
import { StringUtils } from '@common/exports'
import type {
  ConnectorFilterProperties,
  FilterDTO,
  ConnectorStatusStatistics,
  ConnectorTypeStatistics,
  ResponseConnectorStatistics
} from 'services/cd-ng'
import type { FilterDataInterface, FilterInterface } from '@common/components/Filter/Constants'
 
export const getValidFilterArguments = (formData: Record<string, any>): ConnectorFilterProperties => {
  const typeOptions = formData?.types?.map((type: MultiSelectOption) => type?.value)
  const statusOptions = formData?.connectivityStatuses
    ?.filter((status: MultiSelectOption) => status?.value !== 'NA')
    .map((status: MultiSelectOption) => status?.value)
  return {
    connectorNames: formData?.connectorNames || [],
    connectorIdentifiers: formData?.connectorIdentifiers || [],
    description: formData?.description || '',
    types: typeOptions,
    connectivityStatuses: statusOptions,
    tags: formData?.tags
  }
}
 
export type ConnectorFormType = Omit<ConnectorFilterProperties, 'types' | 'connectivityStatuses'> & {
  types?: MultiSelectOption[]
  connectivityStatuses?: MultiSelectOption[]
}
 
export const createRequestBodyPayload = ({
  isUpdate,
  data,
  projectIdentifier,
  orgIdentifier
}: {
  isUpdate: boolean
  data: FilterDataInterface<ConnectorFormType, FilterInterface>
  projectIdentifier: string
  orgIdentifier: string
}): FilterDTO => {
  const {
    metadata: { name: _name, filterVisibility, identifier },
    formValues
  } = data
  const {
    connectorNames: _connectorNames,
    connectorIdentifiers: _connectorIdentifiers,
    description: _description,
    types: _types,
    connectivityStatuses: _connectivityStatuses,
    tags: _tags
  } = getValidFilterArguments(formValues)
  return {
    name: _name,
    identifier: isUpdate ? identifier : StringUtils.getIdentifierFromName(_name),
    projectIdentifier,
    orgIdentifier,
    filterVisibility: filterVisibility,
    filterProperties: {
      filterType: 'Connector',
      connectorNames: typeof _connectorNames === 'string' ? [_connectorNames] : _connectorNames,
      connectorIdentifiers: typeof _connectorIdentifiers === 'string' ? [_connectorIdentifiers] : _connectorIdentifiers,
      description: _description,
      types: _types,
      connectivityStatuses: _connectivityStatuses,
      tags: _tags
    } as ConnectorFilterProperties
  }
}
 
type supportedTypes = string | number | boolean | unknown
 
const tagSeparator = ' : '
 
export const renderItemByType = (data: supportedTypes | Array<supportedTypes> | unknown): string => {
  if (Array.isArray(data)) {
    return data.join(', ')
  } else if (typeof data === 'object') {
    return Object.entries(data as Record<string, any>)
      .map(([key, value]) => {
        return key.toString().concat(value ? tagSeparator.concat(value.toString()) : '')
      })
      .join(', ')
  } else if (typeof data === 'number') {
    return data.toString()
  } else if (typeof data === 'boolean') {
    return data ? 'true' : 'false'
  }
  return typeof data === 'string' ? data : ''
}
 
export const getAggregatedConnectorFilter = (
  query: string,
  filter: ConnectorFilterProperties
): ConnectorFilterProperties | undefined => {
  let existingNamesInFilter
  if (query) {
    /* istanbul ignore else */
    existingNamesInFilter = filter?.connectorNames
    if (existingNamesInFilter && Array.isArray(existingNamesInFilter) && existingNamesInFilter.length > 0) {
      /* istanbul ignore else */
      Iif (!existingNamesInFilter.includes(query)) {
        /* istanbul ignore else */
        existingNamesInFilter.push(query)
      }
    } else {
      existingNamesInFilter = [query]
    }
  }
  const res = Object.assign(filter, { connectorNames: query ? existingNamesInFilter : filter?.connectorNames })
  return res
}
 
export const validateForm = (
  values: Partial<ConnectorFormType>,
  typeMultiSelectValues: string[],
  connectivityStatusMultiValues: string[],
  metaData: ResponseConnectorStatistics
): { typeErrors: Set<string>; connectivityStatusErrors: Set<string> } => {
  const typeErrors = validateMultiSelectFormInput(
    new Set<string>(typeMultiSelectValues),
    new Set<string>(
      values?.types?.map(
        (type: MultiSelectOption) => type?.value as string,
        getOptionsForMultiSelect(ConnectorStatCategories.STATUS, metaData || {})?.map(option => option.value)
      )
    )
  )
  const connectivityStatusErrors = validateMultiSelectFormInput(
    new Set<string>(connectivityStatusMultiValues),
    new Set(
      values?.connectivityStatuses?.map(
        (status: MultiSelectOption) => status?.value as string,
        getOptionsForMultiSelect(ConnectorStatCategories.STATUS, metaData || {})?.map(option => option.value)
      )
    )
  )
  return {
    typeErrors,
    connectivityStatusErrors
  }
}
const validateMultiSelectFormInput = (allowedValues?: Set<string>, inputValues?: Set<string>): Set<string> => {
  Iif (allowedValues?.size === 0 || inputValues?.size === 0) {
    return new Set<string>()
  }
  return new Set<string>([...(inputValues || new Set<string>())].filter(value => !allowedValues?.has(value)))
}
 
export const enum ConnectorStatCategories {
  STATUS = 'STATUS',
  TYPE = 'TYPE',
  TAGS = 'TAGS'
}
 
export const createOption = (val: string, count?: number): MultiSelectOption => {
  const valueSubLabel = count
    ? count > 0
      ? val
          .concat(' ')
          .concat('(')
          .concat((count || '').toString())
          .concat(')')
      : val
    : val
  return {
    label: valueSubLabel,
    value: val
  } as MultiSelectOption
}
 
export const getOptionsForMultiSelect = (
  category: ConnectorStatCategories,
  metaData: ResponseConnectorStatistics
): MultiSelectOption[] => {
  if (category === ConnectorStatCategories.STATUS) {
    return (
      metaData?.data?.statusStats
        ?.filter((item: ConnectorStatusStatistics) => item?.status)
        ?.map((item: ConnectorStatusStatistics) => {
          //TODO @vardan make it match mocks when label accepts custom renderer as well
          const val = item?.status || ''
          return createOption(val, item?.count)
        }) || []
    )
  } else Eif (category === ConnectorStatCategories.TYPE) {
    return (
      metaData?.data?.typeStats?.map((item: ConnectorTypeStatistics) => {
        const val = item?.type || 'NA'
        return createOption(val, item?.count)
      }) || []
    )
  }
  return []
}