92 lines
2.6 KiB
JavaScript
92 lines
2.6 KiB
JavaScript
/**
|
|
* @swagger
|
|
* /api/email/notify:
|
|
* post:
|
|
* tags:
|
|
* - Email
|
|
* summary: Email to user, Phase 2
|
|
* consumes:
|
|
* - application/json
|
|
* description: Send email via gov notify
|
|
* produces: [
|
|
* "application/json",
|
|
* "application/xml"
|
|
* ]
|
|
* requestBody:
|
|
* name: Email
|
|
* description: Email body
|
|
* required: true
|
|
* content:
|
|
* 'application/json':
|
|
* schema:
|
|
* $ref: "#/components/schemas/emailBody"
|
|
* responses:
|
|
* 405:
|
|
* description: Failed
|
|
*/
|
|
|
|
import { consoleLogger, redactSensitive } from "../../../actions/core/logger";
|
|
import { isNonEmptyString, sanitizeString } from "../../../actions/core/guards";
|
|
import { getPreferredLanguage } from "../../../actions/services/accountService";
|
|
import { respondError, respondSuccess } from "../middleware/apiResponse";
|
|
|
|
export default async function ApiProxy(req, res) {
|
|
const data = req.body;
|
|
const emailAddress = sanitizeString(data?.emailAddress);
|
|
|
|
if (!isNonEmptyString(emailAddress)) {
|
|
return respondError(res, {
|
|
status: 400,
|
|
code: "EMAIL_ADDRESS_REQUIRED",
|
|
message: "emailAddress is required"
|
|
});
|
|
}
|
|
|
|
data.emailAddress = emailAddress;
|
|
|
|
if (data?.reference === "PEDW-NEW-CASEREF") {
|
|
try {
|
|
const prefLang = await getPreferredLanguage(data.emailAddress);
|
|
const preferredLanguage =
|
|
prefLang?.value?.[0]?.pinswg_preferredlanguage;
|
|
|
|
if (preferredLanguage === 846040000) {
|
|
data.templateId = "d2d9d0dc-e5d4-4da5-9156-775521d69c5c";
|
|
}
|
|
} catch (error) {
|
|
consoleLogger(error);
|
|
}
|
|
}
|
|
|
|
const NotifyClient = require("notifications-node-client").NotifyClient;
|
|
|
|
const notifyClient = new NotifyClient(process.env.NOTIFY_API_KEY);
|
|
//const emailReplyToId = process.env.EMAIL_REPLY_TO_ID;
|
|
|
|
console.info(
|
|
"notify_send_attempt",
|
|
redactSensitive({
|
|
templateId: data.templateId,
|
|
emailAddress: data.emailAddress,
|
|
reference: data.reference,
|
|
personalisation: data.personalisation
|
|
})
|
|
);
|
|
|
|
try {
|
|
await notifyClient.sendEmail(data.templateId, data.emailAddress, {
|
|
personalisation: data.personalisation,
|
|
reference: data.reference
|
|
});
|
|
|
|
return respondSuccess(res, data);
|
|
} catch (error) {
|
|
consoleLogger(error);
|
|
return respondError(res, {
|
|
status: 400,
|
|
code: "EMAIL_NOTIFY_FAILED",
|
|
message: "Failed to send notify email"
|
|
});
|
|
}
|
|
}
|