-
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.
feat(lamdba): add admin function for NOSO 2 (#1060)
* rewriting noso admin to be a function for an empty package * we adding some tests up in this b * making asharon fixes * changed back the string check for event.body * putting back the .and zod thing * needed to adjust logic so that if the package is in SeaTool we still do send a message to kafka so it shows on our dashboard * updated tests to reflect changes for checking for seaTool packages * :( i have to remove this extra `s` * setting the item origin to SEATool on line 78 was redundant * removed .and on zod, and created a new zod schema for sinkMain * i spelled like all the properties wrong * in changing the sinkmain schema i also have to change the changelog schema * change the admin change title * forgt the status on the extended schema * added mockEvent prop] * fixed the ui part * fixed test and other errors * removed unused code, adde3d example json comment * embarassing left andie console logs
- Loading branch information
1 parent
896c544
commit a1f4e7f
Showing
10 changed files
with
291 additions
and
5 deletions.
There are no files selected for viewing
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
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,114 @@ | ||
import { describe, it, expect, vi, beforeEach } from "vitest"; | ||
import { handler } from "./submitNOSO"; | ||
import { APIGatewayEvent } from "node_modules/shared-types"; | ||
|
||
import { NOT_EXISTING_ITEM_ID, TEST_ITEM_ID } from "mocks"; | ||
|
||
vi.mock("libs/handler-lib", () => ({ | ||
response: vi.fn((data) => data), | ||
})); | ||
|
||
describe("handler", () => { | ||
beforeEach(() => { | ||
vi.clearAllMocks(); | ||
process.env.topicName = "test-topic"; | ||
}); | ||
|
||
it("should return 400 if event body is missing", async () => { | ||
const event = {} as APIGatewayEvent; | ||
const result = await handler(event); | ||
const expectedResult = { statusCode: 400, body: { message: "Event body required" } }; | ||
|
||
expect(result).toStrictEqual(expectedResult); | ||
}); | ||
|
||
it("should return 400 if package ID is not found", async () => { | ||
const noActionevent = { | ||
body: JSON.stringify({ packageId: "123", changeReason: "Nunya", authority: "test" }), | ||
} as APIGatewayEvent; | ||
|
||
const resultPackage = await handler(noActionevent); | ||
|
||
expect(resultPackage?.statusCode).toBe(400); | ||
}); | ||
it("should return 400 if admingChangeType is not found", async () => { | ||
const noApackageEvent = { | ||
body: JSON.stringify({ action: "123", changeReason: "Nunya" }), | ||
} as APIGatewayEvent; | ||
|
||
const resultAction = await handler(noApackageEvent); | ||
|
||
expect(resultAction?.statusCode).toBe(400); | ||
}); | ||
it("should return 400 if existing item is entered", async () => { | ||
const noActionevent = { | ||
body: JSON.stringify({ | ||
id: TEST_ITEM_ID, | ||
adminChangeType: "NOSO", | ||
authority: "SPA", | ||
submitterEmail: "[email protected]", | ||
submitterName: "Name", | ||
status: "submitted", | ||
changeMade: "change", | ||
mockEvent: "mock-event", | ||
changeReason: "reason", | ||
}), | ||
} as APIGatewayEvent; | ||
|
||
const result = await handler(noActionevent); | ||
|
||
const expectedResult = { | ||
statusCode: 400, | ||
body: { message: `Package with id: ${TEST_ITEM_ID} already exists.` }, | ||
}; | ||
expect(result).toStrictEqual(expectedResult); | ||
}); | ||
|
||
it("should submit a new item", async () => { | ||
const validItem = { | ||
body: JSON.stringify({ | ||
id: NOT_EXISTING_ITEM_ID, | ||
authority: "Medicaid SPA", | ||
status: "submitted", | ||
submitterEmail: "[email protected]", | ||
submitterName: "Name", | ||
adminChangeType: "NOSO", | ||
changeMade: "change", | ||
mockEvent: "mock-event", | ||
changeReason: "reason", | ||
}), | ||
} as APIGatewayEvent; | ||
|
||
const result = await handler(validItem); | ||
|
||
const expectedResult = { | ||
statusCode: 200, | ||
body: { message: `${NOT_EXISTING_ITEM_ID} has been submitted.` }, | ||
}; | ||
expect(result).toStrictEqual(expectedResult); | ||
}); | ||
|
||
it("should fail to create a package ID with no topic name", async () => { | ||
process.env.topicName = ""; | ||
const validItem = { | ||
body: JSON.stringify({ | ||
id: NOT_EXISTING_ITEM_ID, | ||
authority: "Medicaid SPA", | ||
status: "submitted", | ||
submitterEmail: "[email protected]", | ||
submitterName: "Name", | ||
adminChangeType: "NOSO", | ||
mockEvent: "mock-event", | ||
changeMade: "change", | ||
changeReason: "reason", | ||
}), | ||
} as APIGatewayEvent; | ||
|
||
const result = await handler(validItem); | ||
const expectedResult = { | ||
statusCode: 500, | ||
body: { message: "Topic name is not defined" }, | ||
}; | ||
expect(result).toStrictEqual(expectedResult); | ||
}); | ||
}); |
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,115 @@ | ||
import { response } from "libs/handler-lib"; | ||
import { APIGatewayEvent } from "aws-lambda"; | ||
import { produceMessage } from "libs/api/kafka"; | ||
import { getPackage } from "libs/api/package"; | ||
import { ItemResult } from "shared-types/opensearch/main"; | ||
import { submitNOSOAdminSchema } from "./adminChangeSchemas"; | ||
import { z } from "zod"; | ||
|
||
import { getStatus } from "shared-types"; | ||
|
||
/* | ||
EXAMPLE EVENT JSON: | ||
{ | ||
"body": { | ||
"id": "CO-34304.R00.01", | ||
"authority": "1915(c)", | ||
"status": "Submitted", | ||
"submitterEmail": "george@example.com", | ||
"submitterName": "George Harrison", | ||
"adminChangeType": "NOSO", | ||
"mockEvent": "app-k", //needed for future actions | ||
"changeMade": "CO-34304.R00.01 added to OneMAC.Package not originally submitted in OneMAC. Contact your CPOC to verify the initial submission documents.", | ||
"changeReason": "Per request from CMS, this package was added to OneMAC." | ||
} | ||
} | ||
*/ | ||
|
||
interface submitMessageType { | ||
id: string; | ||
authority: string; | ||
status: string; | ||
submitterEmail: string; | ||
submitterName: string; | ||
adminChangeType: string; | ||
stateStatus: string; | ||
cmsStatus: string; | ||
} | ||
|
||
const sendSubmitMessage = async (item: submitMessageType) => { | ||
const topicName = process.env.topicName as string; | ||
if (!topicName) { | ||
throw new Error("Topic name is not defined"); | ||
} | ||
|
||
const currentTime = Date.now(); | ||
|
||
await produceMessage( | ||
topicName, | ||
item.id, | ||
JSON.stringify({ | ||
...item, | ||
packageId: item.id, | ||
origin: "SEATool", | ||
isAdminChange: true, | ||
adminChangeType: "NOSO", | ||
description: null, | ||
event: "NOSO", | ||
state: item.id.substring(0, 2), | ||
makoChangedDate: currentTime, | ||
changedDate: currentTime, | ||
statusDate: currentTime, | ||
}), | ||
); | ||
|
||
return response({ | ||
statusCode: 200, | ||
body: { message: `${item.id} has been submitted.` }, | ||
}); | ||
}; | ||
|
||
export const handler = async (event: APIGatewayEvent) => { | ||
if (!event.body) { | ||
return response({ | ||
statusCode: 400, | ||
body: { message: "Event body required" }, | ||
}); | ||
} | ||
|
||
try { | ||
const item = submitNOSOAdminSchema.parse( | ||
typeof event.body === "string" ? JSON.parse(event.body) : event.body, | ||
); | ||
|
||
const { stateStatus, cmsStatus } = getStatus(item.status); | ||
// check if it already exsists | ||
const currentPackage: ItemResult | undefined = await getPackage(item.id); | ||
|
||
if (currentPackage && currentPackage.found == true) { | ||
// if it exists and has origin OneMAC we shouldn't override it | ||
if (currentPackage._source.origin === "OneMAC") { | ||
return response({ | ||
statusCode: 400, | ||
body: { message: `Package with id: ${item.id} already exists.` }, | ||
}); | ||
} | ||
} | ||
|
||
return await sendSubmitMessage({ ...item, stateStatus, cmsStatus }); | ||
} catch (err) { | ||
console.error("Error has occured submitting package:", err); | ||
if (err instanceof z.ZodError) { | ||
return response({ | ||
statusCode: 400, | ||
body: { message: err.errors }, | ||
}); | ||
} | ||
|
||
return response({ | ||
statusCode: 500, | ||
body: { message: err.message || "Internal Server Error" }, | ||
}); | ||
} | ||
}; |
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
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
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