-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
77 lines (65 loc) · 2.13 KB
/
server.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
const express = require('express');
const cors = require('cors');
const mongoose = require('mongoose');
const CronJob = require("cron").CronJob;
require('dotenv').config();
const sendEmail = require('./services/email')
const getExchangeRates = require('./services/exchangeRates')
const transformData = require('./utils/transformData')
const calculateRemainingTime = require('./utils/cron')
const { createReport } = require('./services/reports')
const app = express();
const PORT = process.env.PORT || 5000;
//cors middleware
app.use(cors());
//this will allow us to parse json
app.use(express.json());
const uri = process.env.ATLAS_URI;
//start connection
console.log("Attempting database connection...")
mongoose.connect(uri, { useNewUrlParser: true, useCreateIndex: true, useUnifiedTopology: true }
);
const connection = mongoose.connection;
connection.once('open', () => {
console.log("MongoDB database connection established successfully");
})
const receiverOne = process.env.RECEIVER
const receiverTwo = process.env.RECEIVER_TWO
const thresholdOne = process.env.THRESHOLD_EUR
const thresholdTwo = process.env.THRESHOLD_EUR_TWO
//define two cron jobs for the different recipients
const cronJob = new CronJob(
"*/20 * * * *",
async () => {
const exchangeRateCRO = await getExchangeRates('CRO')
const transformedRatesFormat = transformData(exchangeRateCRO)
await createReport(transformedRatesFormat, thresholdOne)
sendEmail(receiverOne, thresholdOne)
},
null,
true,
"UTC"
);
const cronJobTwo = new CronJob(
"* 10,16,22 * * *",
async () => {
const exchangeRateCRO = await getExchangeRates('CRO')
const transformedRatesFormat = transformData(exchangeRateCRO)
await createReport(transformedRatesFormat, thresholdTwo)
sendEmail(receiverTwo, thresholdTwo)
},
null,
true,
"UTC"
);
setInterval(() => {
calculateRemainingTime(cronJob)
calculateRemainingTime(cronJobTwo)
}, 1000);
app.get('/', async (req, res) => {
res.send("Hello")
})
//server is listening on port...
app.listen(PORT, () => {
console.log(`Server is running on port: ${PORT}`);
});