This repository was archived by the owner on Dec 14, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathemail-notifications.js
More file actions
52 lines (43 loc) · 1.44 KB
/
email-notifications.js
File metadata and controls
52 lines (43 loc) · 1.44 KB
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
'use strict';
var fs = require('fs');
var CpTranslations = require('cp-translations');
var I18NHelper = require('cp-i18n-lib');
var i18nHelper = new I18NHelper({
poFilePath: CpTranslations.getPoFilePath(),
poFileName: 'messages.po',
domain: 'coder-dojo-platform'
});
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) {
var subject = args.subject;
var subjectVariables = args.subjectVariables || [];
var subjectTranslation;
if (options.sendemail) {
var emailCode = args.code + args.locality;
if (!fs.existsSync(CpTranslations.getEmailTemplatePath(emailCode))) emailCode = args.code + 'en_US';
if (!args.to) return done(null, {ok: false, why: 'No recipient set.'});
subjectTranslation = i18nHelper.getClosestTranslation(args.locality, subject);
if (subjectTranslation === null) {
return done(null, {ok: false, why: 'Invalid email subject.'});
}
seneca.act({
role: 'mail', cmd: 'send',
from: options.sendFrom,
code: emailCode,
to: args.to,
subject: subjectTranslation.fetch(subjectVariables),
content: args.content
}, done);
} else {
done();
}
}
return {
name: plugin
};
};