From c80cb6e8e7459c6bb57b9ba3f0c6f3de13550131 Mon Sep 17 00:00:00 2001 From: Jason Lin Date: Tue, 29 Aug 2023 15:44:22 -0400 Subject: [PATCH] Move validateContractsAndConvert to helper file. --- .../contractAndRates/resolverHelpers.ts | 62 ++++++++++++++++++ .../indexHealthPlanPackages.ts | 63 +------------------ 2 files changed, 64 insertions(+), 61 deletions(-) create mode 100644 services/app-api/src/resolvers/healthPlanPackage/contractAndRates/resolverHelpers.ts diff --git a/services/app-api/src/resolvers/healthPlanPackage/contractAndRates/resolverHelpers.ts b/services/app-api/src/resolvers/healthPlanPackage/contractAndRates/resolverHelpers.ts new file mode 100644 index 0000000000..bcf66f0bda --- /dev/null +++ b/services/app-api/src/resolvers/healthPlanPackage/contractAndRates/resolverHelpers.ts @@ -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 } diff --git a/services/app-api/src/resolvers/healthPlanPackage/indexHealthPlanPackages.ts b/services/app-api/src/resolvers/healthPlanPackage/indexHealthPlanPackages.ts index 0189dbc9b3..f2d8dcad9e 100644 --- a/services/app-api/src/resolvers/healthPlanPackage/indexHealthPlanPackages.ts +++ b/services/app-api/src/resolvers/healthPlanPackage/indexHealthPlanPackages.ts @@ -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' @@ -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, @@ -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