Skip to content

Commit

Permalink
Fix some logic, pull out into helper function
Browse files Browse the repository at this point in the history
  • Loading branch information
elsaperelli committed Jan 4, 2025
1 parent 22a99b5 commit e22c406
Showing 1 changed file with 36 additions and 34 deletions.
70 changes: 36 additions & 34 deletions util/ValueSetHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { TestCaseInfo } from '../state/atoms/patientTestCase';
import fhirpath from 'fhirpath';
import { parsedCodePaths } from '../util/codePaths';
import { DataRequirementsLookupByTypeProps } from '../state/selectors/dataRequirementsLookupByType';
import { CodeableConcept } from 'fhir/r2';

export interface GetValueSetCodesProps {
code?: string;
Expand All @@ -24,6 +25,39 @@ function getValueSetCodes(valueSetUrl: string[], mb: fhir4.Bundle | null): GetVa
return codesAndSystems;
}

/**
* Helper function for repeated logic in minimizeTestCaseResources that takes a primaryCodeValue,
* a resource, a matching DataRequirement of the same resourceType, a measureBundle, and a newResources
* array, checks that the primaryCodeValue exists and if the primaryCodeValue matches any of the
* directCodes or ValueSet codes of a DataRequirement of the same resourceType, the resource is added
* to the newResources array
*/
function checkCodesAndValueSets(
primaryCodeValue: fhir4.CodeableConcept,
matchingDRType: DataRequirementsLookupByTypeProps,
measureBundle: fhir4.Bundle | null,
resource: fhir4.BundleEntry,
newResources: fhir4.BundleEntry[]
) {
if (primaryCodeValue) {
if (
matchingDRType.directCodes.length > 0 &&
matchingDRType.directCodes.find(dc => primaryCodeValue.coding?.find(c => c.code === dc.code))
) {
newResources.push(resource);
} else if (matchingDRType.valueSets.length > 0) {
const vsCodesAndSystems = getValueSetCodes(matchingDRType.valueSets, measureBundle);
if (
vsCodesAndSystems.find(vscas =>
primaryCodeValue.coding?.find(c => c.code === vscas.code && c.system === vscas.system)
)
) {
newResources.push(resource);
}
}
}
}

/**
* Helper function that takes in a TestCase, a measure bundle, and the
* current measure bundle's dataRequirements lookup by type object and returns
Expand Down Expand Up @@ -67,23 +101,7 @@ export function minimizeTestCaseResources(
r.resource,
`${codeInfo.primaryCodePath}CodeableConcept`
)[0] as fhir4.CodeableConcept;
if (primaryCodeValue) {
if (matchingDRType.valueSets.length > 0) {
const vsCodesAndSystems = getValueSetCodes(matchingDRType.valueSets, measureBundle);
if (
vsCodesAndSystems.find(vscas =>
primaryCodeValue.coding?.find(c => c.code === vscas.code && c.system === vscas.system)
)
) {
newResources.push(r);
}
}
if (matchingDRType.directCodes.length > 0) {
if (matchingDRType.directCodes.find(dc => primaryCodeValue.coding?.find(c => c.code === dc.code))) {
newResources.push(r);
}
}
}
checkCodesAndValueSets(primaryCodeValue, matchingDRType, measureBundle, r, newResources);
}
} else {
if (primaryCodeInfo.multipleCardinality === true) {
Expand Down Expand Up @@ -118,23 +136,7 @@ export function minimizeTestCaseResources(
r.resource,
codeInfo.primaryCodePath
)[0] as fhir4.CodeableConcept;
if (primaryCodeValue) {
if (matchingDRType.valueSets.length > 0) {
const vsCodesAndSystems = getValueSetCodes(matchingDRType.valueSets, measureBundle);
if (
vsCodesAndSystems.find(vscas =>
primaryCodeValue.coding?.find(c => c.code === vscas.code && c.system === vscas.system)
)
) {
newResources.push(r);
}
}
if (matchingDRType.directCodes.length > 0) {
if (matchingDRType.directCodes.find(dc => primaryCodeValue.coding?.find(c => c.code === dc.code))) {
newResources.push(r);
}
}
}
checkCodesAndValueSets(primaryCodeValue, matchingDRType, measureBundle, r, newResources);
}
}
} else if (primaryCodeInfo.codeType === 'FHIR.Coding') {
Expand Down

0 comments on commit e22c406

Please sign in to comment.