-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
110 lines (101 loc) · 4.03 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
var express = require('express');
var app = express();
var path = require('path');
var axios = require('axios')
const cors = require('cors')
const bodyParser = require('body-parser')
app.use(bodyParser.json())
app.use(cors())
var questionnaireArray = []
questionnaireArray.push({ question: "Do you prefer soda or beer?",
answers: [{name: "Beer", value: 11}, {name: "Soda", value: 42}]
})
questionnaireArray.push({ question: "When are you planning to arrive",
answers: [{name: "Early", value: 5}, {name: "A bit late", value: 7}]
})
questionnaireArray.push({ question: "We are choosing CDs for tonights karaoke. Which are you more into?",
answers: [{name: "Rock", value: 30}, {name: "Dance", value: 4}]
})
questionnaireArray.push({ question: "The sauna and hot tub are heating up. Did you remember to bring your towel?",
answers: [{name: "Yes", value: 1}, {name: "No", value: 1}, {name: "Maybe", value: 1}]
})
// viewed at http://localhost:8080
app.get('/', function(req, res) {
res.send("ayylmao")
});
app.post('/question', function(req, res) {
console.log("form post called")
var tempAnswers = []
console.log("req", req)
console.log("req.body:", req.body)
req.body.options.forEach(option => {
tempAnswers.push({name: option, value: 0})
})
questionnaireArray.push({question: req.body.message, answers: tempAnswers})
axios({
method:'put',
url:'https://rr0c0nebe8.execute-api.eu-west-1.amazonaws.com/dev/webhook',
responseType:'json',
contentType: 'application/json',
data: { message: req.body.message,
options: req.body.options
}
})
.then(function(response) {
console.log(response.data)
res.send(200)
});
})
app.post('/results', function(req, res) {
var {message, option} = req.body
var tempArray = questionnaireArray.map(questionnaire => {
if (questionnaire.question === message) {
questionnaire.answers = questionnaire.answers.map(answer => {
if (answer.name === option) {
answer.value = answer.value + 1
}
return answer
})
}
return questionnaire
})
console.log(JSON.stringify(tempArray))
questionnaireArray = tempArray
res.send(200)
})
app.get('/results', function(req, res) {
console.log(questionnaireArray)
res.send(JSON.stringify([...questionnaireArray]))
})
/*
function caVotingAssistant() {
var question = questionnaireArray[questionnaireArray.length-1]
var randomIndex = parseInt(Math.random()*(question.answers.length-1))
console.log(randomIndex)
console.log(question.answers[randomIndex])
question.answers[randomIndex].value = question.answers[randomIndex].value + 1
}
var doStuff = function() {
caVotingAssistant()
setTimeout(doStuff, 1500)
}
doStuff()
*/
app.get('/clearstate', function(req, res) {
var questionnaireArray = []
questionnaireArray.push({ question: "Do you prefer soda or beer?",
answers: [{name: "Beer", value: 11}, {name: "Soda", value: 42}]
})
questionnaireArray.push({ question: "When are you planning to arrive",
answers: [{name: "Early", value: 5}, {name: "A bit late", value: 7}]
})
questionnaireArray.push({ question: "We are choosing CDs for tonights karaoke. Which are you more into?",
answers: [{name: "Rock", value: 30}, {name: "Dance", value: 4}]
})
questionnaireArray.push({ question: "The sauna and hot tub are heating up. Did you remember to bring your towel?",
answers: [{name: "Yes", value: 1}, {name: "No", value: 1}, {name: "Maybe", value: 1}]
})
res.send(200)
})
var port = process.env.PORT || 8080
app.listen(port);