Skip to content

Commit

Permalink
Merge pull request #888 from CMSgov/QPPA-9863-update-logic-for-update…
Browse files Browse the repository at this point in the history
…-program-names

QPPA-9863: Move updateProgramNames logic to util directory
  • Loading branch information
john-manack authored Jan 10, 2025
2 parents 76eef60 + 6a5fdb3 commit ec85990
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 90 deletions.
70 changes: 8 additions & 62 deletions index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { Constants } from './constants';
import * as mvpDataUtils from './util/mvp-data-utils';
import { ProgramNamesEnum } from './util/interfaces/program-names';

const mvpJson = [
export const mockMvpJson = [
{
mvpId: 'G0053',
clinicalTopic: 'Stroke Care and Prevention',
Expand Down Expand Up @@ -57,60 +57,6 @@ describe('index', () => {
});
});

describe('updateProgramNames', () => {
it('updates the program-names json with any newly-found programs.', () => {
const writeSpy = jest.spyOn(fse, 'writeFileSync').mockImplementation(jest.fn());
vol.fromNestedJSON({
'util/program-names': {
'program-names.json': JSON.stringify({
mips: 'mips',
cpcPlus: 'cpcPlus',
pcf: 'pcf',
app1: 'app1',
DEFAULT: 'mips',
G0053: 'G0053'
}),
},
'mvp/2024': {
'mvp.json': JSON.stringify(mvpJson),
}
});

index.updateProgramNames(2024);

expect(writeSpy).toBeCalledWith(
expect.any(String),
JSON.stringify({
mips: 'mips',
cpcPlus: 'cpcPlus',
pcf: 'pcf',
app1: 'app1',
DEFAULT: 'mips',
G0053: 'G0053',
G0054: 'G0054'
}, null, 2),
);
});

it('gracefully logs an error if json parsing fails.', () => {
const writeSpy = jest.spyOn(fs, 'writeFileSync').mockImplementation(jest.fn());
const logSpy = jest.spyOn(console, 'log').mockImplementationOnce(jest.fn());
vol.fromNestedJSON({
'util/program-names': {
'program-names.json': '{\'badformat\':: \'mips\',\'cpcPlus\': \'cpcPlus\'}',
},
'mvp/2024': {
'mvp.json': JSON.stringify(mvpJson),
}
});

index.updateProgramNames(2024);

expect(writeSpy).not.toBeCalled();
expect(logSpy).toBeCalled();
});
});

describe('getProgramNames', () => {
it('gets the program-names json.', () => {
vol.fromNestedJSON({
Expand Down Expand Up @@ -408,7 +354,7 @@ describe('index', () => {
describe('getMVPData', () => {
let createMvpFileSpy: jest.SpyInstance;
beforeEach(() => {
createMvpFileSpy = jest.spyOn(mvpDataUtils, 'createMVPDataFile').mockImplementation(() => mvpJson);
createMvpFileSpy = jest.spyOn(mvpDataUtils, 'createMVPDataFile').mockImplementation(() => mockMvpJson);
});

it('finds and returns the mvp-enriched json file for the specified performance year.', () => {
Expand Down Expand Up @@ -619,14 +565,14 @@ describe('index', () => {
beforeEach(() => {
vol.fromNestedJSON({
'mvp/2024': {
'mvp.json': JSON.stringify(mvpJson),
'mvp.json': JSON.stringify(mockMvpJson),
},
});
});

it('successfully populates the mvp', () => {
const testMvp = { ...mvpJson[0], qualityMeasureIds: ['001', '321'], qualityMeasures: [] };
mvpDataUtils.populateMeasuresforMVPs(testMvp, mvpJson, testMeasuresData, 'qualityMeasureIds', 'qualityMeasures');
const testMvp = { ...mockMvpJson[0], qualityMeasureIds: ['001', '321'], qualityMeasures: [] };
mvpDataUtils.populateMeasuresforMVPs(testMvp, mockMvpJson, testMeasuresData, 'qualityMeasureIds', 'qualityMeasures');

expect(testMvp).toStrictEqual({
mvpId: 'G0053',
Expand Down Expand Up @@ -681,17 +627,17 @@ describe('index', () => {
it('returns the mvp data with all the ids concatenated for a specified performance year.', () => {
vol.fromNestedJSON({
'mvp/2024': {
'mvp.json': JSON.stringify(mvpJson),
'mvp.json': JSON.stringify(mockMvpJson),
},
});

expect(index.getMVPDataSlim(2024)).toStrictEqual([
{
...mvpJson[0],
...mockMvpJson[0],
measureIds: ['001', '002', '003', '004', '005', '006', '007', '008', '009'],
},
{
...mvpJson[1],
...mockMvpJson[1],
measureIds: ['001', '002', '003', '004', '005'],
},
]);
Expand Down
28 changes: 0 additions & 28 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,34 +27,6 @@ export function getValidPerformanceYears(): number[] {
return Constants.validPerformanceYears;
}

/**
*
* @return {void}
* Adds any new program name fields from the mvp.json file for the given performance year
*/
export function updateProgramNames(performanceYear: number): void {
let programNames: any;
const programNamesFilePath = path.join(__dirname, 'util/program-names', 'program-names.json');
const mvpFilePath = path.join(__dirname, 'mvp', performanceYear.toString(), 'mvp.json');

let mvpData: any[] = [];

try {
programNames = JSON.parse(fse.readFileSync(programNamesFilePath, 'utf8'));
mvpData = JSON.parse(fse.readFileSync(mvpFilePath, 'utf8'));

mvpData.forEach(mvp => {
if (!programNames[mvp.mvpId]) {
programNames[mvp.mvpId] = mvp.mvpId;
}
});

fse.writeFileSync(programNamesFilePath, JSON.stringify(programNames, null, 2));
} catch (e) {
console.log('Error parsing the program-names.json or mvp.json file: ' + ' --> ' + e);
}
}

/**
*
* @return {ProgramNames} - program names -
Expand Down
67 changes: 67 additions & 0 deletions util/update-program-names.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import fs from 'fs';
import fse from 'fs-extra';
import { vol } from 'memfs';
import * as updateProgramNames from './update-program-names';
import { mockMvpJson } from '../index.spec';

describe("updateProgramNames", () => {
it("updates the program-names json with any newly-found programs.", () => {
const writeSpy = jest
.spyOn(fse, "writeFileSync")
.mockImplementation(jest.fn());
vol.fromNestedJSON({
"util/program-names": {
"program-names.json": JSON.stringify({
mips: "mips",
cpcPlus: "cpcPlus",
pcf: "pcf",
app1: "app1",
DEFAULT: "mips",
G0053: "G0053",
}),
},
"mvp/2024": {
"mvp.json": JSON.stringify(mockMvpJson),
},
});

updateProgramNames.updateProgramNames(2024);

expect(writeSpy).toBeCalledWith(
expect.any(String),
JSON.stringify(
{
mips: "mips",
cpcPlus: "cpcPlus",
pcf: "pcf",
app1: "app1",
DEFAULT: "mips",
G0053: "G0053",
G0054: "G0054",
},
null,
2
)
);
});

it("gracefully logs an error if json parsing fails.", () => {
const writeSpy = jest
.spyOn(fs, "writeFileSync")
.mockImplementation(jest.fn());
const logSpy = jest.spyOn(console, "log").mockImplementationOnce(jest.fn());
vol.fromNestedJSON({
"util/program-names": {
"program-names.json": "{'badformat':: 'mips','cpcPlus': 'cpcPlus'}",
},
"mvp/2024": {
"mvp.json": JSON.stringify(mockMvpJson),
},
});

updateProgramNames.updateProgramNames(2024);

expect(writeSpy).not.toBeCalled();
expect(logSpy).toBeCalled();
});
});
33 changes: 33 additions & 0 deletions util/update-program-names.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import * as fse from 'fs-extra';
import * as path from 'path';

/**
*
* @return {void}
* Adds any new program name fields from the mvp.json file for the given performance year
*/
export function updateProgramNames(performanceYear: number): void {
let programNames: any;
const programNamesFilePath = path.join(__dirname, '../util/program-names', 'program-names.json');
const mvpFilePath = path.join(__dirname, '../mvp', performanceYear.toString(), 'mvp.json');

console.log('programNamesFilePath', programNamesFilePath);
console.log('mvpFilePath', mvpFilePath);

let mvpData: any[] = [];

try {
programNames = JSON.parse(fse.readFileSync(programNamesFilePath, 'utf8'));
mvpData = JSON.parse(fse.readFileSync(mvpFilePath, 'utf8'));

mvpData.forEach(mvp => {
if (!programNames[mvp.mvpId]) {
programNames[mvp.mvpId] = mvp.mvpId;
}
});

fse.writeFileSync(programNamesFilePath, JSON.stringify(programNames, null, 2));
} catch (e) {
console.log('Error parsing the program-names.json or mvp.json file: ' + ' --> ' + e);
}
}

0 comments on commit ec85990

Please sign in to comment.