Files
pedwfrontend/pages/api/email/notify.js
T

71 lines
2.0 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 } from "../../../actions/core/logger";
import { getPreferredLanguage } from "../../../actions/services/accountService";
export default async function ApiProxy(req, res) {
var data = req.body;
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);
}
}
var NotifyClient = require("notifications-node-client").NotifyClient;
const notifyClient = new NotifyClient(process.env.NOTIFY_API_KEY);
//const emailReplyToId = process.env.EMAIL_REPLY_TO_ID;
console.log("///////////////\n Sending email \ns//////////////", data);
notifyClient
.sendEmail(data.templateId, data.emailAddress, {
personalisation: data.personalisation,
reference: data.reference
// emailReplyToId: emailReplyToId,
})
.then((response) => {
//console.log("thisis the response", response);
return res.status(200).json(data);
})
.catch((error) => {
consoleLogger(error);
return res.status(400).json(error);
});
//return res.status(200).json(data);
}