-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
107 lines (91 loc) · 3.54 KB
/
app.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
const express = require('express')
const mongoose = require('mongoose')
const cors = require('cors')
const nodemailer = require('nodemailer')
const cron = require('node-cron')
const axios = require('axios')
require('dotenv/config')
const app = express()
//json parser & middleware
app.use(cors())
app.use(express.json({extended: true, limit: '50mb'}));
app.use(express.urlencoded({ extended: true, limit: '50mb' }));
//import routes
const deviceRoutes = require('./routes/device')
const userRoutes = require('./routes/user')
const timelineRoutes = require('./routes/timeline')
const adminRoutes = require('./routes/admin')
//routes example
app.use('/api/device', deviceRoutes)
app.use('/api/user', userRoutes)
app.use('/api/timeline', timelineRoutes)
app.use('/api/admin', adminRoutes)
//connect to db
mongoose.connect(process.env.DB_CONNECTION, { useNewUrlParser: true, useUnifiedTopology: true })
let db = mongoose.connection
db.on('error', console.error.bind(console, 'Database connect error'))
db.once('open', ()=>{ console.log('Database is Connected') })
//listen
app.listen(process.env.PORT || 80, ()=>{
console.log(`Server running in port ${process.env.PORT}`)
})
cron.schedule('40 15 8 * * *', function(){
console.log('running task');
let Data = []
axios.get(`https://api-egg.herokuapp.com/api/device/time`)
.then(res => {
if (res.status === 200) {
Data = res.data.timesUp
// console.log(Data)
for (let x = 0; x < Data.length; x++) {
let slots = ''
// console.log(Data[x].slot)
for (let y = 0; y < Data[x].slot.length; y++) {
let slot = Data[x].slot[y];
slots += '[ Slot ' + slot.toString() + ' ] '
}
// console.log(slots)
let date = new Date();
const templateEmail = {
from: process.env.MAIL_NAME,
to: Data[x].email,
subject: 'Pemberitahuan[TELUR MENETAS] - Egg Cracker',
html: `
<h3> Egg Cracker </h3>
<p>Pemberitahuan : Pada <b>${slots}</b> di Device anda telah melebihi 25 hari
Silahkan Cek Inkubator Anda https://eggcracker.site</p>
<br>
<p>
<span>[email protected]</span><br>
<span>Surabaya, Indonesia</span>
</p>
${date}
`,
}
let transporter = nodemailer.createTransport({
// service: "Gmail",
host: "smtp.gmail.com",
port: 587,
secure: false, // true for 465, false for other ports
requireTLS: true,
auth: {
user: process.env.MAIL_NAME, // generated ethereal user
pass: process.env.MAIL_KEY // generated ethereal password
}
});
const emailSender = async()=>{
try {
await transporter.sendMail(templateEmail)
console.log('email terkirim')
} catch (error) {
console.log(error)
}
}
emailSender()
}
}
},err => {
console.log(err)
});
})
// "bcryptjs": "^2.4.3",