diff --git a/pages/api/email/getcaseref.js b/pages/api/email/getcaseref.js index b4aa179d..ddc59198 100644 --- a/pages/api/email/getcaseref.js +++ b/pages/api/email/getcaseref.js @@ -1,6 +1,4 @@ import axios from "axios"; -import CryptoJS from "crypto-js"; -import _ from "lodash"; import { azureHeaders } from "../../../actions/core/headers"; import { consoleLogger } from "../../../actions/core/logger"; import { getToken } from "../../../actions/core/token"; @@ -35,8 +33,6 @@ export default async function ApiProxy(req, res) { azureHeaders(token.access_token) ) .then(({ data }) => { - var dataStr; - const flattenedResults = data.value.map(flattenWatchlistEntry); return respondSuccess(res, flattenedResults); diff --git a/pages/api/email/getmailinglist.js b/pages/api/email/getmailinglist.js index 44ff7297..3a980c57 100644 --- a/pages/api/email/getmailinglist.js +++ b/pages/api/email/getmailinglist.js @@ -1,14 +1,10 @@ import axios from "axios"; -import CryptoJS from "crypto-js"; -import _ from "lodash"; import { azureHeaders } from "../../../actions/core/headers"; import { consoleLogger } from "../../../actions/core/logger"; import { getToken } from "../../../actions/core/token"; import { hashAPIPath } from "../../../actions/core/hash"; import { respondError, respondSuccess } from "../middleware/apiResponse"; -const WORDKEY = process.env.HASHKEY; - const WEBAPI_URL = process.env.RELAY_ROOT || "https://dev-pedw-ns.servicebus.windows.net/dev-pedw-hc/"; @@ -37,8 +33,6 @@ export default async function ApiProxy(req, res) { azureHeaders(token.access_token) ) .then(({ data }) => { - var dataStr; - const flattenedResults = data.value.map(flattenWatchlistEntry); return respondSuccess(res, flattenedResults); diff --git a/pages/api/email/notify.js b/pages/api/email/notify.js index a9cef216..ca9ec1ba 100644 --- a/pages/api/email/notify.js +++ b/pages/api/email/notify.js @@ -63,10 +63,10 @@ export default async function ApiProxy(req, res) { const notifyClient = new NotifyClient(process.env.NOTIFY_API_KEY); //const emailReplyToId = process.env.EMAIL_REPLY_TO_ID; - console.log( - "///////////////\n Sending email \ns//////////////", - redactSensitive(data) - ); + consoleLogger({ + event: "notify_send_attempt", + payload: redactSensitive(data) + }); notifyClient .sendEmail(data.templateId, data.emailAddress, { diff --git a/tests/phase21/email-handler-contract.test.cjs b/tests/phase21/email-handler-contract.test.cjs index 95744a67..91756bb9 100644 --- a/tests/phase21/email-handler-contract.test.cjs +++ b/tests/phase21/email-handler-contract.test.cjs @@ -150,6 +150,133 @@ test("getall top-level failure returns EMAIL_COMBINED_FETCH_FAILED", async () => ); }); +test("getmailinglist success returns flattened mailing list results", async () => { + const mod = loadModule("pages/api/email/getmailinglist.js", { + respondError: respondErrorMock, + respondSuccess: respondSuccessMock, + getToken: async () => ({ access_token: "token" }), + hashAPIPath: () => "&hash=expected", + azureHeaders: () => ({}), + axios: { + get: async () => ({ + data: { + value: [ + { + pinswg_emailnotifications: true, + pinswg_Contact: { + contactid: "c1", + emailaddress1: "a@test.local" + } + } + ] + } + }) + }, + consoleLogger: () => {} + }); + + const req = { query: {} }; + const res = createRes(); + await mod.default(req, res); + + assert.strictEqual(res.state.statusCode, 200); + assert.deepStrictEqual(JSON.parse(JSON.stringify(res.state.jsonBody)), [ + { + pinswg_emailnotifications: true, + contactid: "c1", + contact_email: "a@test.local" + } + ]); +}); + +test("getmailinglist failure returns MAILING_LIST_FETCH_FAILED", async () => { + const mod = loadModule("pages/api/email/getmailinglist.js", { + respondError: respondErrorMock, + respondSuccess: respondSuccessMock, + getToken: async () => ({ access_token: "token" }), + hashAPIPath: () => "&hash=expected", + azureHeaders: () => ({}), + axios: { + get: async () => { + throw new Error("mailing list failed"); + } + }, + consoleLogger: () => {} + }); + + const req = { query: {} }; + const res = createRes(); + await mod.default(req, res); + + assert.strictEqual(res.state.statusCode, 400); + assert.strictEqual( + res.state.jsonBody.error.code, + "MAILING_LIST_FETCH_FAILED" + ); +}); + +test("getcaseref success returns flattened case ref results", async () => { + const mod = loadModule("pages/api/email/getcaseref.js", { + respondError: respondErrorMock, + respondSuccess: respondSuccessMock, + getToken: async () => ({ access_token: "token" }), + hashAPIPath: () => "&hash=expected", + azureHeaders: () => ({}), + axios: { + get: async () => ({ + data: { + value: [ + { + pinswg_emailnotifications: true, + pinswg_Contact: { + contactid: "c2", + emailaddress1: "b@test.local" + } + } + ] + } + }) + }, + consoleLogger: () => {} + }); + + const req = { query: {} }; + const res = createRes(); + await mod.default(req, res); + + assert.strictEqual(res.state.statusCode, 200); + assert.deepStrictEqual(JSON.parse(JSON.stringify(res.state.jsonBody)), [ + { + pinswg_emailnotifications: true, + contactid: "c2", + contact_email: "b@test.local" + } + ]); +}); + +test("getcaseref failure returns CASE_REF_FETCH_FAILED", async () => { + const mod = loadModule("pages/api/email/getcaseref.js", { + respondError: respondErrorMock, + respondSuccess: respondSuccessMock, + getToken: async () => ({ access_token: "token" }), + hashAPIPath: () => "&hash=expected", + azureHeaders: () => ({}), + axios: { + get: async () => { + throw new Error("case ref failed"); + } + }, + consoleLogger: () => {} + }); + + const req = { query: {} }; + const res = createRes(); + await mod.default(req, res); + + assert.strictEqual(res.state.statusCode, 400); + assert.strictEqual(res.state.jsonBody.error.code, "CASE_REF_FETCH_FAILED"); +}); + const run = async () => { let passed = 0; for (const currentTest of tests) {