diff --git a/lib/lambda/processEmailsHandler.test.ts b/lib/lambda/processEmailsHandler.test.ts index 91017c964..dace4fd42 100644 --- a/lib/lambda/processEmailsHandler.test.ts +++ b/lib/lambda/processEmailsHandler.test.ts @@ -4,112 +4,206 @@ import { SESClient } from "@aws-sdk/client-ses"; import { handler } from "./processEmails"; import { KafkaRecord, KafkaEvent } from "shared-types"; import { Authority } from "shared-types"; - +import { SIMPLE_ID, WITHDRAW_RAI_ITEM_B, WITHDRAW_RAI_ITEM_C } from "mocks"; const nms = "new-medicaid-submission"; const ncs = "new-chip-submission"; const tempExtension = "temp-extension"; const withdrawPackage = "withdraw-package"; const contractingInitial = "contracting-initial"; const capitatedInitial = "capitated-initial"; +const withdrawRai = "withdraw-rai"; +const respondToRai = "respond-to-rai"; describe("process emails Handler", () => { it.each([ - [`should send an email for ${nms} with ${Authority.MED_SPA}`, Authority.MED_SPA, nms], - [`should send an email for ${nms} with ${Authority.CHIP_SPA}`, Authority.CHIP_SPA, nms], - [`should send an email for ${nms} with ${Authority["1915b"]}`, Authority["1915b"], nms], - [`should send an email for ${nms} with ${Authority["1915c"]}`, Authority["1915c"], nms], - [`should send an email for ${ncs} with ${Authority.MED_SPA}`, Authority.MED_SPA, ncs], - [`should send an email for ${ncs} with ${Authority.CHIP_SPA}`, Authority.CHIP_SPA, ncs], - [`should send an email for ${ncs} with ${Authority["1915b"]}`, Authority["1915b"], ncs], - [`should send an email for ${ncs} with ${Authority["1915c"]}`, Authority["1915c"], ncs], + [ + `should send an email for ${respondToRai} with ${Authority.MED_SPA}`, + Authority.MED_SPA, + respondToRai, + SIMPLE_ID, + ], + [ + `should send an email for ${respondToRai} with ${Authority.CHIP_SPA}`, + Authority.CHIP_SPA, + respondToRai, + SIMPLE_ID, + ], + [ + `should send an email for ${respondToRai} with ${Authority["1915b"]}`, + Authority["1915b"], + respondToRai, + SIMPLE_ID, + ], + [ + `should send an email for ${respondToRai} with ${Authority["1915c"]}`, + Authority["1915c"], + respondToRai, + SIMPLE_ID, + ], + [ + `should send an email for ${nms} with ${Authority.MED_SPA}`, + Authority.MED_SPA, + nms, + SIMPLE_ID, + ], + [ + `should send an email for ${nms} with ${Authority.CHIP_SPA}`, + Authority.CHIP_SPA, + nms, + SIMPLE_ID, + ], + [ + `should send an email for ${nms} with ${Authority["1915b"]}`, + Authority["1915b"], + nms, + SIMPLE_ID, + ], + [ + `should send an email for ${nms} with ${Authority["1915c"]}`, + Authority["1915c"], + nms, + SIMPLE_ID, + ], + [ + `should send an email for ${ncs} with ${Authority.MED_SPA}`, + Authority.MED_SPA, + ncs, + SIMPLE_ID, + ], + [ + `should send an email for ${ncs} with ${Authority.CHIP_SPA}`, + Authority.CHIP_SPA, + ncs, + SIMPLE_ID, + ], + [ + `should send an email for ${ncs} with ${Authority["1915b"]}`, + Authority["1915b"], + ncs, + SIMPLE_ID, + ], + [ + `should send an email for ${ncs} with ${Authority["1915c"]}`, + Authority["1915c"], + ncs, + SIMPLE_ID, + ], [ `should send an email for ${tempExtension} with ${Authority.MED_SPA}`, Authority.MED_SPA, tempExtension, + SIMPLE_ID, ], [ `should send an email for ${tempExtension} with ${Authority.CHIP_SPA}`, Authority.CHIP_SPA, tempExtension, + SIMPLE_ID, ], [ `should send an email for ${tempExtension} with ${Authority["1915b"]}`, Authority["1915b"], tempExtension, + SIMPLE_ID, ], [ `should send an email for ${tempExtension} with ${Authority["1915c"]}`, Authority["1915c"], tempExtension, + SIMPLE_ID, ], [ `should send an email for ${withdrawPackage} with ${Authority.MED_SPA}`, Authority.MED_SPA, withdrawPackage, + SIMPLE_ID, ], [ `should send an email for ${withdrawPackage} with ${Authority.CHIP_SPA}`, Authority.CHIP_SPA, withdrawPackage, + SIMPLE_ID, ], [ `should send an email for ${withdrawPackage} for ${ncs} with ${Authority["1915b"]}`, Authority["1915b"], withdrawPackage, + SIMPLE_ID, ], [ `should send an email for ${withdrawPackage} with ${Authority["1915c"]}`, Authority["1915c"], withdrawPackage, + SIMPLE_ID, ], [ `should send an email for ${contractingInitial} with ${Authority.MED_SPA}`, Authority.MED_SPA, contractingInitial, + SIMPLE_ID, ], [ `should send an email for ${contractingInitial} with ${Authority.CHIP_SPA}`, Authority.CHIP_SPA, contractingInitial, + SIMPLE_ID, ], [ `should send an email for ${contractingInitial} with ${Authority["1915b"]}`, Authority["1915b"], contractingInitial, + SIMPLE_ID, ], [ `should send an email for ${contractingInitial} with ${Authority["1915c"]}`, Authority["1915c"], contractingInitial, + SIMPLE_ID, ], [ `should send an email for ${capitatedInitial} with ${Authority.MED_SPA}`, Authority.MED_SPA, capitatedInitial, + SIMPLE_ID, ], [ `should send an email for ${capitatedInitial} with ${Authority.CHIP_SPA}`, Authority.CHIP_SPA, capitatedInitial, + SIMPLE_ID, ], [ `should send an email for ${capitatedInitial} with ${Authority["1915b"]}`, Authority["1915b"], capitatedInitial, + SIMPLE_ID, ], [ `should send an email for ${capitatedInitial} with ${Authority["1915c"]}`, Authority["1915c"], capitatedInitial, + SIMPLE_ID, ], - ])("%s", async (_, auth, eventType) => { + [ + `should send an email for ${withdrawRai} with ${Authority["1915b"]}`, + Authority["1915b"], + withdrawRai, + WITHDRAW_RAI_ITEM_B, + ], + [ + `should send an email for ${withdrawRai} with ${Authority["1915c"]}`, + Authority["1915c"], + withdrawRai, + WITHDRAW_RAI_ITEM_C, + ], + ])("%s", async (_, auth, eventType, id) => { const callback = vi.fn(); const secSPY = vi.spyOn(SESClient.prototype, "send"); const mockEvent: KafkaEvent = { records: { "mock-topic": [ { - key: Buffer.from("VA").toString("base64"), + key: Buffer.from(id).toString("base64"), value: Buffer.from( JSON.stringify({ origin: "mako", @@ -132,3 +226,45 @@ describe("process emails Handler", () => { expect(secSPY).toHaveBeenCalledTimes(2); }); }); +describe("process emails Handler failures", () => { + it.each([ + [ + `should send an email for ${withdrawRai} with ${Authority["1915b"]} and fail due to not finding it`, + Authority["1915b"], + withdrawRai, + SIMPLE_ID, + ], + [ + `should send an email for ${withdrawRai} with ${Authority["1915c"]} and fail due to not finding it`, + Authority["1915c"], + withdrawRai, + SIMPLE_ID, + ], + ])("%s", async (_, auth, eventType, id = SIMPLE_ID) => { + const callback = vi.fn(); + const mockEvent: KafkaEvent = { + records: { + "mock-topic": [ + { + key: Buffer.from(id).toString("base64"), + value: Buffer.from( + JSON.stringify({ + origin: "mako", + event: eventType, + authority: auth, + }), + ).toString("base64"), + headers: {}, + timestamp: 1732645041557, + offset: "0", + partition: 0, + topic: "mock-topic", + } as unknown as KafkaRecord, + ], + }, + eventSource: "", + bootstrapServers: "", + }; + await expect(() => handler(mockEvent, {} as Context, callback)).rejects.toThrow(); + }); +}); diff --git a/lib/lambda/search.test.ts b/lib/lambda/search.test.ts index 4b49522ca..02532a03f 100644 --- a/lib/lambda/search.test.ts +++ b/lib/lambda/search.test.ts @@ -29,7 +29,7 @@ describe("getSearchData Handler", () => { const body = JSON.parse(res.body); expect(body).toBeTruthy(); expect(body?.hits?.hits).toBeTruthy(); - expect(body?.hits?.hits?.length).toEqual(14); + expect(body?.hits?.hits?.length).toEqual(16); }); it("should handle errors during processing", async () => { diff --git a/lib/lambda/update/updatePackage.test.ts b/lib/lambda/update/updatePackage.test.ts index df208f9ae..c95f792f2 100644 --- a/lib/lambda/update/updatePackage.test.ts +++ b/lib/lambda/update/updatePackage.test.ts @@ -7,7 +7,7 @@ import { EXISTING_ITEM_PENDING_ID, CAPITATED_INITIAL_ITEM_ID, CAPITATED_INITIAL_NEW_ITEM_ID, - WEIRD_ID, + SIMPLE_ID, } from "mocks"; vi.mock("libs/handler-lib", () => ({ response: vi.fn((data) => data), @@ -204,7 +204,7 @@ describe("handler", () => { it("should fail to update a package with bad existing id format", async () => { const noActionevent = { body: JSON.stringify({ - packageId: WEIRD_ID, + packageId: SIMPLE_ID, action: "update-id", changeReason: "Nunya", updatedId: "SS-120", @@ -223,7 +223,7 @@ describe("handler", () => { process.env.topicName = ""; const noActionevent = { body: JSON.stringify({ - packageId: WEIRD_ID, + packageId: SIMPLE_ID, action: "update-values", changeReason: "Nunya", updatedFields: {}, @@ -240,7 +240,7 @@ describe("handler", () => { it("should fail to update a package - No valid fields ", async () => { const noActionevent = { body: JSON.stringify({ - packageId: WEIRD_ID, + packageId: SIMPLE_ID, action: "update-values", changeReason: "Nunya", updatedFields: { badfield: "nothing" }, @@ -257,7 +257,7 @@ describe("handler", () => { it("should fail to update a package - Id can not be updated ", async () => { const noActionevent = { body: JSON.stringify({ - packageId: WEIRD_ID, + packageId: SIMPLE_ID, action: "update-values", changeReason: "Nunya", updatedFields: { id: "cant update ID here" }, diff --git a/lib/libs/email/content/withdrawRai/index.tsx b/lib/libs/email/content/withdrawRai/index.tsx index 5dde78064..a8e34e6a4 100644 --- a/lib/libs/email/content/withdrawRai/index.tsx +++ b/lib/libs/email/content/withdrawRai/index.tsx @@ -5,7 +5,7 @@ import { render } from "@react-email/render"; import { EmailProcessingError } from "libs/email/errors"; const getWithdrawRaiEvent = async (id: string) => { - const event = await getLatestMatchingEvent(id, "WithdrawRai"); + const event = await getLatestMatchingEvent(id, "withdraw-rai"); if (!event) { return null; diff --git a/lib/libs/email/index.ts b/lib/libs/email/index.ts index da2eea2ff..ca21f9e4d 100644 --- a/lib/libs/email/index.ts +++ b/lib/libs/email/index.ts @@ -115,7 +115,7 @@ export async function getLatestMatchingEvent( } // Filter matching events - const events = item.hits.hits.filter((event) => event._source.actionType === actionType); + const events = item.hits.hits.filter((event) => event._source.event === actionType); // Check if any matching events were found if (!events.length) { diff --git a/mocks/data/items.ts b/mocks/data/items.ts index 50b9f1e05..458418a0a 100644 --- a/mocks/data/items.ts +++ b/mocks/data/items.ts @@ -17,7 +17,7 @@ export const HI_TEST_ITEM_ID = "HI-0000.R00.00"; export const CAPITATED_INITIAL_ITEM_ID = "SS-2234.R00.00"; export const CAPITATED_INITIAL_NEW_ITEM_ID = "SS-1235.R00.00"; export const CAPITATED_AMEND_ITEM_ID = "VA-2234.R11.01"; -export const WEIRD_ID = "VA"; +export const SIMPLE_ID = "VA"; export const CONTRACTING_INITIAL_ITEM_ID = "MD-007.R00.00"; export const CONTRACTING_AMEND_ITEM_ID = "MD-007.R00.01"; export const MISSING_CHANGELOG_ITEM_ID = "MD-008.R00.00"; @@ -26,6 +26,8 @@ export const INITIAL_RELEASE_APPK_ITEM_ID = "MD-010.R00.01"; export const EXISTING_ITEM_APPROVED_APPK_ITEM_ID = "MD-012.R00.01"; export const SUBMISSION_ERROR_ITEM_ID = "Throw Submission Error"; export const GET_ERROR_ITEM_ID = "Throw Get Item Error"; +export const WITHDRAW_RAI_ITEM_B = "VA-2234.R11.02"; +export const WITHDRAW_RAI_ITEM_C = "VA-2234.R11.03"; const items: Record = { [EXISTING_ITEM_ID]: { @@ -37,7 +39,7 @@ const items: Record = { actionType: "New", }, }, - [WEIRD_ID]: { + [SIMPLE_ID]: { _id: EXISTING_ITEM_ID, found: true, _source: { @@ -399,6 +401,71 @@ const items: Record = { ], }, }, + [WITHDRAW_RAI_ITEM_B]: { + _id: WITHDRAW_RAI_ITEM_B, + found: true, + _source: { + id: WITHDRAW_RAI_ITEM_B, + seatoolStatus: SEATOOL_STATUS.PENDING, + actionType: "respond-to-rai", + authority: "1915(b)", + state: "MD", + origin: "OneMAC", + changelog: [ + { + _id: `${WITHDRAW_RAI_ITEM_B}-001`, + _source: { + id: `${WITHDRAW_RAI_ITEM_B}-0001`, + event: "respond-to-rai", + packageId: WITHDRAW_RAI_ITEM_B, + }, + }, + ], + }, + }, + [WITHDRAW_RAI_ITEM_C]: { + _id: WITHDRAW_RAI_ITEM_C, + found: true, + _source: { + id: WITHDRAW_RAI_ITEM_C, + seatoolStatus: SEATOOL_STATUS.PENDING, + actionType: "respond-to-rai", + authority: "1915(c)", + state: "MD", + leadAnalystName: "lead test", + leadAnalystEmail: "Lead test email", + reviewTeam: [ + { + name: "Test", + email: "testemail", + }, + ], + origin: "OneMAC", + changelog: [ + { + _id: `${WITHDRAW_RAI_ITEM_C}-001`, + _source: { + id: `${WITHDRAW_RAI_ITEM_C}-0001`, + submitterName: "Testmctex", + submitterEmail: "fakeemail;", + event: "respond-to-rai", + packageId: WITHDRAW_RAI_ITEM_C, + }, + }, + + { + _id: `${WITHDRAW_RAI_ITEM_C}-002`, + _source: { + id: `${WITHDRAW_RAI_ITEM_C}-0002`, + submitterName: "Testmctex", + submitterEmail: "fakeemail;", + event: "withdraw-rai", + packageId: WITHDRAW_RAI_ITEM_C, + }, + }, + ], + }, + }, [EXISTING_ITEM_APPROVED_APPK_ITEM_ID]: { _id: EXISTING_ITEM_APPROVED_APPK_ITEM_ID, found: true,