All files / modules/85-cv/pages/health-source/connectors/CustomHealthSource/components/QueryMapping QueryMapping.utils.ts

95.65% Statements 22/23
78.79% Branches 26/33
100% Functions 2/2
95.24% Lines 20/21

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                27x                 27x   27x                 3x       3x             3x 2x   3x 3x 3x       27x               10x         10x 10x 10x 8x 8x   10x 9x   10x          
/*
 * 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 type { MutateMethod } from 'restful-react'
import { getScopeFromValue, getIdentifierFromValue } from '@common/components/EntityReference/EntityReference'
import type { UseGetConnectorProps } from 'services/cd-ng'
import type {
  CustomHealthSampleDataRequest,
  CustomHealthRequestDefinition,
  FetchSampleDataQueryParams,
  ResponseObject,
  TimestampInfo
} from 'services/cv'
import { Scope } from '@common/interfaces/SecretsInterface'
 
export const onFetchRecords = async (
  urlPath?: string,
  endTime?: TimestampInfo,
  startTime?: TimestampInfo,
  requestMethod?: CustomHealthRequestDefinition['method'],
  query?: string,
  getSampleData?: MutateMethod<ResponseObject, CustomHealthSampleDataRequest, FetchSampleDataQueryParams, void>,
  onFetchRecordsSuccess?: (data: { [key: string]: { [key: string]: any } }) => void
): Promise<void> => {
  Iif (!urlPath || !endTime || !startTime) {
    return
  }
 
  const payload: CustomHealthSampleDataRequest = {
    method: requestMethod || 'GET',
    urlPath,
    endTime,
    startTime
  }
 
  if (query && requestMethod === 'POST') {
    payload['body'] = query
  }
  const recordsvalue = await getSampleData?.(payload)
  Eif (recordsvalue?.data) {
    onFetchRecordsSuccess?.(recordsvalue?.data)
  }
}
 
export const connectorParams = (
  connectorIdentifier: string,
  {
    projectIdentifier,
    orgIdentifier,
    accountId
  }: { projectIdentifier: string; orgIdentifier: string; accountId: string }
): UseGetConnectorProps => {
  const queryParams = { accountIdentifier: accountId } as {
    projectIdentifier: string
    orgIdentifier: string
    accountIdentifier: string
  }
  let scope = getScopeFromValue(connectorIdentifier)
  const identifier = getIdentifierFromValue(connectorIdentifier)
  if (scope === Scope.PROJECT) {
    queryParams.projectIdentifier = projectIdentifier
    scope = Scope.ORG
  }
  if (scope === Scope.ORG) {
    queryParams.orgIdentifier = orgIdentifier
  }
  return {
    identifier,
    queryParams
  }
}