-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
140 lines (119 loc) · 3.92 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
import bodyParser from 'body-parser';
import express from 'express';
import axios from 'axios';
import dbSnapshot from './db-snapshot.js';
const app = express();
const PAUSE_TRESHOLD = 6 * 1000;
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'
);
next();
});
const port = 3000;
//Confirm the API version from your stripe dashboard
// parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: false }));
// parse application/json
app.use(bodyParser.json());
app.listen(port, () => {
console.log(`Pealock server listening at http://localhost:${port}`);
});
const fakeDb = dbSnapshot.reduce((acc, user) => {
acc[user.userDetails?.id || 0] = { ...user , lastEventTimeStamp: null };
return acc;
}, {});
app.get('/users/detail', async (req, res, next) => {
const { username, password } = req.query;
const token = await getToken(username, password);
const response = await axios.get(
'https://backstage-api.com/user',
{
headers: {
'Content-Type': 'application/json',
'x-service-id': '5e0ad1b0-515e-11e9-a7ed-371ac744bd33',
'x-application-id': '67eae630-d7ec-11ea-9077-3fff30e06028',
Authorization: `Bearer ${token}`,
}
}
).catch(next);
try {
const profiles = await axios.get(
'https://backstage-api.com/user/profiles',
{
headers: {
'Content-Type': 'application/json',
'x-service-id': '5e0ad1b0-515e-11e9-a7ed-371ac744bd33',
'x-application-id': '67eae630-d7ec-11ea-9077-3fff30e06028',
Authorization: `Bearer ${token}`,
}
}
).catch(next);
res.json({ ...response.data.user, profile: profiles.data.find(profile => profile.selected) });
} catch (e) {
console.log(e);
}
});
const getToken = async (username = '[email protected]', password = 'qwerty123456') => {
const response = await axios.post(
'https://backstage-api.com/user/login',
{ password, username},
{
headers: {
'Content-Type': 'application/json',
'x-service-id': '5e0ad1b0-515e-11e9-a7ed-371ac744bd33',
'x-application-id': '67eae630-d7ec-11ea-9077-3fff30e06028'
}
}
);
return response.data.token;
}
const getUserDbEntry = (userId) => {
const entry = fakeDb[userId];
if (!entry) fakeDb[userId] = {};
return fakeDb[userId];
}
app.post('/event', async (req, res) => {
const userDatabaseEntry = getUserDbEntry(req.body.userId);
userDatabaseEntry.userDetails = req.body.user;
const timeFromLastHeartbeat = req.body.timestamp - (userDatabaseEntry.lastEventTimeStamp || req.body.timestamp);
userDatabaseEntry.lastEventTimeStamp = req.body.timestamp;
if (timeFromLastHeartbeat < PAUSE_TRESHOLD) {
userDatabaseEntry.totalTimeWatched = timeFromLastHeartbeat + (userDatabaseEntry.totalTimeWatched || 0);
}
const points = Math.round(userDatabaseEntry.totalTimeWatched / 100);
userDatabaseEntry.totalPoints = points;
res.status(200).send('OK');
});
app.get('/users/:userId/points/', async (req, res) => {
const userDatabaseEntry = getUserDbEntry(req.params.userId);
res.json({ pointValue: userDatabaseEntry.totalPoints || 0 });
});
app.get('/users/:userId/lastwatched/', async (req, res, next) => {
const { username, password } = req.query;
const token = await getToken(username, password);
const response = await axios.get(
'https://backstage-api.com/playlists/watch-history',
{
headers: {
'Content-Type': 'application/json',
'x-service-id': '5e0ad1b0-515e-11e9-a7ed-371ac744bd33',
'x-application-id': '67eae630-d7ec-11ea-9077-3fff30e06028',
Authorization: `Bearer ${token}`,
}
}
).catch(next);
return res.json(response.data.items);
});
app.get('/users/:userId/', async (req, res) => {
const userDatabaseEntry = getUserDbEntry(req.params.userId);
res.json({ userDatabaseEntry });
});
app.get('/users', async (req, res) => {
const values = Object.values(fakeDb);
res.json(values);
});
app.get('/user/events', async (req, res) => {
});