-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
192 lines (165 loc) · 5.69 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
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
require('dotenv').config();
const utils = require('./backend/utils');
const express = require('express');
const path = require('path');
const cors = require('cors');
const app = express();
app.use(express.json());
app.use(cors());
// Connect to PostgreSQL DB
const { Client } = require('pg');
const { nextTick } = require('process');
const client = new Client({
connectionString: process.env.DATABASE_URL,
ssl: {
rejectUnauthorized: false,
},
});
client.connect();
// Serve static files from the React app
app.use(express.static(path.join(__dirname, 'frontend/build')));
// Create meeting and send back meetCode
app.post("/api/makeMeeting", (req, res) => {
const defaultDate = '2021-02-20 00:00:00-00';
const defaultTime = ' 00:00:00-00';
const meetingID = utils.generateID();
let { meetingName,
startDate,
endDate,
startTime,
endTime } = req.body;
// come back to make a more elegant solution
startDate = startDate === '' ? defaultDate : startDate + defaultTime;
endDate = endDate === '' ? defaultDate : endDate + defaultTime;
startTime = startTime === '' ? defaultTime : startTime;
endTime = endTime === '' ? defaultTime : endTime;
const password = utils.generatePassword();
const query =
`insert into meetings (meetingID, meetingName, password, starttimestamp, endtimestamp, earliesttime, latesttime
) values ($1, $2, $3, $4, $5, $6, $7);`;
client.query(query, [meetingID, meetingName, password, startDate, endDate, startTime, endTime], (err, res) => {
if (err) throw err;
});
console.log('Posted new meeting to db.');
res.send({
meetingID: meetingID,
password: password,
});
console.log('Sent back meeting url.');
});
app.get('/api/meeting/:meetingID', (req, res) => {
const { meetingID } = req.params;
// Query database for meeting name
const query = 'select meetingname, starttimestamp, endtimestamp, earliesttime, latesttime from meetings where meetingID = $1';
client.query(query, [meetingID], (err, resq) => {
if (err) throw err;
let response = {
success: resq.rows.length > 0
};
if (response.success) {
const { meetingname, starttimestamp, endtimestamp, earliesttime, latesttime } = resq.rows[0];
response = {
...response,
meetingName: meetingname,
startDate: starttimestamp,
endDate: endtimestamp,
startTime: earliesttime,
endTime: latesttime,
};
}
res.send(response);
});
});
app.get('/api/meetingLogin', (req, res) => {
const { meetingID, password } = req.query;
// Query database for meeting password
const query = 'select password from meetings where meetingID = $1';
client.query(query, [meetingID], (err, resq) => {
if (err) throw err;
console.log(resq.rows[0]);
res.send({
correct: password === resq.rows[0].password,
});
});
});
// Placeholder for actual meeting data endpoint
app.get('/api/meeting', (req, res) => {
const { meetingID, password } = req.query;
// Query database for meeting data
const query = `select m.meetingname, m.starttimestamp, m.endtimestamp, m.earliesttime, m.latesttime, u.userid, u.username, t.starttime, t.endtime
from meetings m
inner join users u
on u.meetingid = m.meetingid
inner join times t
on t.userid = u.userid
where m.meetingid = $1 and m.password = $2;`;
client.query(query, [meetingID, password], (err, resq) => {
if (err) throw err;
console.log(resq.rows);
const rows = [];
resq.rows.forEach((row) => {
const { starttime, endtime } = row;
rows.push({
start: starttime,
end: endtime
});
});
const response = {
timeInputs: rows
}
res.send(response);
});
});
// Create meeting and send back meetCode
app.post("/api/userInput/:meetingID", (req, res, next) => {
const userID = utils.generateID();
const { meetingID } = req.params;
const { password, userName, times } = req.body;
// flow
// if it's calling this function, it's making a new user - on frontend, create different api for updating, and different endpoint
// create a new user
// input their times
const response = {
createUserMessage: 'Did not create user.',
inputTimesMessage: 'Did not input times.'
};
const userQuery =
`insert into users (userid, username, meetingid) values ($1, $2, $3);`;
client.query(userQuery, [ userID, userName, meetingID ], (err, resq) => {
if (err) throw err;
response.createUserMessage = 'Created user' + userName;
});
if (times.length === 0) {
res.send({
message: response
});
return next();
}
const queryValues = [];
const timesList = [];
for (let i = 0; i < times.length; i++) {
const time = times[i];
timesList.push(time.start);
timesList.push(time.end);
const values = `($1, $` + (i * 2 + 2).toString() + `, $` + (i * 2 + 3).toString() + `)`;
queryValues.push(values);
}
const timesQuery =
`insert into times (userid, starttime, endtime) values ` + queryValues.join(', ') + `;`; // notation is injection safe because it's not inserting directly
console.log('timesQuery: ' + timesQuery);
client.query(timesQuery, [ userID, ...timesList ], (err, resq) => {
if (err) throw err;
response.inputTimesMessage = 'Added user times.';
res.send({
message: response // for some reason it only works if you send back data in the key 'message'
});
});
});
// The "catchall" handler: for any request that doesn't
// match one above, send back React's index.html file.
app.get('*', (req, res) => {
res.sendFile(path.join(__dirname, 'frontend/build/index.html'));
});
const port = process.env.PORT || 5000 || '0.0.0.0';
app.listen(port);
console.log(`When-2-Not-Meet listening on ${port}`);