-
Notifications
You must be signed in to change notification settings - Fork 2
/
common_functions.js
271 lines (234 loc) · 8.51 KB
/
common_functions.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
var fs = require('fs');
const { resolve } = require('path');
var path = require('path');
var crypto = require('crypto');
const passport = require('passport');
var _ = require('lodash');
var path = require('path');
var logger = require('./logger')
// Init logger
var log = logger.app(path.parse(__filename).name);
exports.getJsonFile = function (fileName) {
return new Promise(function (resolve, reject) {
fs.readFile(path.join(__dirname, fileName + ".json"), 'utf8', function (err, contents) {
if (err) return reject(err);
resolve(JSON.parse(contents));
});
})
}
exports.saveJson = function (fileName, data, callback) {
fs.writeFile(path.join(__dirname, fileName + ".json"), JSON.stringify(data), function (err) {
if (err) return log.error(err);
callback();
});
}
exports.getTeam = function (uuidIn, teamData, callback) {
var returnData = {};
if (typeof (uuidIn) === "undefined") {
returnData["success"] = false;
returnData["errorMessage"] = "uuid not given";
callback(returnData);
} else {
//// Check uuid ////
// Find team in data
var currentTeam = teamData.find(function (obj) {
return obj.uuid === uuidIn;
})
// Check if uuid is found
if (typeof (currentTeam) !== "undefined") {
returnData["success"] = true;
returnData["team"] = currentTeam;
} else {
returnData["success"] = false;
returnData["errorMessage"] = "uuid not found";
}
callback(returnData);
}
}
exports.getCurrentQuestion = function (knex) {
return new Promise(function (resolve, reject) {
return knex('current_question')
.where({ name: 'current' })
.first()
.then(row => { resolve(row.question) })
.catch(error => reject(error))
})
}
exports.updateCurrentQuestion = function (knex, questionUuid) {
return new Promise(function (resolve, reject) {
return knex('current_question')
.where({ name: 'current' })
.update({ question: questionUuid })
.then(resolve())
.catch(error => reject(error))
})
}
exports.hashPassword = function (password, salt) {
var hash = crypto.createHash('sha256');
hash.update(password);
hash.update(salt);
return hash.digest('hex');
}
// Middleware to check of user is logged in.
exports.isAuthed = function (req, res, next) {
if (req.isAuthenticated()) {
next();
} else {
if (req.method == "GET") {
// Send previous page with redirect
res.redirect(`/login?r=${encodeURI(req.url)}`)
} else if (req.method == "POST") {
res.send({ result: "error", errorCode: "logged_out", errorMsg: "Login to continue." })
}
}
}
exports.errorHandler = function (action, error, req = false, res = false) {
log.error(`${action}: ${error} \n ${error.stack}`)
if (req != false) {
res.send({ result: "error", errorCode: "generic", errorMsg: `Cannot ${action}` })
}
}
/*
Current order is a array with all the info about rounds and questions without the question data.
Used for question order and the control mechanism (next, previous)
*/
var currentOrder = {}
exports.updateCurrentOrder = function (knex) {
// Unpdate the question and round count.
knex('rounds')
.select("uuid", "order")
.orderBy('order', 'asc')
.then((rounds) => {
knex('questions')
.select("uuid", "round", "order")
.orderBy('order', 'asc')
.then((questions) => {
// Group by round
var grouped = _.groupBy(questions, function (obj) {
return obj.round;
});
// Add questions to rounds
for (rKey in rounds) {
if (typeof (grouped[rounds[rKey].uuid]) !== "undefined") {
// Questions
rounds[rKey].questions = grouped[rounds[rKey].uuid]
// Question count
rounds[rKey].questionCount = grouped[rounds[rKey].uuid].length
}
}
// Add round count to array
currentOrder.roundCount = rounds.length
currentOrder.rounds = rounds;
})
.catch((error) => { exports.errorHandler("Cannot get questions", error) })
})
.catch((error) => { exports.errorHandler("Cannot get rounds", error) })
}
exports.getCurrentOrder = function () {
return currentOrder
}
exports.getQuestion = function (knex, questionUuid) {
return new Promise(function (resolve, reject) {
return knex.select([
'questions.*',
'rounds.name as round_name',
'rounds.details as round_details',
'rounds.uuid as round_uuid',
'rounds.order as round_order'
])
.where({ "questions.uuid": questionUuid })
.from('questions')
.leftJoin(
'rounds',
'questions.round',
'rounds.uuid'
)
.first()
.then(row => {
if (typeof (row) != "undefined") {
// Parse parameters
row.parameters = JSON.parse(row.parameters)
// Strip answers from parameters
for (key in row.parameters) {
delete row.parameters[key].correct
}
resolve(row)
} else {
throw new Error('question does not exists')
}
})
.catch(error => reject(error))
})
}
exports.getQuestions = function (knex) {
return new Promise(function (resolve, reject) {
knex('rounds')
.orderBy('order', 'asc')
.then((rounds) => {
knex('questions')
.orderBy('order', 'asc')
.then((questions) => {
// Parse parameters
for (qKey in questions) {
try {
var parsedParameters = JSON.parse(questions[qKey].parameters)
} catch (error) {
log.error(error)
}
questions[qKey].parameters = parsedParameters
}
// Group by round
var grouped = _.groupBy(questions, function (obj) {
return obj.round;
});
// Add questions to rounds
for (rKey in rounds) {
if (typeof (grouped[rounds[rKey].uuid]) !== "undefined") {
rounds[rKey].questions = grouped[rounds[rKey].uuid]
}
}
resolve(rounds)
})
.catch(error => reject(error))
})
.catch(error => reject(error))
})
}
exports.resetCurrent = function (knex) {
var firstQuestonUuid = exports.getCurrentOrder().rounds[0].questions[0].uuid
return new Promise(function (resolve, reject) {
return exports.updateCurrentQuestion(knex, firstQuestonUuid)
.then(resolve())
.catch(error => reject(error))
})
}
exports.getSetting = function (knex, settingId) {
return new Promise(function (resolve, reject) {
return knex('settings')
.where({ id: settingId })
.first()
.then(row => {
// Convert db entry to boolean
if (row.type == "boolean") {
row.value = (row.value == "1") ? true : false
}
resolve(row)
})
.catch(error => {
log.error(`Cannot get setting: ${settingId}`)
reject(error)
})
})
}
exports.updateSetting = function (knex, settingId, value) {
return new Promise(function (resolve, reject) {
return knex('settings')
.where({ id: settingId })
.update({ value: value })
.then(resolve())
.catch(error => {
log.error(`Cannot update setting: ${settingId}`)
reject(error)
})
})
}