-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
159 lines (128 loc) · 4.76 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
158
159
"use strict";
const request = require("request");
const sqlite3 = require("sqlite3").verbose();
const config = require("./config");
const stockService = require("./lib/service/stockService");
const cron = require("node-cron");
const express = require("express");
var bodyParser = require('body-parser');
stockService.initDB();
let app = express();
// Schedule the stock updates
cron.schedule(config.cronSchedule, function() {
// Update all existing stocks
stockService.getAllStocksInDb(function(stocks) {
if(stocks && stocks.length > 0) {
let counter = 0;
const WAIT_BETWEEN_CALLS = 1000; // 1 second
stocks.forEach(function(stock) {
// This part may get too intensive if tracking lots of stocks.
// Please be respectful of Alphavantage's suggestion of max requests per minute
setTimeout(function() {
console.log("Updating " + stock.symbol);
stockService.updateStock(stock.symbol);
}, counter * WAIT_BETWEEN_CALLS);
counter++;
});
} else {
console.log("No stocks to update");
}
});
});
app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
res.header("Access-Control-Allow-Methods", "GET,POST,PUT,DELETE,OPTIONS");
next();
});
app.use(bodyParser.urlencoded({ extended: true })); //Use to process POST params
app.use(bodyParser.json()); //Use to process POST params
app.get("/stocks", function(req, res) {
if(req.query.stockCodes) {
let requestedStocks = req.query.stockCodes.split(",").map((stock) => stock.trim());
let startDate = req.query.start;
let endDate = req.query.end;
let stockData = [];
stockService.findAllForRange(requestedStocks, startDate, endDate, function(data) {
res.status(200).send(data);
});
} else {
res.status(400).send("Please specify stockCodes separated by ','. Ex: stockCodes=MSFT,GOOG,AAPL");
}
});
app.post("/stocks", function(req, res) {
if(req.body.stockCode) {
console.log("Updating " + req.body.stockCode);
stockService.updateStock(req.body.stockCode);
res.status(200).send();
} else {
res.status(400).send("Please specify stockCode");
}
});
app.get("/accounts", function(req, res) {
stockService.getAccountsWithPortfolios(function(accounts) {
//Also get their portfolios
res.status(200).send(accounts);
});
});
app.post("/accounts", function(req, res) {
console.log(req.body);
if(req.body.accountName) {
stockService.addAccount(req.body.accountName, function(account) {
res.status(200).send({success:true});
});
} else {
res.status(400).send({error:"No name specified"});
}
});
app.delete("/accounts", function(req, res) {
if(req.query.accountId) {
console.log("Deleting account: " + req.query.accountId);
stockService.deleteAccount(req.query.accountId);
res.status(200).send({});
} else {
res.status(400).send("Please specify an accountName");
}
});
app.get("/portfolios", function(req, res) {
if(req.query.portfolioId) {
stockService.getPortfolio(req.query.portfolioId, function(portfolio) {
res.status(200).send(portfolio);
});
} else {
res.status(400).send({error:"No portfolioId specified"});
}
});
app.post("/portfolios", function(req, res) {
console.log(req.body);
if(req.body.accountId && req.body.portfolioName) {
stockService.addPortfolio(req.body.portfolioName, req.body.accountId, function(account) {
res.status(200).send({success:true});
});
} else {
res.status(400).send({error:"No name or accountId specified"});
}
});
app.put("/portfolios", function(req, res) {
console.log(req.body);
if(req.body.portfolio) {
stockService.updatePortfolio(req.body.portfolio, function(account) {
res.status(200).send({success:true});
});
} else {
res.status(400).send({error:"No portfolio to update"});
}
});
app.delete("/portfolios", function(req, res) {
if(req.query.portfolioId) {
console.log("Deleting portfolio: " + req.query.portfolioId);
stockService.deletePortfolio(req.query.portfolioId);
res.status(200).send({});
} else {
res.status(400).send("Please specify a portfolioId");
}
});
var server = app.listen(process.env.PORT || 8080, function() {
var port = server.address().port;
console.log("Stock tracker listening at http://localhost:%s", port);
});