Skip to content

Commit

Permalink
Move validateContractsAndConvert to helper file.
Browse files Browse the repository at this point in the history
  • Loading branch information
JasonLin0991 committed Aug 29, 2023
1 parent d261287 commit c80cb6e
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 61 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import type { ContractOrErrorArrayType } from '../../../postgres/contractAndRates'
import type { Span } from '@opentelemetry/api'
import type { HealthPlanPackageType } from '../../../domain-models'
import type { ContractType } from '../../../domain-models/contractAndRates'
import { convertContractToUnlockedHealthPlanPackage } from '../../../domain-models'
import { logError } from '../../../logger'
import { setErrorAttributesOnActiveSpan } from '../../attributeHelper'

const validateContractsAndConvert = (
contractsWithHistory: ContractOrErrorArrayType,
span?: Span
): HealthPlanPackageType[] => {
// separate valid contracts and errors
const parsedContracts: ContractType[] = []
const errorParseContracts: string[] = []
contractsWithHistory.forEach((parsed) => {
if (parsed.contract instanceof Error) {
errorParseContracts.push(
`${parsed.contractID}: ${parsed.contract.message}`
)
} else {
parsedContracts.push(parsed.contract)
}
})

// log all contracts that failed parsing to otel.
if (errorParseContracts.length > 0) {
const errMessage = `Failed to parse the following contracts:\n${errorParseContracts.join(
'\n'
)}`
logError('indexHealthPlanPackagesResolver', errMessage)
setErrorAttributesOnActiveSpan(errMessage, span)
}

// convert contract type to health plan package type and filter out failures
const convertedContracts: HealthPlanPackageType[] = []
const errorConvertContracts: string[] = []
parsedContracts.forEach((contract) => {
const parsedContract =
convertContractToUnlockedHealthPlanPackage(contract)
if (parsedContract instanceof Error) {
errorConvertContracts.push(
`${contract.id}: ${parsedContract.message}`
)
} else {
convertedContracts.push(parsedContract)
}
})

// log all contracts that failed converting
if (errorConvertContracts.length > 0) {
const errMessage = `Failed to covert the following contracts to health plan packages:\n${errorConvertContracts.join(
'\n'
)}`
logError('indexHealthPlanPackagesResolver', errMessage)
setErrorAttributesOnActiveSpan(errMessage, span)
}

return convertedContracts
}

export { validateContractsAndConvert }
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
import type { Span } from '@opentelemetry/api'
import { ForbiddenError } from 'apollo-server-lambda'
import type { HealthPlanPackageType } from '../../domain-models'
import {
isStateUser,
isCMSUser,
isAdminUser,
convertContractToUnlockedHealthPlanPackage,
} from '../../domain-models'
import { isStateUser, isCMSUser, isAdminUser } from '../../domain-models'
import { isHelpdeskUser } from '../../domain-models/user'
import type { QueryResolvers } from '../../gen/gqlServer'
import { logError, logSuccess } from '../../logger'
Expand All @@ -18,9 +13,8 @@ import {
setSuccessAttributesOnActiveSpan,
} from '../attributeHelper'
import type { LDService } from '../../launchDarkly/launchDarkly'
import type { ContractOrErrorArrayType } from '../../postgres/contractAndRates'
import type { ContractType } from '../../domain-models/contractAndRates'
import { GraphQLError } from 'graphql/index'
import { validateContractsAndConvert } from './contractAndRates/resolverHelpers'

const validateAndReturnHealthPlanPackages = (
results: HealthPlanPackageType[] | StoreError,
Expand Down Expand Up @@ -48,59 +42,6 @@ const validateAndReturnHealthPlanPackages = (
return { totalCount: edges.length, edges }
}

const validateContractsAndConvert = (
contractsWithHistory: ContractOrErrorArrayType,
span?: Span
): HealthPlanPackageType[] => {
// separate valid contracts and errors
const parsedContracts: ContractType[] = []
const errorParseContracts: string[] = []
contractsWithHistory.forEach((parsed) => {
if (parsed.contract instanceof Error) {
errorParseContracts.push(
`${parsed.contractID}: ${parsed.contract.message}`
)
} else {
parsedContracts.push(parsed.contract)
}
})

// log all contracts that failed parsing to otel.
if (errorParseContracts.length > 0) {
const errMessage = `Failed to parse the following contracts:\n${errorParseContracts.join(
'\n'
)}`
logError('indexHealthPlanPackagesResolver', errMessage)
setErrorAttributesOnActiveSpan(errMessage, span)
}

// convert contract type to health plan package type and filter out failures
const convertedContracts: HealthPlanPackageType[] = []
const errorConvertContracts: string[] = []
parsedContracts.forEach((contract) => {
const parsedContract =
convertContractToUnlockedHealthPlanPackage(contract)
if (parsedContract instanceof Error) {
errorConvertContracts.push(
`${contract.id}: ${parsedContract.message}`
)
} else {
convertedContracts.push(parsedContract)
}
})

// log all contracts that failed converting
if (errorConvertContracts.length > 0) {
const errMessage = `Failed to covert the following contracts to health plan packages:\n${errorConvertContracts.join(
'\n'
)}`
logError('indexHealthPlanPackagesResolver', errMessage)
setErrorAttributesOnActiveSpan(errMessage, span)
}

return convertedContracts
}

export function indexHealthPlanPackagesResolver(
store: Store,
launchDarkly: LDService
Expand Down

0 comments on commit c80cb6e

Please sign in to comment.