-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpoll-control.js
89 lines (71 loc) · 2.92 KB
/
poll-control.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
var POLL_MASTER_KEY = process.env.POLL_MASTER_KEY;
if ( !POLL_MASTER_KEY) {
throw new Error("No poll master key defined");
}
console.log("Will use poll master key: "+POLL_MASTER_KEY);
var POLL_KEY_HEADER = "Poll-Key";
var pollsApi = require('./polls-api');
var slackApi = require('./slack-api');
exports.isPollMasterKeyCorrect = function(request) {
return checkIsPollMasterKeyCorrect(request);
};
exports.getQuestionForNumber = function (request, response) {
var questionNumber = request.query.number;
if ( questionNumber ) {
var questionInformation = pollsApi.getQuestionForNumber(request, questionNumber);
if ( questionInformation.error ) {
return response.status(422).json({error: questionInformation.error});
}
return response.status(200).json(questionInformation);
} else {
return response.status(422).json({"error": "You must provide a question number"});
}
};
exports.startPoll = function (request, response) {
var pollStarted = pollsApi.startPoll(request);
if ( pollStarted ) {
slackApi.pollHasStarted();
}
response.status(200).json( { isStarted: pollStarted } );
};
exports.stopPoll = function (request, response) {
var stopResponse = pollsApi.stopPoll(request);
if ( stopResponse.error ) {
return response.status(422).json(stopResponse);
}
var isStoppedStatus = stopResponse.isStopped;
if ( isStoppedStatus ) {
slackApi.pollHasStopped();
}
return response.status(200).json( {isStopped: isStoppedStatus} );
};
exports.sendQuestionToSlack = function (request, response) {
var questionNumber = request.body.number;
if ( questionNumber ) {
var fullQuestion = pollsApi.getQuestionForNumber(request, questionNumber);
if ( fullQuestion ) {
console.log("Sending poll question ["+questionNumber+"] to all slack users");
slackApi.sendNewQuestionToSlackUsers(fullQuestion);
return response.status(200).json( { message: "success" } );
}
return response.status(403).json( { error: "Could not find question ["+questionNumber+"]"} );
}
return response.status(403).json( { error: "No question number provided"} );
};
exports.getStatisticsForQuestion = function (request, response) {
if ( request.nevex.isPollMaster ) {
var questionNumber = request.query.number;
if ( questionNumber) {
var stats = pollsApi.getStatisticsForQuestion(questionNumber);
return response.status(200).json(stats);
} else {
return response.status(422).json( {"error": "You must provide a poll question number"} );
}
} else {
return response.status(403).json({"error": "You are not authorized to view the statistics"});
}
};
function checkIsPollMasterKeyCorrect(request) {
var pollMasterKey = request.get(POLL_KEY_HEADER);
return pollMasterKey && POLL_MASTER_KEY === pollMasterKey;
}