-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathborga-services.js
103 lines (91 loc) · 2.59 KB
/
borga-services.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
"use strict";
const errors = require("./borga-errors");
module.exports = function (data_ext, data_int) {
async function getMostPopularGames() {
const games = await data_ext.getMostPopularGames();
return { games };
}
async function searchGame(query) {
if (!query) {
throw errors.MISSING_PARAM("parameter 'query' is missing");
}
return data_ext.findGame(query);
}
async function getGameById(id) {
if (!id) {
throw errors.MISSING_PARAM("parameter 'id' is missing");
}
return data_ext.getGameById(id);
}
async function getGameDetails(id) {
if (!id) {
throw errors.MISSING_PARAM("parameter 'id' is missing");
}
return data_ext.getGameDetails(id);
}
async function getUsername(token) {
if (!token) {
throw errors.UNAUTHENTICATED("No token");
}
const username = await data_int.tokenToUsername(token);
if (!username) {
throw errors.UNAUTHENTICATED("Bad token");
}
return username;
}
async function checkAndGetUser(username, password) {
if (!username || !password) {
throw errors.MISSING_PARAM("missing credentials");
}
const user = await data_int.getUser(username);
if (user.password !== password) {
throw errors.INVALID_PARAM(`wrong password for '${username}'`);
}
user.token = await data_int.usernameToToken(username);
return user;
}
async function getUser(username) {
const user = await data_int.getUser(username);
delete user.password;
return user;
}
async function getGroupDetails(username, groupId) {
const group = await data_int.loadGroup(username, groupId);
const games = group.gameIds.map(async (gameId) => {
const game = await data_ext.getGameById(gameId);
return game.name;
});
delete group.gameIds;
return Promise.all(games)
.catch((err) => {
throw err;
})
.then((res) => {
group.games = res;
return group;
});
}
async function editGroup(username, groupId, newName, newDescription) {
await data_int.editGroup(username, groupId, newName, newDescription);
return { groupId };
}
return {
getUser,
getMostPopularGames,
searchGame,
getGameById,
getGameDetails,
createUser: data_int.createUser,
getUsername,
getToken: data_int.usernameToToken,
checkAndGetUser,
createGroup: data_int.createGroup,
deleteGroup: data_int.deleteGroup,
listAllGroups: data_int.listAllGroups,
loadGroup: data_int.loadGroup,
getGroupDetails,
editGroup,
addGame: data_int.addGame,
removeGame: data_int.removeGame,
};
};