Skip to content

Commit

Permalink
generate pccm template in backend
Browse files Browse the repository at this point in the history
  • Loading branch information
gmrabian committed Oct 17, 2023
1 parent 18f8002 commit ee4c3d4
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 46 deletions.
3 changes: 2 additions & 1 deletion services/app-api/handlers/reports/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,8 @@ export const createReport = handler(async (event, _context) => {
try {
({ formTemplate, formTemplateVersion } = await getOrCreateFormTemplate(
reportBucket,
reportType
reportType,
unvalidatedMetadata
));
} catch (err) {
logger.error(err, "Error getting or creating template");
Expand Down
49 changes: 47 additions & 2 deletions services/app-api/utils/formTemplates/formTemplates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
FormField,
FormLayoutElement,
FormTemplate,
MCPARReportMetadata,
ModalOverlayReportPageShape,
ReportJson,
ReportRoute,
Expand Down Expand Up @@ -54,9 +55,14 @@ export const formTemplateForReportType = (reportType: ReportType) => {

export async function getOrCreateFormTemplate(
reportBucket: string,
reportType: ReportType
reportType: ReportType,
metadata: MCPARReportMetadata
) {
const currentFormTemplate = formTemplateForReportType(reportType);
let currentFormTemplate = formTemplateForReportType(reportType);
// if program is PCCM generate shortened template
if (metadata?.programIsPCCM?.[0]?.value === "Yes") {
currentFormTemplate = generatePCCMTemplate(currentFormTemplate);
}
const stringifiedTemplate = JSON.stringify(currentFormTemplate);

const currentTemplateHash = createHash("md5")
Expand Down Expand Up @@ -244,3 +250,42 @@ export function getValidationFromFormTemplate(reportJson: ReportJson) {
export function getPossibleFieldsFromFormTemplate(reportJson: ReportJson) {
return Object.keys(getValidationFromFormTemplate(reportJson));
}

const routesToIncludeInPCCM = {
"A: Program Information": [
"Point of Contact",
"Reporting Period",
"Add Plans",
],
"B: State-Level Indicators": ["I: Program Characteristics"],
"C: Program-Level Indicators": ["I: Program Characteristics"],
"D: Plan-Level Indicators": ["I: Program Characteristics", "VIII: Sanctions"],
"Review & Submit": [],
} as { [key: string]: string[] };

const entitiesToIncludeInPCCM = ["plans", "sanctions"];

export const generatePCCMTemplate = (reportTemplate: any) => {
// remove top level sections not in include list
reportTemplate.routes = reportTemplate.routes.filter(
(route: ReportRoute) => !!routesToIncludeInPCCM[route.name]
);

// only include listed subsections
for (let route of reportTemplate.routes) {
if (route?.children) {
route.children = route.children.filter((childRoute: ReportRoute) =>
routesToIncludeInPCCM[route.name].includes(childRoute.name)
);
}
}

// Any entity not in the allow list must be removed.
for (let entityType of Object.keys(reportTemplate.entities)) {
if (!entitiesToIncludeInPCCM.includes(entityType)) {
delete reportTemplate.entities[entityType];
}
}

return reportTemplate;
};
4 changes: 0 additions & 4 deletions services/ui-src/src/components/reports/ReportProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {
releaseReport as releaseReportRequest,
submitReport as submitReportRequest,
flattenReportRoutesArray,
generatePCCMTemplate,
getLocalHourMinuteTime,
getReport,
getReportsByState,
Expand Down Expand Up @@ -69,9 +68,6 @@ export const ReportProvider = ({ children }: Props) => {

const hydrateAndSetReport = (report: ReportShape | undefined) => {
if (report) {
if (report?.programIsPCCM?.[0]?.value === "Yes") {
report.formTemplate = generatePCCMTemplate(report.formTemplate);
}
report.formTemplate.flatRoutes = flattenReportRoutesArray(
report.formTemplate.routes
);
Expand Down
39 changes: 0 additions & 39 deletions services/ui-src/src/utils/reports/reports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,45 +31,6 @@ export const flattenReportRoutesArray = (
return routesArray;
};

const routesToInclude = {
"A: Program Information": [
"Point of Contact",
"Reporting Period",
"Add Plans",
],
"B: State-Level Indicators": ["I: Program Characteristics"],
"C: Program-Level Indicators": ["I: Program Characteristics"],
"D: Plan-Level Indicators": ["I: Program Characteristics", "VIII: Sanctions"],
"Review & Submit": [],
} as { [key: string]: string[] };

const entitiesToInclude = ["plans", "sanctions"];

export const generatePCCMTemplate = (reportTemplate: any) => {
// remove top level sections not in include list
reportTemplate.routes = reportTemplate.routes.filter(
(route: ReportRoute) => !!routesToInclude[route.name]
);

// only include listed subsections
for (let route of reportTemplate.routes) {
if (route?.children) {
route.children = route.children.filter((childRoute: ReportRoute) =>
routesToInclude[route.name].includes(childRoute.name)
);
}
}

// Any entity not in the allow list must be removed.
for (let entityType of Object.keys(reportTemplate.entities)) {
if (!entitiesToInclude.includes(entityType)) {
delete reportTemplate.entities[entityType];
}
}

return reportTemplate;
};

// returns validation schema object for array of fields
export const compileValidationJsonFromFields = (
fieldArray: FormField[],
Expand Down

0 comments on commit ee4c3d4

Please sign in to comment.