-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
148 lines (131 loc) · 4.21 KB
/
app.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
require('dotenv').config();
const express = require('express');
const cors = require('cors');
const AWS = require('aws-sdk');
const { v4: uuidv4 } = require('uuid');
const config = require('./config'); // credentials
const awsConfig = new AWS.Config(config);
const dynamodb = new AWS.DynamoDB(awsConfig);
const { session, sessionOptions } = require('./config/sessionConfig')(dynamodb);
const Poll = require('./model/pollModel')(dynamodb);
const { uuidPattern } = require('./utils');
const whitelist = [
'https://interactive.yr.media',
'https://youthradio.github.io',
'http://localhost:3000',
`http://localhost:${process.env.PORT}`,
'https://radames.static.observableusercontent.com',
];
const corsOptionsDelegate = (req, callback) => {
console.log(req.header('Origin'), whitelist.includes(req.header('Origin')));
const corsOptions = {
origin: whitelist.includes(req.header('Origin')),
methods: 'GET,HEAD,PUT,PATCH,POST,DELETE',
preflightContinue: false,
optionsSuccessStatus: 204,
credentials: true,
};
return callback(null, corsOptions); // callback expects two parameters: error and options
};
const app = express();
if (!process.env.MODE === 'dev' || process.env.MODE === undefined) {
app.set('trust proxy', 1); // trust first proxy
}
app.disable('x-powered-by');
app.use(session(sessionOptions));
app.use(cors(corsOptionsDelegate));
app.use(express.urlencoded({ extended: true }));
app.use(express.json());
app.get('/', (req, res) => {
if (req.session.views) {
req.session.views++;
res.setHeader('Content-Type', 'text/html');
res.write('<p>views: ' + req.session.views + '</p>');
res.write('<p>expires in: ' + req.session.cookie.maxAge / 1000 + 's</p>');
res.end();
} else {
req.session.views = 1;
res.end('welcome to the session demo. refresh!');
}
});
app.get('/getAll', async (req, res, next) => {
try {
const poll = await Poll.scan().exec();
res.header('Content-Type', 'application/json');
res.send(JSON.stringify(poll, null, 4));
} catch (err) {
res.statusCode = 500;
res.err({ msg: new Error('error creating POLL'), err: err });
}
});
app.post('/create', async (req, res, next) => {
const bodyData = req.body;
if (!bodyData) {
res.status(500);
return res.json({ err: true, msg: 'NO DATA' });
}
if (!bodyData.token || bodyData.token !== process.env.TOKEN) {
res.status(500);
return res.json({ err: true, msg: 'Invalid Token' });
}
if (
!bodyData.title ||
!bodyData.question ||
!bodyData.options ||
!bodyData.options.every((e) => e !== '' || e !== undefined)
) {
res.status(500);
return res.json({ err: true, msg: 'Invalid Poll Data' });
}
try {
const poll = new Poll({
poll_id: uuidv4(),
title: bodyData.title,
main_question: bodyData.question,
options: bodyData.options.map((option) => ({
id: uuidv4(),
count: 0,
text: option.text,
})),
});
await poll.save();
res.json(poll);
} catch (err) {
res.statusCode = 500;
res.err({ msg: new Error('error creating POLL'), err: err });
}
});
app.get('/vote_poll/:id/:vote', async (req, res, next) => {
const poll_id = req.params.id;
const vote_id = req.params.vote;
if (!uuidPattern.test(poll_id) && !uuidPattern.test(vote_id)) {
res.status(500);
res.json({ err: true, msg: 'invalid ids' });
}
const selectedPoll = await Poll.get({
poll_id: poll_id,
});
// check if user has already voted
if (req.session.voted_ids && req.session.voted_ids.includes(vote_id)) {
res.header('Content-Type', 'application/json');
res.send(JSON.stringify({ hasvoted: true, poll: selectedPoll }, null, 4));
return;
} else {
req.session.voted_ids = [];
req.session.voted_ids.push(vote_id);
}
const options = selectedPoll.options.map((option) => {
// if voted poll then add 1 otherwise pass
const newcount = option.id === vote_id ? option.count + 1 : option.count;
return Object.assign(option, { count: newcount });
});
await Poll.update(
{
poll_id: poll_id,
},
{ options: options }
);
res.header('Content-Type', 'application/json');
res.send(JSON.stringify({ hasvoted: false, poll: selectedPoll }, null, 4));
});
module.exports = app;