From 45a49fc6f0bde36020ee96d65e2f1260f9baf70e Mon Sep 17 00:00:00 2001 From: Chetan Munegowda Date: Thu, 26 Dec 2024 16:00:21 -0500 Subject: [PATCH] QPPA-9758: create script to add/remove program --- CONTRIBUTING.md | 12 ++++ package.json | 1 + scripts/measures/manage-allowed-programs.ts | 79 +++++++++++++++++++++ updates/measures/2025/changes.meta.json | 4 +- 4 files changed, 94 insertions(+), 2 deletions(-) create mode 100644 scripts/measures/manage-allowed-programs.ts diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 6abf7c26..e4902f33 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -65,6 +65,18 @@ The strata are modified by updating the qcdr and quality strata CSVs in the year The specification links are added by placing the CSV or JSON files into the year's util directory, then running `npm run init:measures $YEAR`. +### Managing Allowed Programs for Measures + +The `manage:allowed-programs` script enables adding or removing a program from the `allowedPrograms` field of all measures in a specific category for a given performance year. This is especially useful for bulk updates, such as adding a new program or making uniform changes across multiple measures. + +```bash +npm run manage:allowed-programs -- +```` +Example: To remove the program `pcf` from all `ia` category measures for the year `2024`: +```bash +npm run manage:allowed-programs -- 2024 ia pcf remove +```` + ### Importing Measures from a CSV File To handle UTF-8 encoding, make sure that you save any new csv from excel as `CSV UTF-8 (Comma delimited) (.csv)`. This will keep Unknown Characters out of the data set. diff --git a/package.json b/package.json index 3a3dcaa7..2f81b91d 100644 --- a/package.json +++ b/package.json @@ -24,6 +24,7 @@ "update:mvp": "scripts/mvp/update-mvp", "parse:mvp": "scripts/mvp/parse-mvp-data", "export:measures": "bash scripts/measures/export-measures", + "manage:allowed-programs": "node dist/scripts/measures/manage-allowed-programs.js", "pretest": "npm run lint", "lint": "eslint scripts index.spec.ts", "lint:fix": "eslint scripts index.spec.ts --fix", diff --git a/scripts/measures/manage-allowed-programs.ts b/scripts/measures/manage-allowed-programs.ts new file mode 100644 index 00000000..1eb84cd0 --- /dev/null +++ b/scripts/measures/manage-allowed-programs.ts @@ -0,0 +1,79 @@ +import fs from 'fs-extra'; +import path from 'path'; +import appRoot from 'app-root-path'; + +// Function to update allowedPrograms for a specific category +export function updateAllowedPrograms( + performanceYear: string, + category: string, + program: string, + action: 'add' | 'remove', +) { + const measuresPath = `measures/${performanceYear}/measures-data.json`; + + try { + // Load the measures data + const measuresJson: any[] = JSON.parse( + fs.readFileSync(path.join(appRoot.toString(), measuresPath), 'utf8') + ); + + let updatedMeasures = 0; + + // Update measures in the specified category + measuresJson.forEach((measure) => { + if (measure.category === category) { + const allowedPrograms: string[] = measure.allowedPrograms || []; + + if (action === 'add') { + // Add program if it doesn't already exist + if (!allowedPrograms.includes(program)) { + allowedPrograms.push(program); + measure.allowedPrograms = allowedPrograms; + updatedMeasures++; + console.log(`Added program "${program}" to measure "${measure.measureId}".`); + } + } else if (action === 'remove') { + // Remove program if it exists + const index = allowedPrograms.indexOf(program); + if (index !== -1) { + allowedPrograms.splice(index, 1); + measure.allowedPrograms = allowedPrograms; + updatedMeasures++; + console.log(`Removed program "${program}" from measure "${measure.measureId}".`); + } + } + } + }); + + // Save the updated measures data back to the file + if (updatedMeasures > 0) { + fs.writeFileSync( + path.join(appRoot.toString(), measuresPath), + JSON.stringify(measuresJson, null, 2) + ); + console.log(`${updatedMeasures} measures updated successfully.`); + } else { + console.log(`No measures required updates.`); + } + } catch (err) { + console.error(`Failed to update measures: ${(err as Error).message}`); + } +} + +if (require.main === module) { + const args = process.argv.slice(2); + + if (args.length !== 4) { + console.error('Usage: node updateAllowedPrograms.js '); + process.exit(1); + } + + const [performanceYear, category, program, action] = args; + + if (!['add', 'remove'].includes(action)) { + console.error('Invalid action. Use "add" or "remove".'); + process.exit(1); + } + + updateAllowedPrograms(performanceYear, category, program, action as 'add' | 'remove'); +} diff --git a/updates/measures/2025/changes.meta.json b/updates/measures/2025/changes.meta.json index 0e4fd5a9..8f6b9fbe 100644 --- a/updates/measures/2025/changes.meta.json +++ b/updates/measures/2025/changes.meta.json @@ -9,6 +9,6 @@ "PI_PY25_CR_20241112.csv", "Quality_QCDR_PY25_CR_20241205.csv", "Cost_Measures_PY225_20241223.csv", - "Cost_Measures_PY225_CR_20241223.csv" + "Cost_Measures_PY225_CR_20241223.csv", "Quality_PY2025_DWH_Repo Compare Updates_CR_12182024.csv" -] \ No newline at end of file +]