generated from github/welcome-to-github
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathscheduledJobs.js
113 lines (105 loc) · 4.34 KB
/
scheduledJobs.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
const nodemailer = require('nodemailer');
const schedule = require('node-schedule');
const keys = require('./keys');
const cquery = async (sql,)=>{
return new Promise((resolve)=>{
connection.query(sql,(err,result)=>{
if(err) console.log(err);
else resolve(result);
})
}
)
}
schedule.scheduleJob('0 0 2 * * *', async ()=>{
let copies=await cquery(`select * from bookCopies;`);
let len = copies.length;
for(let i=0;i<len;i++){
await cquery(`call cancelHold('${copies[i].ISBN}',${copies[i].copyID},@status);`);
let status = await cquery(`select @status;`);
if(status[0]['@status']==1){
console.log('hold revoked');
}else{
console.log('hold not revoked');
}
}
})
schedule.scheduleJob('0 0 0 * * *', async ()=>{
let users = await cquery(`select userID from bookCopiesUser;`);
let prev=-1;
let len=users.length;
for(let i =0 ;i<len;i++){
if(prev==users[i].userID){
continue;
}
let email = await cquery(`select email from user where userID=${users[i].userID};`);
let temp = await cquery(`call emailDetails(${users[i].userID},@unpaidFine);`);
// console.log(temp);
let fine = await cquery(`select @unpaidFine;`);
fine = fine[0]['@unpaidFine'];
let tlen=temp[0].length;
// console.log(tlen);
let mess="";
for(let j=0;j<tlen;j++){
let action = temp[0][j].action;
let cur = temp[0][j];
if(action == 'hold'){
mess+=`<p>Your hold request for the book '${cur.title}', ISBN: ${cur.ISBN}, copyID: ${cur.copyID} has expired on ${cur.dueDate}. Your unpaid fines are Rs. ${fine}.</p>`;
}else if(action == 'loan&hold'){
mess+=`<p>Your loan&hold on the book '${cur.title}', ISBN: ${cur.ISBN}, copyID: ${cur.copyID} has expired on ${cur.dueDate}. Please return it to the library as soon as possible to avoid getting fined. Your unpaid fines are Rs. ${fine}.</p>`;
}else if(action == 'loan'){
mess+=`<p>Your loan on the book '${cur.title}', ISBN: ${cur.ISBN}, copyID: ${cur.copyID} has expired on ${cur.dueDate}. Please renew it or return it to library as soon as possible to avoid getting fined. Your unpaid fines are Rs. ${fine}.</p>`;
}
// mail('Due Date Expired',mess,cur.email);
}
mail('Due Date Expired',mess,email[0].email);
prev=users[i].userID;
}
})
schedule.scheduleJob('0 0 0 * * *',async ()=>{
let users = await cquery(`select * user;`);
let len = users.length;
for(let i=0;i<len;i++){
await cquery(`call updateFine(${users[i].userID});`);
}
console.log('Fines Updated!');
})
schedule.scheduleJob('0 0 0 * * *', async ()=>{
let requests = await cquery(`select * from holdRequest;`);
let len =requests.length;
for(let i=0;i<len;i++){
await cquery(`call approveHold(${requests[i].userID},${requests[i].ISBN},@copyID,@dueDate,@status);`);
let status = await cquery(`select @status;`);
console.log(status);
if(status[0]['@status'] == 1){
let email = await cquery(`select email from user where userID = ${requests[i].userID};`);
let name = await cquery(`select title from book where ISBN=${requests[i].ISBN};`)
let duedate = await cquery(`select @dueDate;`);
let copyid = await cquery(`select @copyID;`);
let subject = "Your Hold Request was approved."
let body = `The hold request that you had registered for the book '${name[0].title}' was approved. Please collect it from the library before ${duedate[0]['@dueDate']}. Your copyID is ${copyid[0]['@copyID']}.`;
console.log(subject,body,email);
mail(subject,body,email[0].email);
}
}
})
const mail = async (subject,body,email)=>{
var transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
user: keys.ac_user,
pass: keys.ac_pass
},
});
var mailOptions = {
to: email,
subject,
html: body
};
transporter.sendMail(mailOptions, (err, info) => {
if (err) { throw err; }
else {
console.log("email sent:" + info.response);
}
})
}