-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmailJobServer.js
More file actions
110 lines (101 loc) · 3.17 KB
/
mailJobServer.js
File metadata and controls
110 lines (101 loc) · 3.17 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
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
const mongoose = require('mongoose');
const keys = require('./config/keys');
const MailList =require('./models/MailListModel');
const User=require('./models/userModel');
const refreshToken = require('./config/refreshToken2');
const nodemailer = require("nodemailer");
const request = require('request-promise');
const CronJob = require('cron').CronJob;
var smtpTransport = nodemailer.createTransport({
service: "gmail",
host: "smtp.gmail.com",
auth: {
user: keys.gmail.user,
pass: keys.gmail.password
}
});
mongoose.connect(keys.mongodb.dbURI,{ useNewUrlParser: true }, function() {
console.log('connected to mongodb');
job.start();
});
// Sending Mail to Users
const job = new CronJob('0 0 5 * * *', function() {
var date= new Date();
var day=parseInt(date.getDate());
var month=parseInt(date.getMonth())+1;
var year=parseInt(date.getFullYear());
User.findOne({codechefId :keys.codechef.username.toLowerCase()}).then(function(currentUser){
MailList.find(function(err,docs){
if(docs.length)
{
refreshToken.refreshAccessToken(currentUser['refreshToken'],keys.codechef.username.toLowerCase()).then(function(accessToken){
var options = {
method: 'GET',
uri: 'https://api.codechef.com/contests?status=future',
headers: {
'Accept': 'application/json',
'Authorization': 'Bearer ' + accessToken
},
json: true // Automatically parses the JSON string in the response
};
request(options)
.then(function (result) {
var message=[];
var events=result['result']['data']['content']['contestList'];
for(var i=0;i<events.length;i++)
{
var date2=events[i].startDate;
var year2=parseInt(date2.substring(0,4));
var month2=parseInt(date2.substring(5,7));
var day2=parseInt(date2.substring(8,10));
var diff=(year2-year)*365+(month2-month)*30+(day2-day);
// console.log(diff);
if(diff<3)
{
message.push({name:events[i].name,start:events[i].startDate,end:events[i].endDate,link:"www.codechef.com/"+events[i].code});
}
}
var i=0;
recursive();
function recursive()
{
var to=docs[i].Id;
var subject="Contest Reminder";
var text="This is a system generated mail please do not reply.<br>"
for(var j=0;j<message.length;j++)
{
text+="<h3>"+message[j].name+"</h3><br>\
Starts at "+message[j].start+"<br>\
Ends at "+message[j].end+"<br>\
<a href='"+message[j].link+"'>Go to contest</a><br>\
<br>";
}
var mailOptions={
to : to,
subject : subject,
html : text
}
// console.log(mailOptions);
smtpTransport.sendMail(mailOptions, function(error, response){
if(error){
console.log(error);
}
else{
console.log("Message sent to " + docs[i].Id);
i++;
recursive();
}
});
}
})
.catch(function(err){
console.log(err);
});
})
.catch(function(err){
console.log(err);
});
}
});
});
});