-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
125 lines (109 loc) · 4.03 KB
/
index.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
'use strict';
const Notifier = require('@runnerty/module-core').Notifier;
const interpreter = require('@runnerty/interpreter-core');
const nodemailer = require('nodemailer');
const aws = require('@aws-sdk/client-ses');
const { defaultProvider } = require('@aws-sdk/credential-provider-node');
const path = require('path');
const fs = require('fs');
class mailNotifier extends Notifier {
constructor(notification) {
super(notification);
}
send(notification) {
const endOptions = {};
notification.to = notification.to ? notification.to.toString() : '';
notification.cc = notification.cc ? notification.cc.toString() : '';
notification.bcc = notification.bcc ? notification.bcc.toString() : '';
const filesReads = [];
const templateDir = path.resolve(notification.templateDir, notification.template);
const htmlTemplate = path.resolve(templateDir, 'html.html');
const txtTemplate = path.resolve(templateDir, 'text.txt');
filesReads.push(this.readFilePromise('html', htmlTemplate));
filesReads.push(this.readFilePromise('text', txtTemplate));
try {
Promise.all(filesReads).then(res => {
let html_data;
let text_data;
if (res[0].hasOwnProperty('html')) {
[html_data, text_data] = [res[0].html.toString(), res[1].text.toString()];
} else {
[html_data, text_data] = [res[1].html.toString(), res[0].text.toString()];
}
const textData = [];
textData.push(interpreter(html_data, notification));
textData.push(interpreter(text_data, notification));
Promise.all(textData).then(res => {
const [html, text] = res;
const mailOptions = {
from: notification.from,
to: notification.to,
cc: notification.cc,
bcc: notification.bcc,
subject: notification.subject,
text: text,
html: html,
attachments: notification.attachments
};
if (notification.disable) {
this.logger('warn', 'Mail sender is disable.');
endOptions.messageLog = 'Mail sender is disable.';
this.end(endOptions);
} else {
// SES Transport
if (notification.transport?.service === 'SES') {
if (!notification.transport.region) throw new Error('Must indicate the region to use SES transport');
const ses = new aws.SES({
apiVersion: '2010-12-01',
region: notification.transport.region,
defaultProvider
});
const transport = nodemailer.createTransport({ SES: { ses, aws } });
if (notification.transport.ses) {
mailOptions.ses = Object.assign(notification.transport.ses, notification.ses);
}
transport.sendMail(mailOptions, err => {
if (err) {
endOptions.messageLog = 'Mail sender:' + JSON.stringify(err);
this.end(endOptions);
} else {
this.end();
}
});
} else {
// SMTP Transport
const transport = nodemailer.createTransport(notification.transport);
transport.sendMail(mailOptions, err => {
if (err) {
endOptions.messageLog = 'Mail sender:' + JSON.stringify(err);
this.end(endOptions);
} else {
this.end();
}
});
}
}
});
});
} catch (err) {
endOptions.end = 'error';
endOptions.messageLog = 'Mail sender:' + JSON.stringify(err);
this.end(endOptions);
}
}
readFilePromise(type, file) {
return new Promise((resolve, reject) => {
fs.readFile(file, (err, data) => {
const res = {};
if (err) {
res[type] = err;
reject(res);
} else {
res[type] = data;
resolve(res);
}
});
});
}
}
module.exports = mailNotifier;