Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HOFF-869 -Refactor notify component to enable email attachments #472

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion components/notify/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,14 @@ module.exports = config => {
if (typeof settings.recipient !== 'string' || !settings.recipient.includes('@')) {
throw new Error('hof-behaviour-emailer: invalid recipient');
}

if (typeof config.subject === 'function') {
settings.subject = config.subject(req.sessionModel.toJSON(), req.translate);
} else {
settings.subject = config.subject;
}
if (config.attachment && config.attachment !== undefined) {
settings.attachment = config.attachment(req.sessionModel.toJSON(), req.translate);
}

return settings;
})
Expand Down
40 changes: 33 additions & 7 deletions components/notify/notify.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,50 @@
'use strict';

const NotifyClient = require('notifications-node-client').NotifyClient;
const { v4: uuidv4 } = require('uuid');
const config = require('../../config/hof-defaults');
const logger = require('../../lib/logger');

module.exports = class Notify {
constructor(opts) {
const options = opts || {};
this.options = options;
this.logger = logger(config);
this.notifyClient = new NotifyClient(options.notifyApiKey);
this.notifyTemplate = options.notifyTemplate;
}

send(email) {
async sendEmail(templateId, recipient, personalisation) {
const reference = uuidv4();

return this.notifyClient.sendEmail(this.notifyTemplate, email.recipient, {
personalisation: {
'email-subject': email.subject,
'email-body': email.body
},
reference });
try {
await this.notifyClient.sendEmail(templateId, recipient, {
personalisation: personalisation,
reference });
this.logger.log('info', 'Email sent');
} catch (error) {
this.logger.log('error', error.response ? error.response.data : error.message);
throw new Error(error.response ? error.response.data : error.message);
}
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we also have a separate sendEmailWithAttachment function?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did some investigation and it is will good but it is out of scope for this bit of work - Please raise it with the project team, thanks

async send(email) {
let personalisation = {
'email-subject': email.subject,
'email-body': email.body
};

if (email.attachment && email.attachment !== undefined) {
personalisation = Object.assign({'email-attachment':
this.notifyClient.prepareUpload(email.attachment)}, personalisation);
}

try {
await this.sendEmail(this.notifyTemplate, email.recipient, personalisation);
} catch (error) {
this.logger.log('error', error.response ? error.response.data : error.message);
throw new Error(error.response ? error.response.data : error.message);
}
}
};

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "hof",
"description": "A bootstrap for HOF projects",
"version": "21.0.5",
"version": "21.0.6-notify-attachments-beta.1",
"license": "MIT",
"main": "index.js",
"author": "HomeOffice",
Expand Down
Loading