-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapp.js
453 lines (411 loc) · 17.8 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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
require('dotenv').config();
var bodyParser = require('body-parser');
const express = require('express');
const app = express();
const Student = require('./models/student');
const mongoose = require('mongoose');
const connectDb = () => {
return mongoose.connect(process.env.DB_URL, {
useNewUrlParser: true,
useUnifiedTopology: true,
useCreateIndex: true,
});
};
const fs = require('fs');
const readline = require('readline');
const { google } = require('googleapis');
const { GoogleAuth } = require('google-auth-library');
const discord = require('./discord/discord_functions');
const ObjectId = mongoose.Types.ObjectId;
mongoose.set('useFindAndModify', false);
const convertNameToID = require('./discord/functions/convertNameToID');
// ------------START GOOGLE SHEETS API CODE--------------
// If modifying these scopes, delete token.json.
const SCOPES = ['https://www.googleapis.com/auth/spreadsheets.readonly'];
// The file token.json stores the user's access and refresh tokens, and is
// created automatically when the authorization flow completes for the first
// time.
const TOKEN_PATH = 'token.json';
// parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: false }));
// parse application/json
app.use(bodyParser.json());
/**
* Create an OAuth2 client with the given credentials, and then execute the
* given callback function.
* @param {Object} credentials The authorization client credentials.
* @param {function} callback The callback to call with the authorized client.
*/
function authorize(credentials, callback) {
const { client_secret, client_id, redirect_uris } = credentials.installed;
const oAuth2Client = new google.auth.OAuth2(
client_id,
client_secret,
redirect_uris[0]
);
// Check if we have previously stored a token.
fs.readFile(TOKEN_PATH, (err, token) => {
if (err) return getNewToken(oAuth2Client, callback);
oAuth2Client.setCredentials(JSON.parse(token));
callback(oAuth2Client);
});
}
/**
* Get and store new token after prompting for user authorization, and then
* execute the given callback with the authorized OAuth2 client.
* @param {google.auth.OAuth2} oAuth2Client The OAuth2 client to get token for.
* @param {getEventsCallback} callback The callback for the authorized client.
*/
function getNewToken(oAuth2Client, callback) {
const authUrl = oAuth2Client.generateAuthUrl({
access_type: 'offline',
scope: SCOPES,
});
console.log('Authorize this app by visiting this url:', authUrl);
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
rl.question('Enter the code from that page here: ', (code) => {
rl.close();
oAuth2Client.getToken(code, (err, token) => {
if (err)
return console.error(
'Error while trying to retrieve access token',
err
);
oAuth2Client.setCredentials(token);
// Store the token to disk for later program executions
fs.writeFile(TOKEN_PATH, JSON.stringify(token), (err) => {
if (err) return console.error(err);
console.log('Token stored to', TOKEN_PATH);
});
callback(oAuth2Client);
});
});
}
async function getAuthToken() {
const auth = new google.auth.GoogleAuth({
scopes: SCOPES,
});
const authToken = await auth.getClient();
return authToken;
}
// ------------END GOOGLE SHEETS API CODE--------------
// Changed the structure of this to the
// new user_schema you see below for efficiency
// and to prevent magic numbers by parsing
// through it like an array
/** const user_schema = {
0 : { name: "email", type: "string", isArray: false },
1 : { name: "discord", type: "string", isArray: false },
2 : { name: "name", type: "string", isArray: false },
3 : { name: "region", type: "integer", isArray: false }, //3 options
4 : { name: "location", type: "string", isArray: false },
5 : { name: "pronouns", type: "string", isArray: false }, //bullet option with other ______
6 : { name: "introduction", type: "string", isArray: false },
7 : { name: "five_years", type: "string", isArray: false },
8 : { name: "goals", type: "string", isArray: true },
9 : { name: "track", type: "integer", isArray: false },
10: { name: "year_school", type: "string", isArray: false }, //bullet option with other ______
11: { name: "lang_prefs", type: "string", isArray: true },
12: { name: "interest_skills", type: "string", isArray: true }, // ??? maybe
13: { name: "lang_importance", type: "integer", isArray: false },
14: { name: "lang_prefenence", type: "string", isArray: false }, // only if 8 or higher on q13
15: { name: "proj_ideas", type: "string", isArray: false },
16: { name: "team_lead", type: "integer", isArray: false },
17: { name: "hours_per_week", type: "integer", isArray: false }, // hours/week
18: { name: "commitment_lev", type: "integer", isArray: false },
19: { name: "commitment_exp", type: "string", isArray: false },
20: { name: "meet_per_week", type: "string", isArray: false }, //bullet option with other ______
21: { name: "curr_responsible", type: "string", isArray: false },
22: { name: "start_date", type: "Date", isArray: false },
23: { name: "end_date", type: "Date", isArray: false },
24: { name: "bring_to_team", type: "string", isArray: false },
25: { name: "professional_link",type: "string", isArray: false },
26: { name: "demographics", type: "string", isArray: false },
27: { name: "partner_prefs", type: "string", isArray: false },
28: { name: "commit_agreement", type: "boolean", isArray: false },
29: { name: "team_agreement", type: "string", isArray: false }, //bullet option with other ______
30: { name: "rules_agreement", type: "boolean", isArray: false },
31: { name: "tips", type: "string", isArray: false },
32: { name: "discord_id", type: "string", isArray: false },
33: { name: "team_name", type: "string", isArray: false }
}; **/
const user_schema = {
data: [
{ name: 'email', type: 'string', isArray: false },
{ name: 'discord', type: 'string', isArray: false },
{ name: 'name', type: 'string', isArray: false },
{ name: 'region', type: 'string', isArray: false }, //3 options
{ name: 'location', type: 'string', isArray: false },
{ name: 'pronouns', type: 'string', isArray: false }, //bullet option with other ______
{ name: 'introduction', type: 'string', isArray: false },
{ name: 'five_years', type: 'string', isArray: false },
{ name: 'goals', type: 'string', isArray: true },
{ name: 'track', type: 'integer', isArray: false },
{ name: 'year_school', type: 'string', isArray: false }, //bullet option with other ______
{ name: 'lang_prefs', type: 'string', isArray: true },
{ name: 'interest_skills', type: 'string', isArray: true }, // ??? maybe
{ name: 'lang_importance', type: 'integer', isArray: false },
{ name: 'lang_prefenence', type: 'string', isArray: false }, // only if 8 or higher on q13
{ name: 'proj_ideas', type: 'string', isArray: false },
{ name: 'team_lead', type: 'integer', isArray: false },
{ name: 'hours_per_week', type: 'integer', isArray: false }, // hours/week
{ name: 'commitment_lev', type: 'integer', isArray: false },
{ name: 'commitment_exp', type: 'string', isArray: false },
{ name: 'meet_per_week', type: 'string', isArray: false }, //bullet option with other ______
{ name: 'curr_responsible', type: 'string', isArray: false },
{ name: 'start_date', type: 'Date', isArray: false },
{ name: 'end_date', type: 'Date', isArray: false },
{ name: 'bring_to_team', type: 'string', isArray: false },
{ name: 'professional_link', type: 'string', isArray: false },
{ name: 'demographics', type: 'string', isArray: false },
{ name: 'partner_prefs', type: 'string', isArray: false },
{ name: 'commit_agreement', type: 'boolean', isArray: false },
{ name: 'team_agreement', type: 'string', isArray: false }, //bullet option with other ______
{ name: 'rules_agreement', type: 'boolean', isArray: false },
{ name: 'tips', type: 'string', isArray: false },
{ name: 'discord_id', type: 'string', isArray: false },
{ name: 'team_name', type: 'string', isArray: false },
],
};
// Enables cors support
const cors = require('cors');
app.use(cors());
app.use('/discord', discord);
app.get('/', function (req, res) {
res.send('Hello world');
});
app.get('/test', function (req, res) {
Student.find({}, function (err, students) {
if (err) return console.error(err);
res.send(students);
});
});
app.get('/addAllUsers', function (req, res) {
// Authorize a client with credentials, then call the Google Sheets API.
// fs.readFile('credentials.json', (err, content) => {
// if (err) res.send('Error loading client secret file:', err);
// authorize(JSON.parse(content), getAllResponses);
// });
// Get all data, including headers, from sheet
function getAllResponses(auth) {
const sheets = google.sheets({ version: 'v4', auth });
sheets.spreadsheets.values.get(
{
spreadsheetId: '1s_YEDpl9fBMEhYTdMx9xA3CNzsYY1F68mzlCIzX1leU',
range: 'Form Responses 9!$A$1:$YY',
},
async (err, res) => {
if (err) res.send('The API returned an error: ' + err);
// allData is all the spreadsheet data
// returned in a multidimensional array!
allData = res.data.values;
let headers = [];
let jsoon = {};
// Populate the "jsoon" with empty values (or arrays)
for (let i = 0; i < user_schema['data'].length; i++) {
let field = user_schema['data'][i].name;
if (user_schema['data'][i].isArray) {
jsoon[field] = [];
} else {
jsoon[field] = '';
}
}
// Read the first row of data and store all headers, will extract question numbers from these later
for (let i = 0; i < allData[0].length; i++) {
headers.push(allData[0][i]);
}
// Parse allData and build the JSON (named "jsoon")
// with student info that we will send to Mongo
for (let r = 1; r < allData.length; r++) {
for (let c = 2; c < allData[r].length; c++) {
// The timestamp and email do not have a number like "1)" before them
// This is why we manually add both to the final "jsoon"
jsoon['timestamp'] = allData[r][0];
jsoon['email'] = allData[r][1];
jsoon['team_name'] = '';
// Get the name and then call convert name to id so then we can convert name into id
const uncleanID = await convertNameToID(allData[r][2]);
// Remove the "ID: " to clean the id
if (uncleanID.title === 'Success') {
// If the name was successfully converted
jsoon['discord_id'] = uncleanID.message.replace('ID: ', '');
} else {
// If not then set it an empty string.
jsoon['discord_id'] = '';
}
// Pick header that corresponds to each column
// Split header into text and question number
// then parse question number as int
// Use this int to find corresponding
// data in user_schema!
let header = headers[c];
let index = header.split(')')[0];
index = parseInt(index);
if (user_schema['data'][index].isArray) {
// For any field that is designated as an array
// push all data that corresponds to that field
// into that fields value in the "jsoon" (which is an array)
let field = user_schema['data'][index].name;
jsoon[field].push(allData[r][c]);
} else {
// Find the correct field name from the user_schema according to question number
// Populate the "jsoon" that we will be using as a payload to push to Mongo!
let field = user_schema['data'][index].name;
jsoon[field] = allData[r][c];
}
}
// This is where the call to Mongo will happen,
// right now it is a simple log so you can see
// the jsoon (JSON) full of data that will be pushed to Mongo
Student.create(jsoon)
.then(function () {
console.log('Data inserted'); // Success
})
.catch(function (error) {
console.log(error); // Failure
});
// Clear all the arrays in the "jsoon"
// for the next set of data
for (let i = 0; i < user_schema['data'].length; i++) {
if (user_schema['data'][i].isArray) {
jsoon[user_schema['data'][i].name] = [];
}
}
}
}
);
res.send('All data inserted');
}
try {
getAuthToken().then((auth) => {
getAllResponses(auth);
});
} catch (error) {
console.log(error.message, error.stack);
}
});
app.get('/clear', function (req, res) {
Student.deleteMany({}).then(() => {
res.send("Cleared");
})
})
app.get('/addLatestUser', function (req, res) {
// Authorize a client with credentials, then call the Google Sheets API.
// fs.readFile('credentials.json', (err, content) => {
// if (err) {console.log("creds err"); res.send('Error loading client secret file: ', err);}
// authorize(JSON.parse(content), getAllResponses);
// });
// Get all data, including headers, from sheet
function getLatestResponse(auth) {
const sheets = google.sheets({ version: 'v4', auth });
sheets.spreadsheets.values.get(
{
spreadsheetId: '1s_YEDpl9fBMEhYTdMx9xA3CNzsYY1F68mzlCIzX1leU',
range: 'Form Responses 9!$A$1:$YY',
},
async (err, res) => {
if (err) {
console.log('sheets err');
res.send('The Sheets API returned an error: ' + err);
}
// allData is all the spreadsheet data
// returned in a multidimensional array!
allData = res.data.values;
console.log('Data: ' + allData);
let headers = [];
let jsoon = {};
// Populate the "jsoon" with empty values (or arrays)
for (let i = 0; i < user_schema['data'].length; i++) {
let field = user_schema['data'][i].name;
if (user_schema['data'][i].isArray) {
jsoon[field] = [];
} else {
jsoon[field] = '';
}
}
// Read the first row of data and store all headers, will extract question numbers from these later
for (let i = 0; i < allData[0].length; i++) {
headers.push(allData[0][i]);
}
// Parse allData and build the JSON (named "jsoon")
// with student info that we will send to Mongo
r = allData.length - 1;
for (let c = 2; c < allData[r].length; c++) {
// The timestamp and email do not have a number like "1)" before them
// This is why we manually add both to the final "jsoon"
jsoon['timestamp'] = allData[r][0];
jsoon['email'] = allData[r][1];
// Get the name and then call convert name to id so then we can convert name into id
const uncleanID = await convertNameToID(allData[r][2]);
// Remove the "ID: " to clean the id
if (uncleanID.title === 'Success') {
// If the name was successfully converted
jsoon['discord_id'] = uncleanID.message.replace('ID: ', '');
} else {
// If not then set it an empty string.
jsoon['discord_id'] = '';
}
// Pick header that corresponds to each column
// Split header into text and question number
// then parse question number as int
// Use this int to find corresponding
// data in user_schema!
let header = headers[c];
let index = header.split(')')[0];
index = parseInt(index);
if (user_schema['data'][index].isArray) {
// For any field that is designated as an array
// push all data that corresponds to that field
// into that fields value in the "jsoon" (which is an array)
let field = user_schema['data'][index].name;
jsoon[field].push(allData[r][c]);
} else {
// Find the correct field name from the user_schema according to question number
// Populate the "jsoon" that we will be using as a payload to push to Mongo!
let field = user_schema['data'][index].name;
jsoon[field] = allData[r][c];
}
}
console.log('jsoon: ' + jsoon);
// This is where the call to Mongo will happen,
// right now it is a simple log so you can see
// the jsoon (JSON) full of data that will be pushed to Mongo
Student.create(jsoon)
.then(function () {
console.log('Data inserted'); // Success
})
.catch(function (error) {
console.log(error); // Failure
});
}
);
res.send('Latest user added');
}
try {
getAuthToken().then((auth) => {
getLatestResponse(auth);
});
} catch (error) {
console.log(error.message, error.stack);
}
});
app.post('/updateUser', async function(req, res) {
Student.findOneAndUpdate(
{_id: mongoose.Types.ObjectId(req.body._id)},
req.body,
{upsert: true, new: true, runValidators: true}, // options
function (err, updatedStudent) { // callback
if (err) console.log('ERROR updating: '+ err);
else res.json(updatedStudent)
}
)
});
connectDb().then(async () => {
app.listen(process.env.PORT, () =>
console.log(`App listening on port ${process.env.PORT}!`)
);
});
// app.listen(process.env.PORT || 8080);