All files / modules/85-cv/pages/health-source/HealthSourceDrawer/component/customiseHealthSource CustomiseHealthSource.tsx

70.59% Statements 24/34
35.42% Branches 17/48
75% Functions 3/4
70.59% Lines 24/34

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              22x 22x 22x 22x 22x 22x           22x 22x 22x   22x   22x       22x             1x 1x 1x 1x 1x     1x           1x 1x       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 React, { useContext, useMemo } from 'react'
import { omit } from 'lodash-es'
import { useParams } from 'react-router-dom'
import { useToaster } from '@wings-software/uicore'
import { useStrings } from 'framework/strings'
import {
  MonitoredServiceDTO,
  MonitoredServiceResponse,
  useSaveMonitoredService,
  useUpdateMonitoredService
} from 'services/cv'
import { getErrorMessage } from '@cv/utils/CommonUtils'
import { BGColorWrapper } from '@cv/pages/health-source/common/StyledComponents'
import { SetupSourceTabsContext } from '@cv/components/CVSetupSourcesView/SetupSourceTabs/SetupSourceTabs'
import type { ProjectPathProps } from '@common/interfaces/RouteInterfaces'
import { LoadSourceByType, createHealthsourceList } from './CustomiseHealthSource.utils'
import type { SourceDataInterface, UpdatedHealthSource } from '../../HealthSourceDrawerContent.types'
import { omitServiceEnvironmentKeys } from './CustomiseHealthSource.constant'
 
type ExcludedSourceDataKeys = 'isEdit' | 'serviceRef' | 'environmentRef' | 'monitoredServiceRef' | 'changeSources'
 
export default function CustomiseHealthSource({
  onSuccess,
  shouldRenderAtVerifyStep
}: {
  onSuccess: (data: MonitoredServiceResponse | UpdatedHealthSource) => void
  shouldRenderAtVerifyStep?: boolean
}): JSX.Element {
  const params = useParams<ProjectPathProps & { identifier: string }>()
  const { getString } = useStrings()
  const { showError, showSuccess } = useToaster()
  const { sourceData } = useContext(SetupSourceTabsContext)
  const { mutate: saveMonitoredService } = useSaveMonitoredService({
    queryParams: { accountId: params.accountId }
  })
  const { mutate: updateMonitoredService } = useUpdateMonitoredService({
    identifier: sourceData?.monitoredServiceRef?.identifier,
    queryParams: { accountId: params.accountId }
  })
 
  // Removing Service and Environment keys
  const filteredSourceData: Omit<SourceDataInterface, ExcludedSourceDataKeys> = useMemo(
    () => omit(sourceData, omitServiceEnvironmentKeys),
    [sourceData]
  )
 
  const isEdit = useMemo(
    () => params?.identifier || shouldRenderAtVerifyStep,
    [params?.identifier, shouldRenderAtVerifyStep]
  )
 
  const submitData = async (formdata: any, healthSourcePayload: UpdatedHealthSource): Promise<void> => {
    if (shouldRenderAtVerifyStep) {
      const healthSourceList = createHealthsourceList(formdata, healthSourcePayload)
      const { identifier, name, description = '', tags = {} } = formdata?.monitoredServiceRef
      try {
        const payload: MonitoredServiceDTO = {
          orgIdentifier: params.orgIdentifier,
          projectIdentifier: params.projectIdentifier,
          serviceRef: sourceData.serviceRef,
          environmentRef: sourceData.environmentRef,
          identifier: identifier?.trim(),
          name,
          description,
          tags,
          type: 'Application',
          sources: {
            healthSources: healthSourceList,
            changeSources: sourceData.changeSources
          }
        }
        // From verify step it will be always update call since monitored service will already be created.
        // This flow will be triggered only when user is adding health source to existing monitored service
        const postdatavalue = isEdit ? await updateMonitoredService(payload) : await saveMonitoredService(payload)
        postdatavalue?.resource && onSuccess(postdatavalue?.resource)
        showSuccess(
          isEdit
            ? getString('cv.monitoredServices.monitoredServiceUpdated')
            : getString('cv.monitoredServices.monitoredServiceCreated')
        )
      } catch (error) {
        showError(getErrorMessage(error))
      }
    } else {
      onSuccess(healthSourcePayload)
    }
  }
 
  return (
    <BGColorWrapper>
      <LoadSourceByType type={sourceData?.sourceType} data={filteredSourceData} onSubmit={submitData} />
    </BGColorWrapper>
  )
}