-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #397 from Enterprise-CMCS/master
Release to val
- Loading branch information
Showing
23 changed files
with
222 additions
and
284 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |
Oops, something went wrong.