diff --git a/pages/api/email/getall.js b/pages/api/email/getall.js index 9aaefd01..d906dc34 100644 --- a/pages/api/email/getall.js +++ b/pages/api/email/getall.js @@ -393,15 +393,9 @@ export default async function CombinedApiProxy(req, res) { }; }) ); - // res.status(200).json(results); // Step 3: Group results by contact_email const groupedByEmail = _.groupBy(results, "contact_email"); - //Convert to array format - // const groupedArray = Object.entries(groupedByEmail).map(([email, entries]) => ({ contact_email: email, entries })); - - //console.log(buildNotifyPayloads(groupedByEmail)); - const payloads = buildNotifyPayloads(groupedByEmail); // watchlistByEmail should be defined // Iterate and send @@ -410,7 +404,6 @@ export default async function CombinedApiProxy(req, res) { for (const payload of payloads) { if (!payload) continue; // skip null entries - //console.log(payload); const result = { email: payload.email_address, reference: payload.reference, diff --git a/pages/api/email/notify.js b/pages/api/email/notify.js index ca9ec1ba..edf8a7d0 100644 --- a/pages/api/email/notify.js +++ b/pages/api/email/notify.js @@ -72,10 +72,8 @@ export default async function ApiProxy(req, res) { .sendEmail(data.templateId, data.emailAddress, { personalisation: data.personalisation, reference: data.reference - // emailReplyToId: emailReplyToId, }) - .then((response) => { - //console.log("thisis the response", response); + .then(() => { return respondSuccess(res, data); }) .catch((error) => { @@ -86,5 +84,4 @@ export default async function ApiProxy(req, res) { message: "Failed to send notify email" }); }); - //return res.status(200).json(data); } diff --git a/tests/phase21/email-handler-contract.test.cjs b/tests/phase21/email-handler-contract.test.cjs index 91756bb9..c648325c 100644 --- a/tests/phase21/email-handler-contract.test.cjs +++ b/tests/phase21/email-handler-contract.test.cjs @@ -277,6 +277,66 @@ test("getcaseref failure returns CASE_REF_FETCH_FAILED", async () => { assert.strictEqual(res.state.jsonBody.error.code, "CASE_REF_FETCH_FAILED"); }); +test("notify returns EMAIL_ADDRESS_REQUIRED when emailAddress is missing", async () => { + const mod = loadModule("pages/api/email/notify.js", { + respondError: respondErrorMock, + respondSuccess: respondSuccessMock, + consoleLogger: () => {}, + redactSensitive: (value) => value, + sanitizeString: () => "", + isNonEmptyString: () => false, + getPreferredLanguage: async () => ({ value: [] }) + }); + + const req = { body: { templateId: "t1", reference: "OTHER" } }; + const res = createRes(); + await mod.default(req, res); + + assert.strictEqual(res.state.statusCode, 400); + assert.strictEqual(res.state.jsonBody.error.code, "EMAIL_ADDRESS_REQUIRED"); +}); + +test("notify send failure returns EMAIL_NOTIFY_FAILED", async () => { + const mod = loadModule("pages/api/email/notify.js", { + respondError: respondErrorMock, + respondSuccess: respondSuccessMock, + consoleLogger: () => {}, + redactSensitive: (value) => value, + sanitizeString: (value) => value, + isNonEmptyString: () => true, + getPreferredLanguage: async () => ({ value: [] }), + require: (name) => { + if (name === "notifications-node-client") { + return { + NotifyClient: function NotifyClient() { + return { + sendEmail: async () => { + throw new Error("notify failed"); + } + }; + } + }; + } + + return require(name); + } + }); + + const req = { + body: { + templateId: "t1", + emailAddress: "user@test.local", + personalisation: {}, + reference: "OTHER" + } + }; + const res = createRes(); + await mod.default(req, res); + + assert.strictEqual(res.state.statusCode, 400); + assert.strictEqual(res.state.jsonBody.error.code, "EMAIL_NOTIFY_FAILED"); +}); + const run = async () => { let passed = 0; for (const currentTest of tests) {