Skip to content

Commit

Permalink
Enable field 2 in Basic State Information (#139460)
Browse files Browse the repository at this point in the history
Co-authored-by: Garrett Rabian <[email protected]>
  • Loading branch information
gmrabian and gmrabian authored Sep 25, 2023
1 parent 835ddd6 commit ecfd6c5
Show file tree
Hide file tree
Showing 9 changed files with 156 additions and 105 deletions.
21 changes: 21 additions & 0 deletions services/app-api/handlers/section/update.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { JSONPath } from "jsonpath-plus";
import handler from "../../libs/handler-lib";
import dynamoDb from "../../libs/dynamodb-lib";
import { getUserCredentialsFromJwt } from "../../libs/authorization";
Expand Down Expand Up @@ -79,6 +80,16 @@ export const updateSections = handler(async (event, _context) => {
const moveToInProgress =
queryValue.Items && stateStatus.status === "not_started";

/*
* attempt to find programType from the reportData
* check current and previous year id
* otherwise use existing programType from state status
*/
const programType =
getProgramType(year, reportData) ??
getProgramType(parseInt(year) - 1, reportData) ??
stateStatus?.programType;

const statusParams = {
TableName: process.env.stateStatusTableName!,
Key: {
Expand All @@ -89,10 +100,20 @@ export const updateSections = handler(async (event, _context) => {
{
status: moveToInProgress ? "in_progress" : stateStatus.status,
lastChanged: lastChanged,
programType: programType,
},
"post"
),
};

await dynamoDb.update(statusParams);
});

const PROGRAM_TYPE_QUESTION_ID = "-00-a-01-02";

const getProgramType = (year: string | number, reportData: any) => {
const idExpression = `${year}${PROGRAM_TYPE_QUESTION_ID}`;
const jpexpr = `$..*[?(@ && @.id=='${idExpression}')]`;
const fragment = JSONPath({ path: jpexpr, json: reportData[0].contents });
return fragment?.[0]?.answer?.entry;
};
1 change: 1 addition & 0 deletions services/app-api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"@types/jest": "^27.4.0",
"aws-lambda": "^1.0.7",
"jest": "^27.4.7",
"jsonpath-plus": "^5.1.0",
"serverless-associate-waf": "^1.2.1",
"serverless-plugin-typescript": "^2.1.0",
"ts-jest": "^27.1.3",
Expand Down
5 changes: 5 additions & 0 deletions services/app-api/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2026,6 +2026,11 @@ jsonfile@^4.0.0:
optionalDependencies:
graceful-fs "^4.1.6"

jsonpath-plus@^5.1.0:
version "5.1.0"
resolved "https://registry.yarnpkg.com/jsonpath-plus/-/jsonpath-plus-5.1.0.tgz#2fc4b2e461950626c98525425a3a3518b85af6c3"
integrity sha512-890w2Pjtj0iswAxalRlt2kHthi6HKrXEfZcn+ZNZptv7F3rUGIeDuZo+C+h4vXBHLEsVjJrHeCm35nYeZLzSBQ==

jsonschema@^1.4.1:
version "1.4.1"
resolved "https://registry.yarnpkg.com/jsonschema/-/jsonschema-1.4.1.tgz#cc4c3f0077fb4542982973d8a083b6b34f482dab"
Expand Down
1 change: 0 additions & 1 deletion services/database/data/seed/seed-section-base-2023.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@
"value": "separate_chip"
}
],
"readonly": true,
"prepopulated": true
}
},
Expand Down
49 changes: 1 addition & 48 deletions services/ui-src/src/components/layout/Part.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,6 @@ import Question from "../fields/Question";
import { selectQuestionsForPart } from "../../store/selectors";
import { shouldDisplay } from "../../util/shouldDisplay";
import Text from "./Text";
import { AppRoles } from "../../types";

const showPart = (contextData, programType, state) => {
if (
contextData &&
programType &&
contextData.show_if_state_program_type_in &&
!contextData.show_if_state_program_type_in.includes(programType)
) {
return false;
}
return shouldDisplay(state, contextData, programType);
};

