Skip to content

Commit

Permalink
Merge pull request #397 from Enterprise-CMCS/master
Browse files Browse the repository at this point in the history
Release to val
  • Loading branch information
mdial89f authored Feb 20, 2024
2 parents b07d8fc + fccf9b3 commit e9ae481
Show file tree
Hide file tree
Showing 23 changed files with 222 additions and 284 deletions.
101 changes: 0 additions & 101 deletions src/services/api/handlers/forms.ts

This file was deleted.

53 changes: 13 additions & 40 deletions src/services/api/handlers/getAllForms.ts
Original file line number Diff line number Diff line change
@@ -1,54 +1,27 @@
import { response } from "../libs/handler";
import * as fs from "fs";
import * as path from "path";
import { webformVersions } from "../webforms";
import { FormSchema } from "shared-types";

interface ObjectWithArrays {
[key: string]: string[];
}
export const mapWebformsKeys = (
webforms: Record<string, Record<string, FormSchema>>
): Record<string, string[]> => {
const result: Record<string, string[]> = {};

export function removeTsAndJsExtentions(
obj: ObjectWithArrays
): ObjectWithArrays {
const result: ObjectWithArrays = {};

for (const key in obj) {
// eslint-disable-next-line no-prototype-builtins
if (obj.hasOwnProperty(key)) {
const filteredFiles = obj[key].filter((file) => !file.endsWith(".ts"));
result[key] = filteredFiles.map((f) =>
f.replace(".js", "").replace("v", "")
);
}
}

return result;
}

function getAllFormsAndVersions(directoryPath: string) {
const result: ObjectWithArrays = {};

const subDirectories = fs.readdirSync(directoryPath);

subDirectories.forEach((subDir) => {
const subDirPath = path.join(directoryPath, subDir);

if (fs.statSync(subDirPath).isDirectory()) {
const files = fs.readdirSync(subDirPath);
result[subDir] = files;
}
Object.entries(webforms).forEach(([key, value]) => {
result[key] = Object.keys(value);
});

return removeTsAndJsExtentions(result);
}
return result;
};

export const getAllForms = async () => {
try {
const filePath = getAllFormsAndVersions("/opt/");
const formsWithVersions = mapWebformsKeys(webformVersions);

if (filePath) {
if (formsWithVersions) {
return response({
statusCode: 200,
body: filePath,
body: formsWithVersions,
});
}
} catch (error: any) {
Expand Down
76 changes: 76 additions & 0 deletions src/services/api/handlers/getForm.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import { response } from "../libs/handler";
import { APIGatewayEvent } from "aws-lambda";
import { convertRegexToString } from "shared-utils";
import { webformVersions } from "../webforms";

type GetFormBody = {
formId: string;
formVersion?: string;
};

export const getForm = async (event: APIGatewayEvent) => {
if (!event.body) {
return response({
statusCode: 400,
body: { message: "Event body required" },
});
}
try {
const body = JSON.parse(event.body) as GetFormBody;
if (!body.formId) {
return response({
statusCode: 400,
body: { error: "File ID was not provided" },
});
}

const id = body.formId.toUpperCase();

if (!webformVersions[id]) {
return response({
statusCode: 400,
body: { error: "Form ID not found" },
});
}

let version = "v";
if (body.formVersion) {
version += body.formVersion;
} else {
version += getMaxVersion(id);
}

if (id && version) {
const formObj = await webformVersions[id][version];
const cleanedForm = convertRegexToString(formObj);
return response({
statusCode: 200,
body: cleanedForm,
});
}
} catch (error: any) {
console.error("Error:", error);
return response({
statusCode: 502,
body: {
error: error.message ? error.message : "Internal server error",
},
});
}
return response({
statusCode: 500,
body: {
error: "Internal server error",
},
});
};

function getMaxVersion(id: string): string {
const webform = webformVersions[id];

const keys = Object.keys(webform);
keys.sort();
return keys[keys.length - 1];
}

export const handler = getForm;
93 changes: 0 additions & 93 deletions src/services/api/handlers/tests/forms.test.ts

This file was deleted.

37 changes: 37 additions & 0 deletions src/services/api/handlers/tests/getAllForms.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { describe, it, expect, vi } from "vitest";
import { getAllForms } from "../getAllForms";
import { response } from "../../libs/handler";

// Mock the dependencies
vi.mock("../../libs/handler", () => ({
response: vi.fn((arg) => arg),
}));

vi.mock("../../webforms", () => ({
webformVersions: {
ABP1: {
v202401: { name: "Test Form", data: "schema1" },
v202402: { name: "Test Form", data: "schema2" },
},
ABP3: {
v202401: { name: "Test Form", data: "schema3" },
},
},
}));

describe("getAllForms", () => {
it("should return a response with status code 200 and the mapped webforms", async () => {
const expectedResponse = {
statusCode: 200,
body: {
ABP1: ["v202401", "v202402"],
ABP3: ["v202401"],
},
};

const result = await getAllForms();
expect(result?.statusCode).toEqual(200);
expect(result?.body).toEqual(expectedResponse.body);
expect(response).toHaveBeenCalledWith(expectedResponse);
});
});
Loading

0 comments on commit e9ae481

Please sign in to comment.