-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathserver.js
112 lines (97 loc) · 2.72 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
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
'use strict';
var express = require('express');
var bodyParser = require("body-parser");
var app = express();
app.use(bodyParser.json());
let initialTransactions = [{
id: '123',
date: '2016-12-11T12:23:34Z',
description: 'A bag of spanners',
amount: '35.25',
currency: 'GBP'
},{
id: '124',
date: '2016-12-12T01:58:59Z',
description: 'Hot chocholate',
amount: '12.50',
currency: 'GBP'
},{
id: '125',
date: '2016-12-12T06:11:06Z',
description: 'Subscriptions - Magazine',
amount: '5.99',
currency: 'GBP'
},{
id: '126',
date: '2016-12-13T10:03:17Z',
description: 'Movie rental',
amount: '3.99',
currency: 'GBP'
}];
let initialBalance = 192.27;
let accounts = {};
// Auth & checking request headers
//////////////////////////////////
app.use((req, res, next) => {
req.user = req.get('Authorization') ? accounts[req.get('Authorization').substring(7)] : null;
let isNotAuthorised = ['/', '/#'].indexOf(req.path) === -1 && !req.user;
let headerError = !req.accepts('application/json') || (req.method === 'POST' && !req.is('json'));
if (isNotAuthorised) {
res.status(401).send();
} else if (headerError) {
res.status(406).send();
} else {
next();
}
});
// Endpoints
////////////
app.get('/', (req, res) => {
res.send('Interviewer says hello! 👋');
});
app.post('/#', (req, res) => {
let token = Math.random().toString(36).replace(/[^a-z]+/g, '');
accounts[token] = {
balance: initialBalance,
transactions: JSON.parse(JSON.stringify(initialTransactions))
};
res.status(200).json({
'token': token
});
});
app.get('/transactions', (req, res) => {
let delay = Math.random() * 1000 + 200;
setTimeout(() => {
res.status(200).json(req.user.transactions);
}, delay);
});
app.get('/balance', (req, res) => {
res.status(200).json({
'balance': "" + (Math.round(req.user.balance * 100) / 100).toFixed(2),
'currency': 'GBP'
});
});
app.post('/spend', (req, res) => {
let amount = parseFloat(req.body.amount) || 0;
let canSpend = amount > 0 && ((req.user.balance * 100) / 100) - ((amount * 100) / 100) >= 0;
// TODO: more input validation
if (canSpend) {
let transactions = req.user.transactions;
transactions.push({
id: parseInt(transactions[transactions.length - 1].id) + 1 + "",
date: req.body.date,
description: req.body.description,
amount: amount.toFixed(2),
currency: req.body.currency
});
req.user.balance -= amount;
res.status(204).send();
} else {
res.status(406).send();
}
});
// start listening
var server = app.listen(process.env.PORT || 8080, () => {
var port = server.address().port;
console.log('App now running on port', port);
});