const Part = ({
context_data: contextData,
Expand Down Expand Up @@ -100,49 +87,15 @@ const mapStateToProps = (state, { partId }) => {
const part = selectFragment(state, partId);
const questions = selectQuestionsForPart(state, partId);
const contextData = part.context_data;
const location = window.location.pathname.split("/");
const userState = location[3]; // Current state, ie: "AL" or "CT"
const programData = state.allStatesData.find(
(element) => element.code === userState
);

return {
context_data: part.context_data,
questions,
show: showPartBasedOnUserType(contextData, programData, state),
show: shouldDisplay(state, contextData),
text: part ? part.text : null,
title: part ? part.title : null,
isFetching: state.global.isFetching,
};
};

export default connect(mapStateToProps)(Part);

/**
* This function considers what arguments to invoke showPart() with based on user type.
* Business and CO users do not have program types saved to their user objects in local state
* @function showPartBasedOnUserType
* @param {object} contextData - The context data for a Part from JSON
* @param {object} programData - An object with the state's program type
* @param {string} state - application state from redux
* @returns {boolean} - determines if an element should show by invoking showPart()
*/
const showPartBasedOnUserType = (contextData, programData, state) => {
const role = state.stateUser.currentUser.role;

if (
programData &&
(role === AppRoles.CMS_ADMIN ||
role === AppRoles.INTERNAL_USER ||
role === AppRoles.HELP_DESK ||
role === AppRoles.CMS_APPROVER ||
role === AppRoles.CMS_USER ||
role === AppRoles.STATE_USER)
) {
// program type from programData object, for bus_user and co_user
return showPart(contextData, programData.program_type, state);
} else {
// program type from stateUser object, for state_user's
return showPart(contextData, state.stateUser.programType, state);
}
};
7 changes: 0 additions & 7 deletions services/ui-src/src/store/formData.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,6 @@ export default (state = initialState, action) => {
updatedData[0].contents.section.subsections[0].parts[0].questions[0] =
lastYearData[0].contents.section.subsections[0].parts[0].questions[0]; // Name
}
if (
!updatedData[0].contents.section.subsections[0].parts[0].questions[1]
.answer.entry
) {
updatedData[0].contents.section.subsections[0].parts[0].questions[1] =
lastYearData[0].contents.section.subsections[0].parts[0].questions[1]; // Type
}
// Cohort Questions - These should be revolving around a 2 year cycle, but today just pull forward
if (twoYearCycle) {
updatedData[3].contents.section.subsections[2].parts[5].questions[1].answer =
Expand Down
2 changes: 0 additions & 2 deletions services/ui-src/src/store/stateUser.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ export const getUserData = (userObject) => {

export const getProgramData = (programObject) => ({
type: PROGRAM_INFO,
programType: programObject.programType,
programName: programObject.programName,
formName: programObject.formName,
});
Expand Down Expand Up @@ -84,7 +83,6 @@ export default (state = initialState, action) => {
case PROGRAM_INFO:
return {
...state,
programType: action.programType,
programName: action.programName,
formName: action.formName,
};
Expand Down
41 changes: 27 additions & 14 deletions services/ui-src/src/util/shouldDisplay.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { selectFragmentById } from "../store/formData";
import { AppRoles } from "../types";
import jsonpath from "./jsonpath";
import {
Expand Down Expand Up @@ -166,28 +167,39 @@ const hideIfTableValue = (state, hideIfTableValueInfo) => {
return result;
};

const PROGRAM_TYPE_QUESTION_ID = "-00-a-01-02";

const getProgramTypeFromForm = (state) => {
// attempt to find programType from same year as form
const formYear = state.formData[0].year;
const currentYearProgramType = selectFragmentById(
state,
`${formYear}${PROGRAM_TYPE_QUESTION_ID}`
)?.answer?.entry;
if (currentYearProgramType) {
return currentYearProgramType;
}

// attempt to find programType from the previous year's form, otherwise retrieve from status
const previousYear = parseInt(formYear) - 1;
const previousYearProgramType = selectFragmentById(
state,
`${previousYear}${PROGRAM_TYPE_QUESTION_ID}`
)?.answer?.entry;
const reportStatusCode = state.formData[0].stateId + state.formData[0].year;
const programFromStatus = state.reportStatus[reportStatusCode].programType;
return previousYearProgramType || programFromStatus;
};

/**
* This function checks to see if a question should display based on an answer from a different question
* @function shouldDisplay
* @param {object} state - The application state from redux, the object required for jsonpath to query
* @param {object} context - the context_data from a question
* @param {string} programType - program type of the user's state
* @returns {boolean} - determines if an element should be filtered out, returning true means a question will display
*/
const shouldDisplay = (state, context, programType = null) => {
let program, reportStatusCode;
const shouldDisplay = (state, context) => {
if (state.stateUser.currentUser.role === AppRoles.CMS_ADMIN) return true;
if (!programType) {
// If program type is not provided as an argument (the user is a bus_user, co_user), use the value for program type present in state
if (state.stateUser.currentUser.role === AppRoles.STATE_USER) {
reportStatusCode = state.stateUser.abbr + state.global.formYear;
} else {
reportStatusCode = state.formData[0].stateId + state.formData[0].year;
}
program = state.reportStatus[reportStatusCode].programType;
} else {
program = programType;
}

if (
!context ||
Expand All @@ -201,6 +213,7 @@ const shouldDisplay = (state, context, programType = null) => {
* displaying relies on that answer being included in the show_if_state_program_type_in array
*/
if (context.show_if_state_program_type_in) {
const program = getProgramTypeFromForm(state);
return context.show_if_state_program_type_in.includes(program);
}

Expand Down
Loading

0 comments on commit ecfd6c5

Please sign in to comment.