-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
277 lines (260 loc) · 9.29 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
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
272
273
274
275
276
277
"use strict";
const express = require("express");
const multer = require("multer");
const request = require("request");
const {Pool} = require('pg');
require('dotenv').config();
const app = express();
app.use(express.urlencoded({extended: true}));
app.use(express.json());
app.use(multer().none());
const INVALID_PARAM_ERROR = 400;
const SERVER_ERROR = 500;
const SERVER_ERROR_MSG = "Something went wrong on the server, please try again later.";
const pool = new Pool({
ssl: {
rejectUnauthorized: false
},
connectionString: process.env.DATABASE_URL
});
// month is incremented from 0, date is incremented from 1
/*
* Retrieves the sessions available on the requested date from database
*/
app.get("/session", async function(req, res) {
try {
if (req.query.date) {
let response = await getAllSessions(req.query.date);
if (response.length === 0) {
res.status(INVALID_PARAM_ERROR).json({"error": "No available sessions for today"});
} else {
res.json({"sessions": response});
}
} else {
res.status(INVALID_PARAM_ERROR).json({"error": "Missing date parameters"});
}
} catch (error) {
res.status(SERVER_ERROR).json({"error": SERVER_ERROR_MSG});
}
});
/*
* Schedules the selected meeting by adding the user's name and email to the database
*/
app.post("/schedule", async function(req, res) {
try {
let sessions = req.body.sessions;
let sessionsArray = sessions.split(",");
let tutorId = await scheduleSession(req.body.name, req.body.email, sessionsArray);
res.json({"date": sessionsArray[0], "id": tutorId, "email": req.body.email});
} catch (error) {
console.log(error);
res.status(SERVER_ERROR).json({"error": SERVER_ERROR_MSG});
}
});
/*
* Uses the passed in zoom authorization code to get a zoom access token
* Store the access token, refresh token and expiration time in database
*/
app.get("/authorizationCode", async function(req, res) {
try {
if (req.query.authorizationCode) {
let options = {
method: 'POST',
headers: {
"Authorization": "Basic " + process.env.ZOOM_CREDENTIALS
},
url: 'https://zoom.us/oauth/token?grant_type=authorization_code&redirect_uri=https://blooming-wildwood-89768.herokuapp.com/authorization.html&code=' + req.query.authorizationCode
};
await request(options, async function(error, response, body) {
if (error) {
console.log(error);
throw new Error(error);
}
let resp = JSON.parse(body);
let date = new Date();
let expirationDate = date.getTime() + 3500000;
console.log(expirationDate);
console.log(resp.access_token);
console.log(resp.refresh_token);
if (resp.access_token !== undefined && resp.refresh_token !== undefined) {
await storeAccessToken(resp.access_token, resp.refresh_token, expirationDate);
} else {
throw new Error("Invalid access or refresh tokens returned");
}
res.json({"date": expirationDate});
});
}
} catch (error) {
console.log(error);
res.status(SERVER_ERROR).json({"error": SERVER_ERROR_MSG});
}
});
/*
* Uses the refresh token to request a new access token and refresh token
* Stores the new access token, new refresh token and new expiration date in database
*/
app.get("/refreshToken", async function(req, res) {
try {
// let refreshToken = await query("SELECT refresh_token FROM zoom_oauth;");
let refreshToken = await pgQuery("SELECT refresh_token, expiration_date FROM zoom_oauth;", []);
let date = new Date();
if (refreshToken[0].expiration_date <= date.getTime()) {
let options = {
method: "Post",
headers: {
"Authorization": "Basic " + process.env.ZOOM_CREDENTIALS
},
url: "https://zoom.us/oauth/token?grant_type=refresh_token&refresh_token=" + refreshToken[0].refresh_token
};
await request(options, async function(error, response, body) {
if (error) {
console.log(error);
throw new Error(error);
}
let resp = JSON.parse(body);
let expirationDate = date.getTime() + 3500000;
if (resp.access_token !== undefined && resp.refresh_token !== undefined) {
await storeAccessToken(resp.access_token, resp.refresh_token, expirationDate);
res.json(body);
} else {
throw new Error("Invalid access or refresh tokens returned");
}
});
} else {
res.json({"status": "not expired"});
}
} catch (error) {
console.log(error);
res.status(SERVER_ERROR).json({"error": SERVER_ERROR_MSG});
}
});
// makes sure access token isn't expired.
/*
* Uses the zoom access token to request the creation of a new zoom meeting with the
* scheduled session date and time
*/
app.get("/createMeeting", async function(req, res) {
try {
if (req.query.date) {
// let accessToken = await query("SELECT access_token FROM zoom_oauth;");
let accessToken = await pgQuery("SELECT access_token FROM zoom_oauth;", []);
let options = {
method: 'POST',
url: 'https://api.zoom.us/v2/users/jzwu12@gmail.com/meetings',
headers: {
'content-type': 'application/json',
"authorization": 'Bearer ' + accessToken[0].access_token
},
body: {
topic: 'Tutoring Test meeting',
type: 2,
start_time: req.query.date,
duration: 30,
schedule_for: 'jzwu12@gmail.com',
timezone: 'America/Los_Angeles',
password: '',
agenda: 'Math tutoring with James',
settings: {
host_video: false,
participant_video: false,
cn_meeting: false,
in_meeting: false,
join_before_host: true,
mute_upon_entry: true,
watermark: false,
use_pmi: false,
approval_type: 2,
audio: 'both',
auto_recording: 'none',
enforce_login: false,
enforce_login_domains: '',
alternative_hosts: '',
global_dial_in_countries: [],
registrants_email_notification: false
}
},
json: true
};
await request(options, function(error, response, body) {
if (error) {
throw new Error(error);
}
storeJoinURL(body.join_url, req.query.date, req.query.id);
res.json(body);
});
} else {
res.status(INVALID_PARAM_ERROR).json({"error": "Missing date parameter"});
}
} catch (error) {
console.log(error);
res.status(SERVER_ERROR).json({"error": SERVER_ERROR_MSG});
}
});
// ----------------------- SESSION HELPER FUNCTIONS -----------------------
/**
* @param {number} date Date of the month
* Returns all the sessions that are available (student for the session is null) on the given date
*/
async function getAllSessions(date) {
let qry = "SELECT t.name, s.date FROM tutors t, sessions s WHERE SUBSTR(s.date, 0, POSITION(' ' IN s.date)) = $1 AND t.id = s.tutor_id AND s.student IS NULL;";
let sessions = await pgQuery(qry, [date]);
return sessions;
}
/**
* Updates scheduled session in database to include student name and email
* @param {String} name Name of user
* @param {String} email User's email
* @param {String[]} sessions Array of properly formatted dates to represent the scheduled sessions
*/
async function scheduleSession(name, email, sessions) {
let qry = "UPDATE sessions SET student = $1, email = $2 WHERE date = $3;";
await pgQuery(qry, [name, email, sessions[0]]);
let qryTutor = "SELECT tutor_id FROM sessions WHERE date = $1";
let tutorId = await pgQuery(qryTutor, [sessions[0]]);
return tutorId[0].tutor_id;
// for (let i = 0; i < sessions.length; i++) {
// let params = [name, email, sessions[i]];
// await pgQuery(qry, params);
// }
}
// ----------------------- OAUTH FUNCTIONS --------------------------
/**
* Stores the access token, refresh token and expiration date in the database
* @param {String} access zoom access token
* @param {String} refresh zoom refresh token
* @param {integer} expiration access token expiration date (3500 sec from generation)
*/
function storeAccessToken(access, refresh, expiration) {
let qry = "UPDATE zoom_oauth SET access_token = $1, refresh_token = $2, expiration_date = $3;";
pgQuery(qry, [access, refresh, expiration]);
}
/**
* Stores the Zoom meeting url in the database next to the session
* @param {String} url Zoom meeting join link
* @param {String} date Date of the session
* @param {integer} tutor id of the tutor teaching the session
*/
async function storeJoinURL(url, date, tutor) {
let qry = "UPDATE sessions SET join_url = $1 WHERE date = $2 AND tutor_id = $3";
await pgQuery(qry, [url, date.replace("T", " "), tutor]);
}
// ----------------------- SQL QUERY FUNCTIONS -----------------------
/**
* Executes the given query with the given parameters
* @param {String} qry SQL syntax to be executed
* @param {String[]} param Array of parameters for the query
*/
async function pgQuery(qry, param) {
let client = await pool.connect();
try {
let res = await client.query(qry, param);
return res.rows;
} catch (error) {
console.log(error);
} finally {
client.release();
}
}
app.use(express.static("public"));
const PORT = process.env.PORT || 8000;
app.listen(PORT);