hof 22.4.2-beta-patch-fixes → 22.5.0-notify-attachments-beta.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -38,12 +38,14 @@ module.exports = config => {
|
|
38
38
|
if (typeof settings.recipient !== 'string' || !settings.recipient.includes('@')) {
|
39
39
|
throw new Error('hof-behaviour-emailer: invalid recipient');
|
40
40
|
}
|
41
|
-
|
42
41
|
if (typeof config.subject === 'function') {
|
43
42
|
settings.subject = config.subject(req.sessionModel.toJSON(), req.translate);
|
44
43
|
} else {
|
45
44
|
settings.subject = config.subject;
|
46
45
|
}
|
46
|
+
if (config.attachment && config.attachment !== undefined) {
|
47
|
+
settings.attachment = config.attachment(req.sessionModel.toJSON(), req.translate);
|
48
|
+
}
|
47
49
|
|
48
50
|
return settings;
|
49
51
|
})
|
@@ -1,24 +1,50 @@
|
|
1
1
|
'use strict';
|
2
|
+
|
2
3
|
const NotifyClient = require('notifications-node-client').NotifyClient;
|
3
4
|
const { v4: uuidv4 } = require('uuid');
|
5
|
+
const config = require('../../config/hof-defaults');
|
6
|
+
const logger = require('../../lib/logger');
|
4
7
|
|
5
8
|
module.exports = class Notify {
|
6
9
|
constructor(opts) {
|
7
10
|
const options = opts || {};
|
8
11
|
this.options = options;
|
12
|
+
this.logger = logger(config);
|
9
13
|
this.notifyClient = new NotifyClient(options.notifyApiKey);
|
10
14
|
this.notifyTemplate = options.notifyTemplate;
|
11
15
|
}
|
12
16
|
|
13
|
-
|
17
|
+
async sendEmail(templateId, recipient, personalisation) {
|
14
18
|
const reference = uuidv4();
|
15
19
|
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
20
|
+
try {
|
21
|
+
await this.notifyClient.sendEmail(templateId, recipient, {
|
22
|
+
personalisation: personalisation,
|
23
|
+
reference });
|
24
|
+
this.logger.log('info', 'Email sent');
|
25
|
+
} catch (error) {
|
26
|
+
this.logger.log('error', error.response ? error.response.data : error.message);
|
27
|
+
throw new Error(error.response ? error.response.data : error.message);
|
28
|
+
}
|
29
|
+
}
|
30
|
+
|
31
|
+
async send(email) {
|
32
|
+
let personalisation = {
|
33
|
+
'email-subject': email.subject,
|
34
|
+
'email-body': email.body
|
35
|
+
};
|
36
|
+
|
37
|
+
if (email.attachment && email.attachment !== undefined) {
|
38
|
+
personalisation = Object.assign({'email-attachment':
|
39
|
+
this.notifyClient.prepareUpload(email.attachment)}, personalisation);
|
40
|
+
}
|
41
|
+
|
42
|
+
try {
|
43
|
+
await this.sendEmail(this.notifyTemplate, email.recipient, personalisation);
|
44
|
+
} catch (error) {
|
45
|
+
this.logger.log('error', error.response ? error.response.data : error.message);
|
46
|
+
throw new Error(error.response ? error.response.data : error.message);
|
47
|
+
}
|
22
48
|
}
|
23
49
|
};
|
24
50
|
|