Skip to content

Commit

Permalink
Pulled out handling stratifier results
Browse files Browse the repository at this point in the history
  • Loading branch information
elsaperelli committed Aug 7, 2024
1 parent cd80e35 commit 0f94176
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 51 deletions.
58 changes: 42 additions & 16 deletions src/calculation/DetailedResultsBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ export function createPopulationValues(
populationResults = popAndStratResults.populationResults;
stratifierResults = popAndStratResults.stratifierResults;
populationResults = handlePopulationValues(populationResults, populationGroup, measureScoringCode);
if (stratifierResults) {
stratifierResults = handleStratificationValues(populationGroup, populationResults, stratifierResults);
}
} else {
// episode of care based measure
// collect results per episode
Expand All @@ -45,6 +48,9 @@ export function createPopulationValues(
const popAndStratResults = createPatientPopulationValues(populationGroup, patientResults);
populationResults = popAndStratResults.populationResults;
stratifierResults = popAndStratResults.stratifierResults;
if (stratifierResults) {
stratifierResults = handleStratificationValues(populationGroup, populationResults, stratifierResults);
}
} else {
populationResults = [];
stratifierResults = [];
Expand Down Expand Up @@ -97,6 +103,9 @@ export function createPopulationValues(
}
});
});
if (stratifierResults) {
stratifierResults = handleStratificationValues(populationGroup, populationResults, stratifierResults);
}
}
}
const detailedResult: DetailedPopulationGroupResult = {
Expand Down Expand Up @@ -304,27 +313,11 @@ export function createPatientPopulationValues(
populationGroup.stratifier.forEach(strata => {
if (strata.criteria?.expression) {
const value = patientResults[strata.criteria?.expression];

// if the cqfm-appliesTo extension is present, then we want to consider the result of that
// population in our stratifier result
const appliesToExtension = strata.extension?.find(
e => e.url === 'http://hl7.org/fhir/us/cqfmeasures/StructureDefinition/cqfm-appliesTo'
);

let popValue = true;
if (appliesToExtension) {
const popCode = appliesToExtension.valueCodeableConcept?.coding?.[0].code;
if (popCode) {
popValue = patientResults[popCode];
}
}
const result = isStatementValueTruthy(value);
const appliesResult = isStatementValueTruthy(value && popValue);

stratifierResults?.push({
strataCode: strata.code?.text ?? strata.id ?? `strata-${strataIndex++}`,
result,
appliesResult,
...(strata.id ? { strataId: strata.id } : {})
});
}
Expand All @@ -337,6 +330,39 @@ export function createPatientPopulationValues(
};
}

export function handleStratificationValues(
populationGroup: fhir4.MeasureGroup,
populationResults: PopulationResult[],
stratifierResults: StratifierResult[]
): StratifierResult[] {
if (populationGroup.stratifier) {
populationGroup.stratifier.forEach(strata => {
if (strata.criteria?.expression) {

Check warning on line 340 in src/calculation/DetailedResultsBuilder.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🌿 Branch is not covered

Warning! Not covered branch
const strataResult = stratifierResults.find(strataRes => strataRes.strataId === strata.id);

if (strataResult) {
// if the cqfm-appliesTo extension is present, then we want to consider the result of that
// population in our stratifier result
const appliesToExtension = strata.extension?.find(
e => e.url === 'http://hl7.org/fhir/us/cqfmeasures/StructureDefinition/cqfm-appliesTo'
);

let popValue = true;
if (appliesToExtension) {
const popCode = appliesToExtension.valueCodeableConcept?.coding?.[0].code;

Check warning on line 352 in src/calculation/DetailedResultsBuilder.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🌿 Branch is not covered

Warning! Not covered branch

Check warning on line 352 in src/calculation/DetailedResultsBuilder.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🌿 Branch is not covered

Warning! Not covered branch
if (popCode) {
popValue = populationResults.find(pr => pr.populationType === popCode)?.result ?? true;

Check warning on line 354 in src/calculation/DetailedResultsBuilder.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🌿 Branch is not covered

Warning! Not covered branch

Check warning on line 354 in src/calculation/DetailedResultsBuilder.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🌿 Branch is not covered

Warning! Not covered branch
}
}
console.log(popValue);
strataResult.appliesResult = isStatementValueTruthy(popValue && strataResult.result);
}
}
});
}
return stratifierResults;
}

function isStatementValueTruthy(value: any): boolean {
if (Array.isArray(value) && value.length > 0) {
return true;
Expand Down
88 changes: 53 additions & 35 deletions test/unit/DetailedResultsBuilder.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import * as DetailedResultsBuilder from '../../src/calculation/DetailedResultsBu
import { getJSONFixture } from './helpers/testHelpers';
import { MeasureScoreType, PopulationType } from '../../src/types/Enums';
import { StatementResults } from '../../src/types/CQLTypes';
import { PopulationResult, EpisodeResults } from '../../src/types/Calculator';
import { PopulationResult, EpisodeResults, StratifierResult } from '../../src/types/Calculator';
import { ELMExpressionRef, ELMQuery, ELMTuple, ELMFunctionRef } from '../../src/types/ELMTypes';

type MeasureWithGroup = fhir4.Measure & {
Expand Down Expand Up @@ -1111,8 +1111,8 @@ describe('DetailedResultsBuilder', () => {
});
});

describe('Patient Population Values', () => {
test('should take population result into consideration when appliesTo extension exists', () => {
describe('handleStratificationValues', () => {
test('it should take population result into consider when appliesTo extension exists', () => {
const populationGroup: fhir4.MeasureGroup = {
stratifier: [
{
Expand All @@ -1139,23 +1139,32 @@ describe('DetailedResultsBuilder', () => {
]
};

const patientResults: StatementResults = {
'Initial Population': false,
Denominator: false,
'Denominator Exclusion': false,
Numerator: false,
'Numerator Exclusion': false,
strat1: true
};
const populationResults: PopulationResult[] = [
{
populationType: PopulationType.IPP,
criteriaExpression: 'Initial Population',
result: false,
populationId: 'exampleId'
}
];

const stratifierResults: StratifierResult[] = [
{
strataCode: 'example-strata-id',
result: true,
strataId: 'example-strata-id'
}
];

const { stratifierResults } = DetailedResultsBuilder.createPatientPopulationValues(
const newStratifierResults = DetailedResultsBuilder.handleStratificationValues(
populationGroup,
patientResults
populationResults,
stratifierResults
);

expect(stratifierResults).toBeDefined();
expect(stratifierResults).toHaveLength(1);
expect(stratifierResults).toEqual(
expect(newStratifierResults).toBeDefined();
expect(newStratifierResults).toHaveLength(1);
expect(newStratifierResults).toEqual(
expect.arrayContaining([
expect.objectContaining({
strataId: 'example-strata-id',
Expand All @@ -1166,39 +1175,48 @@ describe('DetailedResultsBuilder', () => {
);
});

test('does not take any population result into consideration when appliesTo extension does not exist', () => {
const populationGroupNoExtension: fhir4.MeasureGroup = {
test('it should not take population result into consideration when appliesTo extension does not exist', () => {
const populationGroup: fhir4.MeasureGroup = {
stratifier: [
{
id: 'example-strata-id-2',
id: 'example-strata-id',
criteria: {
expression: 'strat2',
expression: 'strat1',
language: 'text/cql'
}
}
]
};

const patientResults: StatementResults = {
'Initial Population': false,
Denominator: false,
'Denominator Exclusion': false,
Numerator: false,
'Numerator Exclusion': false,
strat2: true
};
const populationResults: PopulationResult[] = [
{
populationType: PopulationType.IPP,
criteriaExpression: 'Initial Population',
result: false,
populationId: 'exampleId'
}
];

const { stratifierResults } = DetailedResultsBuilder.createPatientPopulationValues(
populationGroupNoExtension,
patientResults
const stratifierResults: StratifierResult[] = [
{
strataCode: 'example-strata-id',
result: true,
strataId: 'example-strata-id'
}
];

const newStratifierResults = DetailedResultsBuilder.handleStratificationValues(
populationGroup,
populationResults,
stratifierResults
);

expect(stratifierResults).toBeDefined();
expect(stratifierResults).toHaveLength(1);
expect(stratifierResults).toEqual(
expect(newStratifierResults).toBeDefined();
expect(newStratifierResults).toHaveLength(1);
expect(newStratifierResults).toEqual(
expect.arrayContaining([
expect.objectContaining({
strataId: 'example-strata-id-2',
strataId: 'example-strata-id',
result: true,
appliesResult: true
})
Expand Down

0 comments on commit 0f94176

Please sign in to comment.