-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.js
76 lines (70 loc) · 2.62 KB
/
utils.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
const DataModel = require('./dist/models/DataModel');
const mongoose = require('mongoose');
const converter = require('json-2-csv');
const sendAttachment = function(transporter, mailOptions) {
transporter.sendMail(mailOptions, function(err, info) {
if (err) {
console.log(err);
} else {
console.log("Message sent: " + info.response);
}
});
};
const sendCSV = function(transporter, mailOptions, CSVOptions) {
DataModel.find({}).lean().exec(function(err, data) {
const now = new Date().toString().split(' ').slice(0,5).join(' ');
mailOptions.subject = 'CSV Data for ' + now;
const CSVString = converter.json2csv(data, function(err, csv) {
mailOptions.attachments = [{
'filename': now + '-CSV.txt',
'content': csv
}];
// send e-mail
sendAttachment(transporter, mailOptions);
}, CSVOptions);
});
};
const sendBoth = function(transporter, mailOptions, CSVOptions) {
DataModel.find({}).lean().exec(function(err, data) {
const now = new Date().toString().split(' ').slice(0,5).join(' ');
mailOptions.subject = 'CSV and JSON Data for ' + now;
const CSVString = converter.json2csv(data, function(err, csv) {
const JSONString = JSON.stringify(data);
mailOptions.attachments = [{
'filename': now + '-CSV.txt',
'content': csv
},
{
'filename': now + '-JSON.txt',
'content': JSONString
}];
// send e-mail
sendAttachment(transporter, mailOptions);
}, CSVOptions);
});
};
const sendJSON = function(transporter, mailOptions) {
DataModel.find({}).lean().exec(function(err, data) {
const now = new Date().toString().split(' ').slice(0,5).join(' ');
mailOptions.subject = 'JSON Data for ' + now;
const JSONString = JSON.stringify(data);
mailOptions.attachments = [{
'filename': now + '-JSON.txt',
'content': JSONString
}];
sendAttachment(transporter, mailOptions);
});
};
const pastRange = function(range) {
const nowMilli = Date.now();
const now = new Date(nowMilli).toISOString();
const before = new Date(nowMilli - range).toISOString();
return { $gt: before, $lte: now };
};
module.exports = {
past: pastRange,
sendAttachment: sendAttachment,
sendCSV: sendCSV,
sendJSON: sendJSON,
sendBoth: sendBoth
};