-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnodejs.js
More file actions
236 lines (224 loc) · 7.09 KB
/
Copy pathnodejs.js
File metadata and controls
236 lines (224 loc) · 7.09 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
var mysql = require('mysql');
var moment = require('moment');
var diff = require('diff');
var cron = require('node-cron')
var nodemailer = require("nodemailer");
var smtpTransport = require('nodemailer-smtp-transport');
var stripe = require('stripe')('sk_test_6pmcf1MsiY5pTMPMaOqxbfXJ');
var async = require('async')
var smtpTransport = nodemailer.createTransport({
service: "gmail",
host: "smtp.gmail.com",
secure: "true",
auth: {
user: "bhavya@iface.io",
pass: "!@!^&iitjee@)!@2"
}
});
//var con = mysql.createConnection({
// host: "192.168.123.2",
// user: "root",
// password: "redhat",
// database: "iface_crm_admin"
//});
var pool = mysql.createPool({
connectionLimit : 10,
host: "192.168.123.2",
user: "root",
password: "redhat",
database: "iface_crm_admin"
});
pool.getConnection(function (err, connection) {
if (err) {
connection.release();
callback(null, err);
throw err;
}
console.log("connected!")
cron.schedule("13 19 * * *", function() {
connection.query("SELECT * FROM tbl_companies", function (err, result, fields) {
if (err) throw err;
connection.query("delete from tbl_expired_databases ;")
for (let i=0; i< result.length; i++) {
var date = moment(result[i].subscription_end_date)
var today = moment(new Date())
var diffDays = date.diff(today, 'hours')
if (diffDays <= "0"){
connection.query("insert into tbl_expired_databases values ('"+ result[i].company_pid+"');")
console.log("expired");
}else {
console.log(diffDays +"hr are remaning")
}
}
});
connection.on('error', function (err) {
connection.release();
callback(null, err);
throw err;
});
});
cron.schedule("15 13 19 * * * ",function() {
connection.query("select * from tbl_expired_databases;", function (err, result, fields) {
if (err) throw err;
connection.query("delete from tbl_delete_database ;")
console.log(result)
if (result.length === 0) {
console.log("No Databases are going to delete")
}else {
let companyIdArray = []
for (var i = 0; i < result.length; i++) {
companyIdArray.push(parseInt(result[i].company_pid))
}
connection.query("select * from tbl_companies where company_pid in ("+companyIdArray+");", function (err1, result1, field) {
if (err1) throw err1
async.map(result1,function check(i,mapcallback){
var companyinfo = "";
console.log(i)
var date = moment(i.subscription_end_date)
var today = moment(new Date())
var diffDays = date.diff(today, 'hours')
//console.log(diffDays)
if (diffDays <= 0){
if ( i.is_subscribed === 1 ) {
stripe.subscriptions.retrieve(i.payment_subscription_id, function(err, subscription) {
if(err){
console.log(err);
mapcallback(err , null)
}else{
//console.log(subscription.current_period_end)
var enddate = moment(subscription.current_period_end*1000)
//console.log(enddate)
var diff_check = enddate.diff(today, 'hours')
if (diff_check <= 0){
database_delete(function(err , companyinfo){
if(err){
mapcallback(err , null);
}else{
console.log("------------");
console.log(companyinfo)
mapcallback(null , companyinfo)
}
})
}else{
mapcallback("User subscription is extended over stripe" , null)
}
console.log("stripe checked")
}
});
} else {
database_delete(function(err , companyinfo){
if(err){
mapcallback(err , null);
}else{
console.log("------------");
console.log(companyinfo)
mapcallback(null , companyinfo)
}
})
}
}else{
mapcallback(null,companyinfo)
}
function database_delete(callback){
console.log(callback)
connection.query("insert into tbl_delete_database values ('"+ i.company_pid+"');" , function(err , result){
if(err){
console.log(err);
callback(err , null)
}else{
companyinfo = {"companyid": i.company_pid,"companyname": i.company_name}
console.log(companyinfo);
callback(null ,companyinfo);
}
})
}
},function(err,companyinfo){
console.log("inside callback")
if(err){
console.log(err);
}else{
console.log(companyinfo)
}
sendMail(companyinfo)
})
});
// for (let j=0; j <result.length; j++) {
// connection.query("select * from tbl_companies where company_pid='"+result[j].company_pid+"'", function (err1, result1, field) {
// if (err1) throw err1
// var date = moment(result1.subscription_end_date)
// var today = moment(new Date())
// // var diffDays = parseInt((result[i].date - today) / (1000 * 60 * 60 ))
// var diffDays = date.diff(today, 'hours')
// if (diffDays <= 0){
// connection.query("insert into tbl_delete_database values ('"+ result[j].company_pid+"');")
// companyid = companyid+"\n"+result[j].company_pid;
// console.log(companyid)
// }else {
// console.log(diffDays +"hr are remaning")
// }
// });
// }
function sendMail(companyinfo){
console.log("hello")
console.log(companyinfo)
console.log(companyinfo.length)
let table_body = "";
for(let i=0 ; i < companyinfo.length ; i++){
console.log(typeof companyinfo[i]);
// if(companyinfo[i] !== "undefined"){
// table_body += "<tr><td>"+companyinfo[i].companyid+"</td><td>"+companyinfo[i].companyname+"</td></tr>"
// }
}
var mailOptions={
to : "bhavya@iface.io",
subject : "company's are going to be delete today",
html: `<html>
<head>
<style>
#itemList {
font-family: arial, sans-serif;
border-collapse: collapse;
width: 100%;
}
td, th {
border: 1px solid #dddddd;
text-align: left;
padding: 8px;
}
tr:nth-child(even) {
background-color: #dddddd;
}
</style>
</head>
<body>
<h2>Companies List</h2>
<table id="itemList">
<tr>
<th>Company Pid</th>
<th>Company Name</th>
</tr>
</table>
<br>
<a href = 'www.google.com'>View Details</a>
</body>
</html>`
}
console.log(mailOptions);
smtpTransport.sendMail(mailOptions, function(error, response){
if(error){
console.log(error);
}
else{
console.log("Message sent: ");
}
});
}
}
});
connection.on('error', function (err) {
connection.release();
callback(null, err);
throw err;
});
});
});