Skip to content

Commit

Permalink
refactor files and add proj type validation
Browse files Browse the repository at this point in the history
  • Loading branch information
jessicamcinchak committed Jul 8, 2024
1 parent 9413a56 commit 5d35073
Show file tree
Hide file tree
Showing 9 changed files with 429 additions and 280 deletions.
2 changes: 1 addition & 1 deletion api.planx.uk/modules/flows/validate/controller.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Node } from "@opensystemslab/planx-core/types";
import { ValidatedRequestHandler } from "../../../shared/middleware/validate";
import { z } from "zod";
import { validateAndDiffFlow } from "./service";
import { validateAndDiffFlow } from "./service/index";
import { ServerError } from "../../../errors";

interface ValidateAndDiffResponse {
Expand Down
278 changes: 0 additions & 278 deletions api.planx.uk/modules/flows/validate/service.ts

This file was deleted.

76 changes: 76 additions & 0 deletions api.planx.uk/modules/flows/validate/service/fileTypes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import { getValidSchemaValues } from "@opensystemslab/planx-core";
import {
ComponentType,
FlowGraph,
Node,
} from "@opensystemslab/planx-core/types";
import countBy from "lodash/countBy";

import { isComponentType } from "../helpers";
import { FlowValidationResponse } from "./index";

const validateFileTypes = (flowGraph: FlowGraph): FlowValidationResponse => {
// Get all passport variables set by FileUpload and/or FileUploadAndLabel
const allFileFns = [
...getFileUploadNodeFns(flowGraph),
...getFileUploadAndLabelNodeFns(flowGraph),
];
if (allFileFns.length < 1) {
return {
title: "File types",
status: "Not applicable",
message: "Your flow is not using FileUpload or UploadAndLabel",
};
}

// Get all file types supported by current release of ODP Schema & compare
const validFileTypes = getValidSchemaValues("FileType");
const invalidFileFns: string[] = [];
allFileFns.forEach((fn) => {
if (!validFileTypes?.includes(fn)) {
invalidFileFns.push(fn);
}
});
if (invalidFileFns.length > 0) {
// Get unique fns with count of occurances
const countInvalidFileFns = countBy(invalidFileFns);
const summarisedInvalidFileFns: string[] = [];
Object.entries(countInvalidFileFns).map(([k, v]: [string, number]) => {
summarisedInvalidFileFns.push(`${k} (${v})`);
});
return {
title: "File types",
status: "Warn",
message: `Your FileUpload or UploadAndLabel are setting data fields that are not supported by the current release of the ODP Schema: ${summarisedInvalidFileFns.join(", ")}`,
};
}

return {
title: "File types",
status: "Pass",
message:
"Files collected via FileUpload or UploadAndLabel are all supported by the ODP Schema",
};
};

const getFileUploadNodeFns = (flowGraph: FlowGraph): string[] => {
const fileUploadNodes = Object.entries(flowGraph).filter((entry) =>
isComponentType(entry, ComponentType.FileUpload),
);
return fileUploadNodes.map(([_nodeId, node]) => node.data?.fn as string);
};

const getFileUploadAndLabelNodeFns = (flowGraph: FlowGraph): string[] => {
// Exclude Upload & Label nodes used in "info-only" mode with a hidden dropzone
const uploadAndLabelNodes = Object.entries(flowGraph).filter(
(entry) =>
isComponentType(entry, ComponentType.FileUploadAndLabel) &&
entry[1].data?.hideDropZone !== true,
);
const uploadAndLabelFileTypes = uploadAndLabelNodes
.map(([_nodeId, node]: [string, Node]) => node.data?.fileTypes)
.flat();
return uploadAndLabelFileTypes?.map((file: any) => file?.fn as string);

Check warning on line 73 in api.planx.uk/modules/flows/validate/service/fileTypes.ts

View workflow job for this annotation

GitHub Actions / Run API Tests

Unexpected any. Specify a different type
};

export { validateFileTypes };
Loading

0 comments on commit 5d35073

Please sign in to comment.