Skip to content

Commit

Permalink
updating all measure queries to use new measure table
Browse files Browse the repository at this point in the history
  • Loading branch information
angelaco11 committed Sep 17, 2024
1 parent 65b7bb2 commit 3df7020
Show file tree
Hide file tree
Showing 12 changed files with 61 additions and 51 deletions.
16 changes: 9 additions & 7 deletions services/app-api/handlers/coreSets/create.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import handler from "../../libs/handler-lib";
import dynamoDb from "../../libs/dynamodb-lib";
import { getCoreSet } from "./get";
import { createCoreSetKey } from "../dynamoUtils/createCompoundKey";
import {
createCoreSetKey,
createMeasureKey,
} from "../dynamoUtils/createCompoundKey";
import { MeasureMetaData, measures } from "../dynamoUtils/measureList";
import {
hasRolePermissions,
Expand Down Expand Up @@ -46,7 +49,7 @@ export const createCoreSet = handler(async (event, context) => {

await createDependentMeasures(
state,
parseInt(year),
year,
coreSet as Types.CoreSetAbbr,
type
);
Expand Down Expand Up @@ -91,11 +94,11 @@ export const createCoreSet = handler(async (event, context) => {

const createDependentMeasures = async (
state: string,
year: number,
year: string,
coreSet: Types.CoreSetAbbr,
type: string
) => {
const filteredMeasures = measures[year].filter(
const filteredMeasures = measures[parseInt(year)].filter(
(measure: MeasureMetaData) => measure.type === type
);

Expand All @@ -104,10 +107,9 @@ const createDependentMeasures = async (
for await (const measure of filteredMeasures) {
// The State Year and ID are all part of the path
const measureId = measure["measure"];
// Dynamo only accepts one row as a key, so we are using a combination for the dynamoKey
const dynamoKey = `${state}${year}${coreSet}${measureId}`;
const dynamoKey = createMeasureKey({ state, year, coreSet });
const params = {
TableName: process.env.measureTableName!,
TableName: process.env.measureTable!,
Item: {
compoundKey: dynamoKey,
state: state,
Expand Down
28 changes: 16 additions & 12 deletions services/app-api/handlers/coreSets/delete.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import handler from "../../libs/handler-lib";
import dynamoDb from "../../libs/dynamodb-lib";
import { createCoreSetKey } from "../dynamoUtils/createCompoundKey";
import {
createCoreSetKey,
createMeasureKey,
} from "../dynamoUtils/createCompoundKey";
import { convertToDynamoExpression } from "../dynamoUtils/convertToDynamoExpressionVars";
import { hasStatePermissions } from "../../libs/authorization";
import { Measure } from "../../types";
Expand Down Expand Up @@ -34,7 +37,7 @@ export const deleteCoreSet = handler(async (event, context) => {
};

await dynamoDb.delete(params);
await deleteDependentMeasures(state, parseInt(year), coreSet);
await deleteDependentMeasures(state, year, coreSet);

return {
status: StatusCodes.SUCCESS,
Expand All @@ -44,16 +47,16 @@ export const deleteCoreSet = handler(async (event, context) => {

const deleteDependentMeasures = async (
state: string,
year: number,
year: string,
coreSet: string
) => {
const measures = await getMeasures(state, year, coreSet);
const Items = measures;

for await (const { measure } of Items) {
const dynamoKey = `${state}${year}${coreSet}${measure}`;
const dynamoKey = createMeasureKey({ state, year, coreSet });
const params = {
TableName: process.env.measureTableName!,
TableName: process.env.measureTable!,
Key: {
compoundKey: dynamoKey,
coreSet: coreSet,
Expand All @@ -64,14 +67,15 @@ const deleteDependentMeasures = async (
}
};

const getMeasures = async (state: string, year: number, coreSet: string) => {
const getMeasures = async (state: string, year: string, coreSet: string) => {
const dynamoKey = createMeasureKey({ state, year, coreSet });
const params = {
TableName: process.env.measureTableName!,
...convertToDynamoExpression(
{ state: state, year: year, coreSet: coreSet },
"list"
),
TableName: process.env.measureTable!,
KeyConditionExpression: "compoundKey = :compoundKey",
ExpressionAttributeValues: {
":compoundKey": dynamoKey,
},
};
const queryValue = await dynamoDb.scanAll<Measure>(params);
const queryValue = await dynamoDb.queryAll<Measure>(params);
return queryValue;
};
4 changes: 2 additions & 2 deletions services/app-api/handlers/dynamoUtils/createCompoundKey.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { CoreSetParameters, MeasureParameters } from "../../types";

export const createMeasureKey = (measureParams: MeasureParameters) => {
const { state, year, coreSet, measure } = measureParams;
const { state, year, coreSet } = measureParams;

return `${state}${year}${coreSet}${measure}`;
return `${state}${year}${coreSet}`;
};

export const createCoreSetKey = (coreSetParams: CoreSetParameters) => {
Expand Down
4 changes: 2 additions & 2 deletions services/app-api/handlers/measures/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ export const createMeasure = handler(async (event, context) => {
} // if not state user, can safely assume admin type user due to baseline handler protections

const body = JSON.parse(event!.body!);
const dynamoKey = createMeasureKey({ state, year, coreSet, measure });
const dynamoKey = createMeasureKey({ state, year, coreSet });
const params = {
TableName: process.env.measureTableName!,
TableName: process.env.measureTable!,
Item: {
compoundKey: dynamoKey,
state: state,
Expand Down
6 changes: 3 additions & 3 deletions services/app-api/handlers/measures/delete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ export const deleteMeasure = handler(async (event, context) => {
};
}

const dynamoKey = createMeasureKey({ state, year, coreSet, measure });
const dynamoKey = createMeasureKey({ state, year, coreSet });
const params = {
TableName: process.env.measureTableName!,
TableName: process.env.measureTable!,
Key: {
compoundKey: dynamoKey,
coreSet: coreSet,
measure: measure,
},
};

Expand Down
12 changes: 6 additions & 6 deletions services/app-api/handlers/measures/get.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import handler from "../../libs/handler-lib";
import dynamoDb from "../../libs/dynamodb-lib";
import { convertToDynamoExpression } from "../dynamoUtils/convertToDynamoExpressionVars";
import { createMeasureKey } from "../dynamoUtils/createCompoundKey";
import { measures } from "../dynamoUtils/measureList";
import {
Expand Down Expand Up @@ -34,11 +33,12 @@ export const listMeasures = handler(async (event, context) => {
};
}
} // if not state user, can safely assume admin type user due to baseline handler protections
const dynamoKey = createMeasureKey({ state, year, coreSet });
const params = {
TableName: process.env.measureTable,
TableName: process.env.measureTable!,
KeyConditionExpression: "compoundKey = :compoundKey",
ExpressionAttributeValues: {
":compoundKey": `${state}${parseInt(year)}${coreSet}`,
":compoundKey": dynamoKey,
},
};

Expand Down Expand Up @@ -81,12 +81,12 @@ export const getMeasure = handler(async (event, context) => {
}
} // if not state user, can safely assume admin type user due to baseline handler protections

const dynamoKey = createMeasureKey({ state, year, coreSet, measure });
const dynamoKey = createMeasureKey({ state, year, coreSet });
const params = {
TableName: process.env.measureTableName!,
TableName: process.env.measureTable!,
Key: {
compoundKey: dynamoKey,
coreSet: coreSet,
measure: measure,
},
};
const queryValue = await dynamoDb.get<Measure>(params);
Expand Down
6 changes: 3 additions & 3 deletions services/app-api/handlers/measures/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export const editMeasure = handler(async (event, context) => {
detailedDescription,
} = JSON.parse(event!.body!);

const dynamoKey = createMeasureKey({ state, year, coreSet, measure });
const dynamoKey = createMeasureKey({ state, year, coreSet });
const lastAlteredBy = getUserNameFromJwt(event);

const descriptionParams =
Expand All @@ -46,10 +46,10 @@ export const editMeasure = handler(async (event, context) => {
}
: {};
const params = {
TableName: process.env.measureTableName!,
TableName: process.env.measureTable!,
Key: {
compoundKey: dynamoKey,
coreSet: coreSet,
measure: measure,
},
...convertToDynamoExpression(
{
Expand Down
2 changes: 1 addition & 1 deletion services/app-api/handlers/rate/get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
} from "../../libs/authorization";
import handler from "../../libs/handler-lib";
import { getCombinedRatesFromTable } from "../../storage/table";
import { MeasureParameters, UserRoles } from "../../types";
import { UserRoles } from "../../types";
import { Errors, StatusCodes } from "../../utils/constants/constants";
import { parseMeasureParameters } from "../../utils/parseParameters";

Expand Down
12 changes: 6 additions & 6 deletions services/app-api/handlers/rate/rateCalculations.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {
getMeasureFromTable,
putCombinedRatesToTable,
} from "../../storage/table";
import { MeasureParameters } from "../../types";
import { RateParameters } from "../../types";
import { calculateAndPutRate } from "./rateCalculations";

jest.mock("../../storage/table", () => ({
Expand All @@ -21,15 +21,15 @@ describe("Combined Rate Calculations", () => {
});

it("Should not calculate a combined rate for core sets that are already combined", async () => {
await calculateAndPutRate({ coreSet: "ACS" } as MeasureParameters);
await calculateAndPutRate({ coreSet: "CCS" } as MeasureParameters);
await calculateAndPutRate({ coreSet: "HHCS" } as MeasureParameters);
await calculateAndPutRate({ coreSet: "ACS" } as RateParameters);
await calculateAndPutRate({ coreSet: "CCS" } as RateParameters);
await calculateAndPutRate({ coreSet: "HHCS" } as RateParameters);

expect(getMeasureFromTable).not.toHaveBeenCalled();
});

it("Should calculate combined rates for separated child core sets", async () => {
await calculateAndPutRate({ coreSet: "CCSM" } as MeasureParameters);
await calculateAndPutRate({ coreSet: "CCSM" } as RateParameters);

expect(getMeasureFromTable).toHaveBeenCalledWith(
expect.objectContaining({ coreSet: "CCSM" })
Expand All @@ -44,7 +44,7 @@ describe("Combined Rate Calculations", () => {
});

it("Should calculate combined rates for separated asult core sets", async () => {
await calculateAndPutRate({ coreSet: "ACSC" } as MeasureParameters);
await calculateAndPutRate({ coreSet: "ACSC" } as RateParameters);

expect(getMeasureFromTable).toHaveBeenCalledWith(
expect.objectContaining({ coreSet: "ACSM" })
Expand Down
6 changes: 2 additions & 4 deletions services/app-api/handlers/rate/rateCalculations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
CoreSetAbbr,
isCoreSetAbbr,
Measure,
MeasureParameters,
RateParameters,
} from "../../types";
import { collectDataSources } from "./dataSourceAnalysis";
import { combineRates } from "./rateNDRCalculations";
Expand All @@ -19,9 +19,7 @@ import { calculateAdditionalValues } from "./rateValueCalculations";
* 3. Perform all calculations
* 4. Store the results to the Rates table
*/
export const calculateAndPutRate = async (
pathParameters: MeasureParameters
) => {
export const calculateAndPutRate = async (pathParameters: RateParameters) => {
const { coreSet, measure } = pathParameters;
if (!isCoreSetAbbr(coreSet)) {
return;
Expand Down
8 changes: 4 additions & 4 deletions services/app-api/storage/table.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import dynamoDb from "../libs/dynamodb-lib";
import * as Types from "../types";
import { MeasureParameters, CombinedRatesPayload } from "../types";
import { RateParameters, CombinedRatesPayload } from "../types";

export const getMeasureFromTable = async (parameters: MeasureParameters) => {
export const getMeasureFromTable = async (parameters: RateParameters) => {
const { state, year, coreSet, measure } = parameters;
return await dynamoDb.get<Types.Measure>({
TableName: process.env.measureTableName,
Expand All @@ -14,7 +14,7 @@ export const getMeasureFromTable = async (parameters: MeasureParameters) => {
};

export const putCombinedRatesToTable = async (
parameters: MeasureParameters,
parameters: RateParameters,
combinedRates: CombinedRatesPayload
) => {
const { year, state, coreSet, measure } = parameters;
Expand All @@ -39,7 +39,7 @@ export const putCombinedRatesToTable = async (
};

export const getCombinedRatesFromTable = async (
parameters: MeasureParameters
parameters: RateParameters
): Promise<CombinedRatesPayload> => {
const { year, state, coreSet, measure } = parameters;
const queryValue = await dynamoDb.get({
Expand Down
8 changes: 7 additions & 1 deletion services/app-api/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,13 +172,19 @@ export enum MeasurementSpecificationType {
TheJointCommission = "TheJointCommission",
}

export interface MeasureParameters {
export interface RateParameters {
state: string;
year: string;
coreSet: string;
measure: string;
}

export interface MeasureParameters {
state: string;
year: string;
coreSet: string;
}

export interface CoreSetParameters {
state: string;
year: string;
Expand Down

0 comments on commit 3df7020

Please sign in to comment.