-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
157 lines (144 loc) · 5.49 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
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
const cron = require("node-cron");
const express = require("express");
const mongoose = require('mongoose');
let nodemailer = require("nodemailer");
const Order = require('./api/models/order');
app = express();
// create mail transporter
var transporter = nodemailer.createTransport({
host: "smtp.mailtrap.io",
port: 2525,
auth: {
user: '3348b04943bfb0',
pass: '01c1ffe9a3b4bb'
}
});
// cron.schedule("*/2 * * * *", function() {
// console.log("---------------------");
// console.log("Running Cron Job");
// mongoose.connect(process.env.MONGODB_URI || 'mongodb://localhost:27017/meeks', {useNewUrlParser: true});
// Order.find()
// .exec()
// .then(docs => {
// if(docs){
// console.log(docs)
// }else{
// console.log('Not Found')
// }
// });
// });
// sending emails at periodic intervals
cron.schedule("*/2 * * * *", function() {
console.log("---------------------");
console.log("Running Cron Job");
mongoose.connect(process.env.MONGODB_URI || 'mongodb://localhost:27017/meeks', {useNewUrlParser: true});
Order.find()
.where('status').equals('CANCELLED')
.limit(1)
.exec()
.then(doc => {
if(doc){
// const response = {
// count: docs.length,
// orderArray: docs.map(doc => {
// return {
// id: doc._id,
// uniqueId: doc.uniqueId,
// user: doc.user,
// cart: doc.cart,
// reference: doc.reference,
// status: doc.status,
// createdAt: doc.createdAt
// }
// })
// }
// const d = {
// newArray: response.orderArray.map(m => {
// return {
// status: m.status,
// id: m.id,
// uniqueId: m.uniqueId,
// full_name: m.user.full_name,
// email: m.user.email
// }
// })
// }
// console.log(d.newArray)
// var rep = d.newArray.filter(obj => JSON.stringify(obj.status));
// console.log(rep[0]);
// res.status(200).json({
// _id: doc._id,
// uniqueId: doc.uniqueId,
// user: doc.user,
// cart: doc.cart
// })
// var rep = doc.filter(obj => {
// return {
// _id: doc._id,
// uniqueId: doc.uniqueId,
// user: doc.user,
// cart: doc.cart
// }
// });
//console.log(JSON.stringify(doc));
var email = doc.map(obj => obj.user.email).pop();
var status = doc.map(obj => obj.status).pop();
var full_name = doc.map(obj => obj.user.full_name).pop();
id = doc.map(obj => obj._id).pop();
var uniqueId = doc.map(obj => obj.uniqueId).pop();
// console.log(email);
// console.log(status);
// console.log(full_name);
// console.log(id);
// console.log(uniqueId);
if(status === "CANCELLED"){
mongoose.connect(process.env.MONGODB_URI || 'mongodb://localhost:27017/meeks', {useNewUrlParser: true});
Order.remove({_id: id}).exec().then(function (result) {
if(result){
const mailOptions = {
from: '[email protected]',
to: email,
subject: 'Order Has Been Cancelled',
text: 'Hello,\n\n' + full_name + 'Your Order:' + id + ' ' + 'has been successfully cancelled' };
transporter.sendMail(mailOptions, function (err) {
if (err) {
throw err;
}else{
console.log('A verification email has been sent to ' + email + '.');
// res.status(200).json('A verification email has been sent to ' + doc.user.email + '.');
}
});
// transporter.sendMail ends
}
else{
return;
}
});
}
}else{
console.log('Not found')
}
})
.catch(err => {
throw err
});
})
// sending emails at periodic intervals
// cron.schedule("*/2 * * * *", function(){
// console.log("---------------------");
// console.log("Running Cron Job");
// let mailOptions = {
// from: "[email protected]",
// to: "[email protected]",
// subject: `Not a GDPR update ;)`,
// text: `Hi there, this email was automatically sent by us`
// };
// transporter.sendMail(mailOptions, function(error, info) {
// if (error) {
// throw error;
// } else {
// console.log("Email successfully sent!");
// }
// });
// });
app.listen("3128");