-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
126 lines (106 loc) · 5.61 KB
/
server.js
File metadata and controls
126 lines (106 loc) · 5.61 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
const express = require('express');
const path = require('path');
const bodyParser = require('body-parser');
const nodemailer = require('nodemailer');
const MongoClient = require('mongodb').MongoClient;
const axios = require('axios');
const nodeMailerSMTPSettings = require('./secret').nodeMailerSettings;
const urlDb = require('./secret').urlDb;
const app = express();
const port = process.env.PORT || 5000;
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
// nodeMailer settings
let transporter = nodemailer.createTransport(nodeMailerSMTPSettings);
transporter.verify(function (error, success) {
if (error) {
console.log(error);
} else {
console.log('Server is ready to take our messages');
}
});
// email sender using nodeMailer
const emailer = (emailAddress, btc, setValue, isPriceHigher) => {
const mailIfPriceIsHigher = "<html>\n\
<body style='text-align: center'>\n\
<h2>Hi</h2>\n\
<h5>We would like to inform you that Bitcoin price is currently higher than what you set (" + setValue + " $).</h5>\n\
<h3> Currently the price of Bitcoin is " + btc + " $.</h3>\n\
<p> We do not want to spam your inbox, therefore we will not sent you more notification. If you want to be notify again please use our app.</p>\n\
</body>\n\
</html>";
const mailIfPriceIsBelow = "<html>\n\
<body style='text-align: center'>\n\
<h2>Hi</h2>\n\
<h5>We would like to inform you that Bitcoin price is currently below what you set (" + setValue + " $).</h5>\n\
<h3> Currently the price of Bitcoin is " + btc + " $.</h3>\n\
<p> We do not want to spam your inbox, therefore we will not sent you more notification. If you want to be notify again please use our app.</p>\n\
</body>\n\
</html>";
const sender_email = emailAddress;
const htmlContent = isPriceHigher ? mailIfPriceIsHigher : mailIfPriceIsBelow;
let mailOptions = {
from: 'Crypto Notificator <username@domain.com>',
to: sender_email,
subject: "Bitcoin price changed",
html: htmlContent
};
transporter.sendMail(mailOptions).then(console.log('messege sent'));
}
// API calls
app.get('/api/title', (req, res) => {
res.send({ title: 'Bitcoin Notifier' });
});
// verify user's email address => save data(email, choosen price) to DB => inform user about successful request
app.post('/api/form', (req, res) => {
console.log(req.body);
const emailRegexp = /^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/;
if (emailRegexp.test(req.body.emailAddress)) {
db.collection('quotes').save(req.body, (err, result) => {
if (err) return console.log(err);
console.log('saved to database');
})
const messageForHigherPrice = req.body.priceIsHigherThan ? `We will inform you when the Bitcoin price will be higher than: ${req.body.priceIsHigherThan}.` : "";
const messageForLowerPrice = req.body.priceIsLowerThan ? `We will inform you when the Bitcoin price will be below: ${req.body.priceIsLowerThan}.` : "";
res.send(
`We received your request. ${messageForHigherPrice} ${messageForLowerPrice} We will notify you on following email address: ${req.body.emailAddress}`,
);
}
});
if (process.env.NODE_ENV === 'production') {
// Serve any static files
app.use(express.static(path.join(__dirname, 'client/build')));
// Handle React routing, return all requests to React app
app.get('*', function(req, res) {
res.sendFile(path.join(__dirname, '/client/build', 'index.html'));
});
}
setInterval(() => {getBtcPriceCompareAndNotify()}, 1000);
// get bitcoin price from btc exchange => get user's selected price from DB => compare prices and eventually notify user via emailer
const getBtcPriceCompareAndNotify = () => {
axios.get('https://api.coindesk.com/v1/bpi/currentprice.json')
.then(response => {
const btcPrice = parseFloat(response.data.bpi.USD.rate.replace(",",""));
db.collection('quotes').find().toArray()
.then(response => {
response.forEach((element) => {
if (!element.wasNotifiedForHigh && btcPrice > element.priceIsHigherThan) {
emailer(element.emailAddress, btcPrice, element.priceIsHigherThan, true);
db.collection('quotes').updateMany({ emailAddress: element.emailAddress }, { $set: { wasNotifiedForHigh: true }});
}
if (!element.wasNotifiedForLow && btcPrice < element.priceIsLowerThan) {
emailer(element.emailAddress, btcPrice, element.priceIsLowerThan, false);
db.collection('quotes').updateMany({ emailAddress: element.emailAddress }, { $set: { wasNotifiedForLow: true }});
}
})
})
.catch(error => {console.log(error);});
})
.catch(error => {console.log(error);});
}
var db
MongoClient.connect(urlDb, (err, client) => {
if (err) return console.log(err);
db = client.db('crypto');
app.listen(port, () => console.log(`Listening on port ${port}`));
})