-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.js
817 lines (745 loc) · 27.3 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
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var formidable = require('formidable');
var fs = require('fs');
var firebase = require('firebase');
require('firebase/auth');
require('firebase/database');
// Initialize Firebase
var config = {
apiKey: 'AIzaSyCruoCX_m38WliVCubNRBPqyCjGfwA_M8k',
authDomain: 'newstock-4f49e.firebaseapp.com',
databaseURL: 'https://newstock-4f49e.firebaseio.com',
projectId: "newstock-4f49e",
storageBucket: '',
messagingSenderId: '877130652031'
};
firebase.initializeApp(config);
// firebase.auth().onAuthStateChanged(function(user) {
// if (user) {
// // User is signed in.
// var email = user.email;
// console.log("user: " + email);
// } else {
// console.log("no X user");
// }
// });
// firebase.auth().signInWithEmailAndPassword('[email protected]', 'password').catch(function(error) {
// firebase.auth().signInWithEmailAndPassword('[email protected]', 'password').catch(function(error) {
// // Handle Errors here.
// var errorCode = error.code;
// var errorMessage = error.message;
// // ...
// });
// Include each page's /routes/*.js file here
var stockPage = require('./routes/stock');
var groupsPage = require('./routes/groups');
var profilePage = require('./routes/profile');
var signupPage = require('./routes/signup');
var loginPage = require('./routes/login');
var landingPage = require('./routes/landing');
var app = express();
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'hbs');
// uncomment after placing your favicon in /public
//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
// Routing for each page
app.use('/', landingPage);
app.use('/stock', stockPage);
app.use('/groups', groupsPage);
app.use('/profile', profilePage);
app.use('/signup', signupPage);
app.use('/login', loginPage);
app.post('/sign_up_user', async function(req, res, next) {
var firstName = req.body.firstName;
var lastName = req.body.lastName;
var email = req.body.email;
var password = req.body.password;
console.log("signing up with " + email + password);
res.contentType('json');
firebase.auth().createUserWithEmailAndPassword(email, password).then(function(result) {
if (result) {
console.log(result.uid);
firebase.database().ref(`users/${result.uid}`).set({
firstName: firstName,
lastName: lastName,
email: email,
userId: result.uid,
balance: 100000.00,
bio: 'No bio yet.'
});
console.log("successs");
res.send({ success: 'Saved!' });
}
}, function(error) {
console.log('cannot sign up');
console.log(error);
res.status(500).send({ error: error });
});
});
app.post('/sign_in_user', async function(req, res, next) {
var email = req.body.email;
var password = req.body.password;
console.log("signing up with " + email + password);
res.contentType('json');
try {
const result = await firebase.auth().signInWithEmailAndPassword(email, password);
if (result) {
console.log("successs login");
res.send({ success: 'Saved!' });
}
} catch (e) {
console.log(e);
res.status(500).send({ error: e});
}
});
app.post('/get_user_info', async function(req, res, next) {
res.contentType('json');
try {
var userId = firebase.auth().currentUser.uid;
firebase.database().ref('/users/' + userId).once('value').then(function(snapshot) {
var first = snapshot.val().firstName;
var last = snapshot.val().lastName;
var bal = snapshot.val().balance;
var bio = snapshot.val().bio;
var purchases = snapshot.val().purchases;
var groups = snapshot.val().groups;
// console.log(`profile info: ${first}, ${last}, ${bal}, ${bio}, ${groups}, ${purchases}`);
res.send({
userId: userId,
name: first + ' ' + last,
balance: bal,
bio: bio,
groups: groups,
purchases: purchases
});
});
console.log('success');
} catch (e) {
console.log('fail');
console.error(e);
res.send({'name': 'Unknown', 'balance': 'Unknown'});
}
});
app.post('/get_user_purchases', async function(req, res, next) {
var userId = firebase.auth().currentUser.uid;
if (req.body.user) {
userId = req.body.user;
}
res.contentType('json');
try {
var userId = firebase.auth().currentUser.uid;
var purchaseList = []
firebase.database().ref(`/users/${userId}/purchases/`).once('value').then(function(snapshot) {
var i = 0;
snapshot.forEach(x => {
purchaseList.push({
companyCode: x.val().companyCode,
companyName: x.val().companyName,
date: x.val().date,
num_units: x.val().num_units,
share_price: x.val().share_price,
tradeAmount: x.val().tradeAmount,
type: x.val().type,
id: Object.keys(snapshot.val())[i]
});
i++;
})
res.send({'purchaseList': purchaseList});
});
console.log('success Purchases');
} catch (e) {
console.log('fail Purchases');
console.error(e);
res.send({'purchaseList': 'Unknown'});
}
});
app.post('/get_user_purchase_history', async function(req, res, next) {
var userId = firebase.auth().currentUser.uid;
if (req.body.user) {
userId = req.body.user;
}
res.contentType('json');
try {
var historyList = []
firebase.database().ref(`/users/${userId}/history/`).once('value').then(function(snapshot) {
var i = 0;
snapshot.forEach(x => {
historyList.push({
companyCode: x.val().companyCode,
companyName: x.val().companyName,
date: x.val().date,
profit_loss_dollars: x.val().profit_loss_dollars,
profit_loss_percent: x.val().profit_loss_percent,
tradeAmount: x.val().tradeAmount,
num_units: x.val().num_units,
type: x.val().type,
id: Object.keys(snapshot.val())[i],
comments: x.val().comments
});
i++;
});
res.send({'historyList': historyList});
});
console.log('success history');
} catch (e) {
console.log('fail history');
console.error(e);
res.send({'historyList': 'Unknown'});
}
});
app.post('/get_user_watchList', async function(req, res, next) {
res.contentType('json');
try {
var userId = firebase.auth().currentUser.uid;
var watchList = []
firebase.database().ref(`/users/${userId}/watchList/`).once('value').then(function(snapshot) {
snapshot.forEach(x => {
watchList.push({
companyCode: x.val().companyCode,
companyName: x.val().companyName,
// share_price: x.val().share_price
})
})
res.send({'watchList': watchList});
});
console.log('success watch');
} catch (e) {
console.log('fail watch');
console.error(e);
res.send({'watchList': 'Unknown'});
}
});
app.post('/add_To_Watch_List', async function(req, res, next) {
var companyName = req.body.companyName;
var companyCode = req.body.companyCode;
console.log(companyCode);
console.log(companyName);
res.contentType('json');
var user = firebase.auth().currentUser.uid;
console.log("current user = " + user);
try {
if (user) {
console.log(user)
let ref = firebase.database().ref(`users/${user}/watchList`);
firebase.database().ref(`/users/${user}/watchList/`).once('value').then(function(snapshot) {
var alreadyWatching = false;
snapshot.forEach(x => {
if (x.val().companyName == companyName) {
alreadyWatching = true;
}
// console.log(x.val().companyName)
})
if (!alreadyWatching) {
firebase.database().ref(`users/${user}/watchList`).push({
companyName: companyName,
companyCode: companyCode,
});
}
})
res.send({watchAdded: true});
} else {
res.send({watchAdded: false});
}
} catch (e) {
console.log('fail');
console.error(e);
res.send({watchAdded: false});
}
});
app.post('/close_trade', async function(req, res, next) {
var item = req.body;
res.contentType('json');
try {
var userId = firebase.auth().currentUser.uid;
firebase.database().ref(`users/${userId}/history`).push({
tradeAmount: item.trade_amount,
companyName: item.companyName,
companyCode: item.companyCode,
date: Date.now(),
type: item.type,
num_units: item.num_units,
profit_loss_dollars: item.profit_loss_dollars,
profit_loss_percent: item.profit_loss_percent,
comments: []
});
firebase.database().ref(`/users/${userId}/purchases/`).once('value').then(function(snapshot) {
var getPurchsaseId;
snapshot.forEach(x => {
if (item.companyCode == x.val().companyCode && x.val().num_units == item.num_units && x.val().date == item.date) {
getPurchsaseId = x.key
}
});
firebase.database().ref(`users/${userId}/purchases/${getPurchsaseId}`).remove();
});
var ref = firebase.database().ref(`users/${userId}/balance`);
ref.once('value', function(snapshot) {
if (item.type == "long") {
var i = parseFloat(item.trade_amount) + parseFloat(item.profit_loss_dollars);
} else {
var i = parseFloat(item.profit_loss_dollars);
}
var curBalance = parseFloat(snapshot.val());
var newBalance = parseFloat(curBalance + i);
var finalBalance = newBalance.toFixed(2);
firebase.database().ref(`users/${userId}`).update({balance: finalBalance});
});
res.send({'closed': true});
console.log('success');
} catch (e) {
console.log('fail');
console.error(e);
res.send({'closed': false});
}
});
app.post('/remove_from_watchlist', async function(req, res, next) {
var item = req.body;
res.contentType('json');
try {
var userId = firebase.auth().currentUser.uid;
firebase.database().ref(`/users/${userId}/watchList/`).once('value').then(function(snapshot) {
var getWatchListItemId;
snapshot.forEach(x => {
if (item.companyCode == x.val().companyCode) {
getWatchListItemId = x.key
}
});
firebase.database().ref(`users/${userId}/watchList/${getWatchListItemId}`).remove();
});
res.send({'removed': true});
console.log('success');
} catch (e) {
console.log('fail');
console.error(e);
res.send({'closed': false});
}
});
app.post('/update_bio', async function(req, res, next) {
var newBio = req.body.bio;
res.contentType('json');
try {
var userId = firebase.auth().currentUser.uid;
firebase.database().ref(`users/${userId}`).update({'bio': newBio});
res.send({'bio': true});
console.log('success');
} catch (e) {
console.log('fail');
console.error(e);
res.send({'bio': false});
}
});
app.post('/upload_image', async function(req, res, next) {
var userId = firebase.auth().currentUser.uid;
var form = new formidable.IncomingForm();
var whereto = path.join(__dirname, 'public', 'profile_images', userId + '.png');
console.log('writing to: ' + whereto);
//upload image file
form.parse(req, function(err, fields, files) {
var filepath = files.uploadfile.path;
console.log('moving: ' + filepath);
fs.rename(filepath, whereto, function(err) {
console.log(err);
});
});
//redirect to profile page on completion
res.redirect('/profile');
});
app.post('/new_group', async function(req, res, next) {
var name = req.body.name;
var date = req.body.date;
res.contentType('json');
try {
var user = firebase.auth().currentUser.uid;
console.log("current user = " + user);
var makeGroup = true;
var make = firebase.database().ref('/groups').once('value').then(function(snapshot) {
snapshot.forEach(function(childSnapshot) {
if (childSnapshot.val().name == name) {
console.log("group name exists");
makeGroup = false;
}
});
if (makeGroup === true) {
firebase.database().ref('/users/' + user).once('value').then(function(snapshot) {
var first = snapshot.val().firstName;
var last = snapshot.val().lastName;
var person = first + ' ' + last;
//create the new group
var newGroupKey = firebase.database().ref().child('groups').push().key;
var updates = {};
updates[`/groups/${newGroupKey}`] = {'name': name, 'users': [user], 'history': [{'user': person, 'joined': date, 'left': '', 'id': user, 'created': 'created'}]};
updates[`/users/${user}/groups/${newGroupKey}`] = name;
firebase.database().ref().update(updates);
res.send({'group': newGroupKey});
console.log('success');
});
} else {
res.send({'group': ''});
console.log('group already exists');
}
});
} catch (e) {
console.log('fail');
console.error(e);
res.send({'group': ''});
}
});
app.post('/invite_to_group', async function (req, res, next) {
var invite_uids = JSON.parse(req.body['invite_uids']);
var group_id = req.body['group_id'];
var now = req.body['date'];
// console.log("Inviting to group: " + group_id);
// console.log("UIDS TO BE INVITED: " + invite_uids);
try {
var updates = {};
//add new "joined" events to group history - before appending existing uids
var allHistory = [];
invite_uids.forEach(x => {
firebase.database().ref('/users/' + x).once('value').then(function(snapshot) {
var first = snapshot.val().firstName;
var last = snapshot.val().lastName;
var person = first + ' ' + last;
allHistory.push({'user': person, 'joined': now, 'left': '', 'created': 'joined', 'id': x});
});
});
// Add all existing group members to invite_uids
var existing_uids = [];
firebase.database().ref(`/groups/${group_id}/users`).once('value').then(function (snapshot) {
snapshot.forEach(x => {
if (invite_uids.indexOf(x.val()) === -1) { // If uid not already in invite_uids
existing_uids.push(x.val());
}
});
updates[`/groups/${group_id}/users`] = existing_uids.concat(invite_uids);
//add existing group history to allHistory
firebase.database().ref(`/groups/${group_id}/history`).once('value').then(function (snapshot) {
snapshot.forEach(x => {
if (allHistory.indexOf(x.val()) === -1) {
allHistory.push(x.val());
}
});
});
updates[`/groups/${group_id}/history`] = allHistory;
var group_name = "unknown";
firebase.database().ref(`/groups/${group_id}`).once('value').then(function (snapshot) {
group_name = snapshot.val().name; // Fetch group name from firebase
console.log("Group name: " + group_name);
invite_uids.forEach(function (uid) {
updates[`/users/${uid}/groups/${group_id}`] = group_name;
});
firebase.database().ref().update(updates);
res.send({'group_members': existing_uids.concat(invite_uids)});
console.log('invite success');
});
});
} catch (e) {
console.log('fail');
console.error(e);
res.send({'group': false});
}
});
app.post('/leave_group', async function (req, res, next) {
var user = firebase.auth().currentUser.uid;
var group_id = req.body.group_id;
var date = req.body.date;
var updates = {};
firebase.database().ref(`/groups/${group_id}/users`).once('value').then(function (snapshot) {
var user_difference = [];
snapshot.forEach(x => {
if (x.val() !== user) { // If uid not already in invite_uids
user_difference.push(x.val());
}
});
//get current history and add leave date/time for current user
var newHistory = [];
firebase.database().ref('/users/' + user).once('value').then(function(snapshot) {
var first = snapshot.val().firstName;
var last = snapshot.val().lastName;
var personLeaving = first + ' ' + last;
//get current user (i.e. user that's leaving) & update history of that user
firebase.database().ref(`/groups/${group_id}/history`).once('value').then(function (snapshot) {
snapshot.forEach(x => {
var updated = x.val();
if (x.val().user === personLeaving) {
updated = {'user': personLeaving, 'joined': x.val().joined, 'left': date, 'created': x.val().created};
}
if (newHistory.indexOf(updated) === -1) {
newHistory.push(updated);
}
});
updates[`/groups/${group_id}/history/`] = newHistory; //update group history
updates[`/users/${user}/groups/${group_id}`] = null; // Remove group from user entry
updates[`/groups/${group_id}/users/`] = user_difference; // Remove from group page
firebase.database().ref().update(updates);
res.send({'user_ids': user_difference});
});
});
});
});
app.post('/comment_on_feed', async function (req, res, next) {
var user = firebase.auth().currentUser.uid;
var comment = req.body.comment;
var feedItemUser = req.body.postId.replace(/.*\./, '');
var historyItem = req.body.postId.replace(/\..*/, '');
var date = req.body.timestamp;
console.log('comment: ' + comment + ' feedItemUser: ' + feedItemUser + ' historyItem: ' + historyItem + ' date: ' + date);
var updates = {};
firebase.database().ref('/users/' + user).once('value').then(function(snapshot) {
var first = snapshot.val().firstName;
var last = snapshot.val().lastName;
var person = first + ' ' + last;
//add new comment to the historyItem of feedItemUser
var newCommentId = firebase.database().ref(`/users/${feedItemUser}/history/${historyItem}/comments`).push().key;
var c = {
'poster': person,
'comment': comment,
'date': date,
'posterId': user
};
console.log('adding comment: ' + c);
updates[`/users/${feedItemUser}/history/${historyItem}/comments/${newCommentId}`] = c;
firebase.database().ref().update(updates);
res.send({'me': person, 'id': newCommentId, 'userId': user});
console.log('success');
});
});
app.post('/delete_comment_on_feed', async function (req, res, next) {
var user = firebase.auth().currentUser.uid;
var commId = req.body.comm;
var userId = req.body.user;
var historyId = req.body.history;
console.log('comm: ' + commId + ' userId: ' + userId + ' historyId: ' + historyId);
//delete comment from the historyId of userId
firebase.database().ref(`/users/${userId}/history/${historyId}/comments/${commId}`).remove();
res.send({'removed': 'success'});
console.log('success');
});
// app.post('/get_user_list', async function(req, res, next) {
// res.contentType('json');
// console.log("@!$#!@$H!@H$!@H#H!@$@");
// try {
// var usersRef = firebase.database().ref('users').once('value', function(snapshot){
// console.log(snapshot.val());
// // snapshot.forEach(function(childSnapshot) {
// // //var key = childSnapshot.key;
// // //var childData = childSnapshot.val();
// // var first = childSnapshot.val().firstName;
// // var last = childSnapshot.val().lastName;
// //
// // //console.log('heres a key: ' + key);
// // console.log('name ' + first + ' ' + last);
// // //console.log('heres data: ' + childData);
// // res.send({'name': first + ' ' + last});
// // });
// users = {}
//
// });
// } catch (e) {
// console.log('fail');
// console.error(e);
// res.send({'name': 'unknown'});
// }
// });
app.post('/get_user_list', async function(req, res, next) {
res.contentType('json');
try {
var userList = [];
firebase.database().ref('/users').once('value').then(function(snapshot) {
//console.log(snapshot.val());
//console.log("PLEASEPALEASEAE");
snapshot.forEach(x => {
userList.push({
name: x.val().firstName + ' ' + x.val().lastName,
uid: x.val().userId
})
})
res.send({'userList': userList, 'myuid': firebase.auth().currentUser.uid});
});
console.log('success user list');
} catch (e) {
console.log('fail user list');
console.error(e);
res.send({'userList': 'unknown'});
}
});
app.post('/get_group_info', async function(req, res, next) {
var id = req.body.id;
res.contentType('json');
var num, first, last, history;
try {
var numMembers = 0;
var memberIds = [];
var members = {};
firebase.database().ref('/groups/' + id).once('value').then(function(snapshot) {
memberIds = snapshot.val().users;
numMembers = snapshot.val().users.length;
history = snapshot.val().history;
console.log(`number of users: ${num}`);
});
var usersRef = firebase.database().ref('/users').once('value').then(function(snapshot){
//console.log(snapshot.val());
snapshot.forEach(function(childSnapshot) {
var userId = childSnapshot.val().userId;
if (memberIds.indexOf(userId) !== -1) { // If user id is in memberIds, add to memberNames
var first = childSnapshot.val().firstName;
var last = childSnapshot.val().lastName;
var name = first + ' ' + last;
var balance = childSnapshot.val().balance;
members[userId] = {
name: name,
balance: balance,
}
}
});
// MOCK DATA - TODO REMOVE
/*members['User1'] = {
name: 'User 1',
balance: '123'
};
members['User2'] = {
name: 'User 2',
balance: '456'
};*/
// Generates an array of member user ids in alphabetical order
var memberNameIds = Object.keys(members).sort(function (a, b) {
return members[a].name - members[b].name;
});
// Generate leaderboard based on balance - TODO use better formula
// This generates an array of member user ids in descending order of balance
var leaderboardIds = Object.keys(members).sort(function (a, b) {
return members[b].balance - members[a].balance;
});
var myId = firebase.auth().currentUser.uid;
firebase.database().ref('/users/' + myId).once('value').then(function(snapshot) {
console.log(myId);
var first = snapshot.val().firstName;
var last = snapshot.val().lastName;
var myName = first + ' ' + last;
res.send({
'numMembers': numMembers,
'members': members,
'memberNameIds': memberNameIds,
'leaderboardIds': leaderboardIds,
'history': history,
'myName': myName
});
});
});
console.log('success');
} catch (e) {
console.log('fail');
console.error(e);
res.send({
'numMembers': 'Unknown',
'members': {},
'memberNameIds': [],
'leaderboardIds': [],
'history': []
});
}
});
app.post('/purchase_stock', async function(req, res, next) {
var tradeAmount = req.body.tradeAmount;
var companyName = req.body.companyName;
var companyCode = req.body.companyCode;
var share_price = req.body.share_price;
var type = req.body.type;
var num_units = req.body.num_units;
console.log(companyCode);
console.log(companyName);
console.log(tradeAmount);
res.contentType('json');
var user = firebase.auth().currentUser.uid;
console.log("current user = " + user);
try {
if (user) {
console.log(user)
firebase.database().ref(`users/${user}/purchases`).push({
tradeAmount: tradeAmount,
companyName: companyName,
companyCode: companyCode,
date: Date.now(),
type: type,
num_units: num_units,
share_price: share_price
});
if (type == "long") {
var ref = firebase.database().ref(`users/${user}/balance`);
ref.once('value', function(snapshot) {
// console.log(snapshot.val());
var newBalance = snapshot.val() - tradeAmount;
var finalBalance = newBalance.toFixed(2);
firebase.database().ref(`users/${user}`).update({balance: finalBalance})
});
}
res.send({purchase_made: true});
} else {
res.send({purchase_made: false});
}
console.log("successs");
} catch (e) {
console.log('fail');
console.error(e);
res.send({purchase_made: false});
}
});
app.post('/ibm', async function(req, res, next) {
var articleText = req.body.text;
//console.log(articleText);
var NaturalLanguageUnderstandingV1 = require('watson-developer-cloud/natural-language-understanding/v1.js');
//use ibm analytics module
var natural_language_understanding = new NaturalLanguageUnderstandingV1({
'username': '38b6800f-cd8d-4144-b268-4fb588b0c021',
'password': 'tdntnZanPEQt',
'version_date': '2017-02-27'
});
//request sentiment for the article
var parameters = {
'text': articleText,
'features': {
'sentiment': {
}
}
};
natural_language_understanding.analyze(parameters, function(err, response) {
if (err) {
console.log('error:', err);
} else {
//send back the flat json
var data = JSON.stringify(response, null, 2);
//console.log('sentiment result: ' + JSON.stringify(data));
res.writeHead(200, {
'Content-Type': 'text/html',
'Content-Length': data.length
});
res.write(data);
res.end();
}
});
});
// catch 404 and forward to error handler
app.use(function(req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
});
// error handler
app.use(function(err, req, res, next) {
// set locals, only providing error in development
res.locals.message = err.message;
res.locals.error = req.app.get('env') === 'development' ? err : {};
// render the error page
res.status(err.status || 500);
res.render('error');
});
module.exports = app;