forked from CoderDojo/cp-dojos-service
-
Notifications
You must be signed in to change notification settings - Fork 0
/
email-notifications.js
57 lines (49 loc) · 1.63 KB
/
email-notifications.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
'use strict';
var fs = require('fs');
var path = require('path');
var _ = require('lodash');
module.exports = function (options) {
var seneca = this;
var plugin = 'email-notifications';
var so = seneca.options();
options = seneca.util.deepextend({}, so[plugin], options);
seneca.add({role: plugin, cmd: 'send'}, send_notification);
function send_notification (args, done) {
if (options.sendemail && options.email) {
var emailCode = args.code + args.locality;
if (!fs.existsSync(path.join(__dirname, '/email-templates/', emailCode))) emailCode = args.code + 'en_US';
if (!args.to) return done(null, {ok: false, why: 'No recipient set.'});
seneca.act({
role: 'mail', cmd: 'send',
from: args.from || options.sendFrom,
code: emailCode,
to: args.to,
replyTo: args.replyTo || options.sendFrom,
subject: args.subject,
content: args.content,
headers: setHeader()
}, done);
} else {
done();
}
/**
* @function : recover the predefined header and append the function name to it
*/
function setHeader () {
var headerContainer = _.cloneDeep(options.email.headers);
if (emailCode) {
var headerKey = _.keys(headerContainer)[0];
if (headerKey) {
var JSONHeaderValue = JSON.parse(headerContainer[headerKey]);
var categoryHeaderKey = _.keys(JSONHeaderValue)[0];
JSONHeaderValue[categoryHeaderKey].push(emailCode);
headerContainer[headerKey] = JSON.stringify(JSONHeaderValue);
}
}
return headerContainer;
}
}
return {
name: plugin
};
};