/
app.js
788 lines (679 loc) · 23.1 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
var express = require('express');
var exphbs = require('express-handlebars');
const MongoClient = require('mongodb').MongoClient;
var utils = require('./utils');
const moment = require('moment')
var app = express();
app.engine('handlebars', exphbs({defaultLayout: 'main'}));
app.set('view engine', 'handlebars');
var config = utils.getConfig();
// Connection URL
const url = config.mongo_uri;
var db;
var collection;
// Database Name
const db_name = config.db_name;
const collection_name = 'user_tokens';
// Use connect method to connect to the server
MongoClient.connect(url, function(err, client) {
if(!err) {
console.log("Connected successfully to server");
db = client.db(db_name);
// Get the documents collection
collection = db.collection(collection_name);
} else {
utils.log(err, 'api');
}
});
app.get('/', function (req, res) {
var data = {};
data.posts = [
{
url: 'dsadsa',
net_votes: 44,
vote_weight: "0.03"
}];
data.total_votes = 323;
data.total_money = "$0.63";
// res.render('home', data);
res.send('Hello there!');
});
/* function handles calculating and returning user token count */
grabUserTokensFunc = async function (req, res){
let user = await collection.findOne({_id: req.params.user}, {fields : { _id:0} });
console.log(user);
//fixing token amount display for 3 digits
if (typeof user!= "undefined" && user!=null){
if (typeof user.tokens!= "undefined"){
user.tokens = user.tokens.toFixed(3)
}
}
return user;
}
/* end point for user total token count display */
app.get('/user/:user', async function (req, res) {
let user = await grabUserTokensFunc(req,res);
res.header('Access-Control-Allow-Origin', '*');
res.send(user);
});
/* end point for user transactions display (per user or general actifit token transactions, limited by 250 */
app.get('/transactions/:user?', async function (req, res) {
let query = {};
var transactions;
if(req.params.user){
query = {user: req.params.user}
transactions = await db.collection('token_transactions').find(query, {fields : { _id:0} }).sort({date: -1}).toArray();
}else{
//only limit returned transactions in case this is a general query
transactions = await db.collection('token_transactions').find(query, {fields : { _id:0} }).sort({date: -1}).limit(1000).toArray();
}
res.header('Access-Control-Allow-Origin', '*');
res.send(transactions);
});
/* end point for returning number of awarded users and tokens distributed */
app.get('/userTokensInfo', async function(req, res) {
await db.collection(collection_name).aggregate([
{
$match: {}
},
{
$group:
{
_id: null,
tokens_distributed: { $sum: "$tokens" },
user_count: { $sum: 1 }
}
}
]).toArray(function(err, results) {
var output = 'rewarded users:'+results[0].user_count+',';
output += 'tokens distributed:'+results[0].tokens_distributed;
res.header('Access-Control-Allow-Origin', '*');
res.send(results);
console.log(results);
});
});
/* end point for returning total delegation payments (number of delegators and amount paid) on a specific date */
app.get('/delegationPayments', async function(req, res) {
var todayDate = moment(moment().utc().startOf('date').toDate()).format('YYYY-MM-DD');
var dateRegex = todayDate; // /^2018-08-05/
if (req.query.targetDate){
dateRegex = req.query.targetDate;
}
await db.collection('token_transactions').aggregate([
{
$match:
{
"reward_activity": "Delegation",
"date": {
'$eq' : new Date(dateRegex)
}
}
},
{
$group:
{
_id: null,
tokens_distributed: { $sum: "$token_count" },
user_count: { $sum: 1 }
}
}
]).toArray(function(err, results) {
res.header('Access-Control-Allow-Origin', '*');
res.send(results);
console.log(results);
});
});
/* end point for returning total payments (categorized by reward type as well as a full total) on a specific date */
app.get('/totalTokensDistributed', async function(req, res) {
var startDate = moment(moment().utc().startOf('date').toDate()).format('YYYY-MM-DD');
if (req.query.targetDate){
startDate = moment(moment(req.query.targetDate).utc().startOf('date').toDate()).format('YYYY-MM-DD');
}
var endDate = moment(moment(startDate).utc().add(1, 'days').toDate()).format('YYYY-MM-DD');
console.log("startDate:"+startDate+" endDate:"+endDate);
await db.collection('token_transactions').aggregate([
{
$match:
{
"date": {
"$lte": new Date(endDate),
"$gt": new Date(startDate)
}
}
},
{
$group:
{
_id: {reward_activity:"$reward_activity"},
tokens_distributed: { $sum: "$token_count" },
}
}
]).toArray(function(err, results) {
res.header('Access-Control-Allow-Origin', '*');
//also append total token count to the grouped display
let tot_tokens = 0;
for (let entry of results) {
tot_tokens += entry.tokens_distributed;
}
console.log(tot_tokens);
results.push([{"_id":null,"tokens_distributed":tot_tokens}]);
res.send(results);
console.log(results);
});
});
/* end point for returning count of posts/activities rewarded */
app.get('/rewarded-activity-count', async function(req, res) {
await db.collection("posts").aggregate( [
{ $count: "reward_count" }
]).toArray(function(err, results) {
console.log(results);
utils.log(results, 'rewarded-activity-count');
res.header('Access-Control-Allow-Origin', '*');
res.send(results);
});
});
/* end point for returning charity data supported by actifit */
app.get('/charities', async function (req, res) {
var charities = await db.collection('available_charities').find({status:"enabled"}, {charity_name: 1}).sort({charity_name: 1}).toArray();
res.header('Access-Control-Allow-Origin', '*');
res.send(charities);
});
/* end point for returning current active delegator data by actifit */
app.get('/topDelegators', async function (req, res) {
var delegatorList;
if (isNaN(req.query.count)){
delegatorList = await db.collection('active_delegations').find().sort({steem_power: -1}).toArray();
}else{
delegatorList = await db.collection('active_delegations').find().sort({steem_power: -1}).limit(parseInt(req.query.count)).toArray();
}
res.header('Access-Control-Allow-Origin', '*');
res.send(delegatorList);
});
activeDelegationFunc = async function (userName){
let user = await db.collection('active_delegations').findOne({_id: userName}, {fields : { _id:0} });
console.log(user);
return user;
}
/* end point for returning a single user last recorded active delegation amount */
app.get('/delegation/:user', async function (req, res) {
var user = await activeDelegationFunc(req.params.user);
res.header('Access-Control-Allow-Origin', '*');
res.send(user);
});
moderatorsListFunc = async function () {
let moderatorList = await db.collection('team').find({title:'moderator', status:'active'}).sort({name: 1}).toArray();
return moderatorList;
}
/* end point for returning current active moderators data by actifit */
app.get('/moderators', async function (req, res) {
var moderatorList;
moderatorList = await moderatorsListFunc();
res.header('Access-Control-Allow-Origin', '*');
res.send(moderatorList);
});
/* end point for returning current active ambassadors data by actifit */
app.get('/ambassadors', async function (req, res) {
var ambassadorList;
ambassadorList = await db.collection('team').find({title:'ambassador', status:'active'}).sort({name: 1}).toArray();
res.header('Access-Control-Allow-Origin', '*');
res.send(ambassadorList);
});
/* end point for returning current top AFIT token holders */
app.get('/topTokenHolders', async function (req, res) {
var delegatorList;
if (isNaN(req.query.count)){
delegatorList = await db.collection('user_tokens').find().sort({tokens: -1}).toArray();
}else{
delegatorList = await db.collection('user_tokens').find().sort({tokens: -1}).limit(parseInt(req.query.count)).toArray();
}
res.header('Access-Control-Allow-Origin', '*');
res.send(delegatorList);
});
/* end point for returning accounts banned by actifit*/
app.get('/bannedUsers', async function (req, res) {
var banned_users = await db.collection('banned_accounts').find({ban_status:"active"}).toArray();
res.header('Access-Control-Allow-Origin', '*');
res.send(banned_users);
});
/* end point for counting number of reblogs on a certain date param (default current date) */
app.get('/reblogCount', async function (req, res) {
var todayDate = moment(moment().utc().startOf('date').toDate()).format('YYYY-MM-DD');
//fileName = "steemrewards"+fileName+".json";
var dateRegex = new RegExp ('^'+todayDate); // /^2018-08-05/
if (req.query.targetDate){
dateRegex = new RegExp ('^'+req.query.targetDate);
}
let query = await db.collection('token_transactions').find({
"reward_activity": "Post Reblog",
"date": dateRegex
})
try{
console.log('counting');
let reblog_count = await query.count();
console.log(reblog_count);
res.header('Access-Control-Allow-Origin', '*');
res.send(JSON.stringify({reblog_count:reblog_count}));
}catch(err){
console.log(err.message);
}
});
/* end point for counting number of upvotes on a certain date param (default current date) */
app.get('/upvoteCount', async function (req, res) {
var startDate = moment(moment().utc().startOf('date').toDate()).format('YYYY-MM-DD');
if (req.query.targetDate){
startDate = moment(moment(req.query.targetDate).utc().startOf('date').toDate()).format('YYYY-MM-DD');
}
var endDate = moment(moment(startDate).utc().add(1, 'days').toDate()).format('YYYY-MM-DD');
console.log("startDate:"+startDate+" endDate:"+endDate);
//adjust query to include dates
query_json = {
"reward_activity": "Post Vote",
"date": {
"$lte": new Date(endDate),
"$gt": new Date(startDate)
}
};
let query = await db.collection('token_transactions').find(query_json);
try{
console.log('counting');
let upvote_count = await query.count();
console.log(upvote_count);
res.header('Access-Control-Allow-Origin', '*');
res.send(JSON.stringify({upvote_count:upvote_count}));
}catch(err){
console.log(err.message);
}
});
/* end point for counting number of rewarded posts on a certain date param (default current date) */
app.get('/rewardedPostCount', async function (req, res) {
var startDate = moment(moment().utc().startOf('date').toDate()).format('YYYY-MM-DD');
if (req.query.targetDate){
startDate = moment(moment(req.query.targetDate).utc().startOf('date').toDate()).format('YYYY-MM-DD');
}
var endDate = moment(moment(startDate).utc().add(1, 'days').toDate()).format('YYYY-MM-DD');
console.log("startDate:"+startDate+" endDate:"+endDate);
//adjust query to include dates
query_json = {
"reward_activity": "Post",
"date": {
"$lte": new Date(endDate),
"$gt": new Date(startDate)
}
};
let query = await db.collection('token_transactions').find(query_json);
try{
console.log('counting');
let rewarded_post_count = await query.count();
console.log(rewarded_post_count);
res.header('Access-Control-Allow-Origin', '*');
res.send(JSON.stringify({rewarded_post_count:rewarded_post_count}));
}catch(err){
console.log(err.message);
}
});
/* refactored function to grab rewarded post count per user for use across get calls */
userRewardedPostCountFunc = async function(req, res){
var user = req.params.user;
//default query
var query_json = {
"reward_activity": "Post",
"user": user
};
//if this is a sum for specific period v/s a total sum
if (typeof req.query.period != "undefined" && !isNaN(req.query.period)){
var days = req.query.period;
//console.log("days:"+days);
var startDate = moment(moment().utc().startOf('date').toDate()).format('YYYY-MM-DD');
var endDate = moment(moment().utc().startOf('date').subtract(days, 'days').toDate()).format('YYYY-MM-DD');
//console.log("startDate:"+startDate+" endDate:"+endDate);
//adjust query to include dates
query_json = {
"reward_activity": "Post",
"user": user,
"date": {
"$gte": new Date(endDate),
"$lt": new Date(startDate)
}
};
}
//build up query accordingly
let query = await db.collection('token_transactions').find(query_json);
try{
//grab total number of matching records
let rewarded_post_count = await query.count();
//console.log("rewarded_post_count:"+rewarded_post_count);
return rewarded_post_count;
}catch(err){
console.log(err.message);
return "";
}
}
/* end point for counting number of rewarded posts on a certain date param (default current date) */
app.get('/userRewardedPostCount/:user', async function (req, res) {
//grab user account
if (typeof req.params.user!= "undefined" && req.params.user!=null){
var rewarded_post_count = await userRewardedPostCountFunc(req, res);
res.header('Access-Control-Allow-Origin', '*');
res.send(JSON.stringify({rewarded_post_count:rewarded_post_count}));
}else{
res.send("");
}
});
/* end point for getting current user's Actifit rank */
app.get('/getRank/:user', async function (req, res) {
if (typeof req.params.user!= "undefined" && req.params.user!=null){
//delegation calculation matrix
var delegation_rules = [
[9,0],
[499,0.05],
[999,0.10],
[4999,0.20],
[9999,0.30],
[19999,0.40],
[49999,0.55],
[99999,0.65],
[499999,0.75],
[999999,0.90],
[1000000,1]
]
//AFIT token calculation matrix
var afit_token_rules = [
[9,0],
[999,0.10],
[4999,0.20],
[9999,0.30],
[19999,0.40],
[49999,0.50],
[99999,0.60],
[499999,0.70],
[999999,0.80],
[4999999,0.90],
[5000000,1]
]
//Rewarded Posts calculation matrix
var rewarded_posts_rules = [
[9,0],
[29,0.10],
[59,0.20],
[89,0.30],
[119,0.40],
[179,0.50],
[359,0.60],
[539,0.70],
[719,0.80],
[1079,0.90],
[1080,1]
]
//Rewarded Posts calculation matrix
var recent_reward_posts_rules = [
[0,0],
[2,0.20],
[4,0.40],
[6,0.60],
[8,0.80],
[9,1]
]
var user_rank = 0;
//grab delegation amount
var userDelegations = await activeDelegationFunc(req.params.user);
let delegSP = 0;
//get current delegated SP if any
if (userDelegations != null){
console.log('already delegated');
delegSP = userDelegations.steem_power;
}
//console.log(userDelegations.steem_power);
var delegation_score = 0;
//check if the user has an alt account as beneficiary
let delegator_info = await getAltAccountStatusFunc(req.params.user);
//check if returned object is not empty
if (Object.keys(delegator_info).length > 0){
if (parseInt(delegator_info.user_rank_benefit) == 1){
//consider as no delegations
delegSP = 0;
}
}else{
//also check the other case where the account is an alt-account
delegator_info = await getAltAccountByNameFunc(req.params.user);
//check if returned object is not empty
if (Object.keys(delegator_info).length > 0){
if (parseInt(delegator_info.user_rank_benefit) == 1){
//get original user delegation amount
userDelegations = await activeDelegationFunc(delegator_info.delegator);
if (userDelegations != null){
delegSP = userDelegations.steem_power;
}
}
}
}
if (parseFloat(delegSP) > 0){
delegation_score = utils.calcScore(delegation_rules, config.delegation_factor, parseFloat(delegSP));
}
user_rank += delegation_score;
//grab user token count
var userTokens = await grabUserTokensFunc(req,res);
//console.log(userTokens.tokens);
var afit_tokens_score = 0;
if (userTokens != null){
afit_tokens_score = utils.calcScore(afit_token_rules, config.afit_token_factor, parseFloat(userTokens.tokens));
}
user_rank += afit_tokens_score;
//grab total rewarded posts count
var tot_rewarded_post_count = await userRewardedPostCountFunc(req, res);
//console.log(tot_rewarded_post_count);
var tot_posts_score = utils.calcScore(rewarded_posts_rules, config.rewarded_posts_factor, parseInt(tot_rewarded_post_count));
user_rank += tot_posts_score;
//set the check period for config value of days days, and rerun the call to get last rewarded posting activity during this period
req.query.period = config.recent_posts_period;
var recent_rewarded_post_count = await userRewardedPostCountFunc(req, res);
//console.log(recent_rewarded_post_count);
var recent_posts_score = utils.calcScore(recent_reward_posts_rules, config.recent_posts_factor, parseInt(recent_rewarded_post_count));
user_rank += recent_posts_score;
var score_components = JSON.stringify({
user_rank: user_rank,
delegation_score: delegation_score,
afit_tokens_score: afit_tokens_score,
tot_posts_score: tot_posts_score,
recent_posts_score:recent_posts_score
});
console.log(score_components)
res.header('Access-Control-Allow-Origin', '*');
res.send(score_components);
}else{
res.send("");
}
});
/* function handles the backbone for grabbing Alt Account Status */
getAltAccountStatusFunc = async function (user){
let delegator_info = null;
if (typeof user!= "undefined" && user!=null){
//in this case, we check the status of a single user
var query_json = {
"delegator": user
};
delegator_info = await db.collection('delegation_alt_beneficiaries').findOne(query_json, {fields : { _id:0} });
if (delegator_info==null){
delegator_info = {};
}
console.log(delegator_info);
}else{
//alternatively grab all alt-account reward delegations
delegator_info = await db.collection('delegation_alt_beneficiaries').find().toArray();
console.log(delegator_info);
}
return delegator_info;
}
/* function handles checking if alt-account is linked to a delegator */
getAltAccountByNameFunc = async function (targetUser){
let delegator_info = null;
if (typeof targetUser!= "undefined" && targetUser!=null){
//in this case, we check the status of a single user
var query_json = {
"alt_account": targetUser
};
delegator_info = await db.collection('delegation_alt_beneficiaries').findOne(query_json, {fields : { _id:0} });
if (delegator_info==null){
delegator_info = {};
}
console.log(delegator_info);
}
return delegator_info;
}
/* end point for getting list of SP delegator accounts who wish to move their user rank and/or rewards to their alt-accounts*/
app.get('/getAltAccountStatus/:user?', async function (req, res) {
let delegator_info = await getAltAccountStatusFunc(req.params.user);
res.header('Access-Control-Allow-Origin', '*');
res.send(delegator_info);
});
/* function handles processing requests for getting AFIT token pay depending on reward activity type */
getPostRewardFunc = async function(user, url, reward_activity){
var query_json = {
"reward_activity": reward_activity,
"user": user,
"url":url
};
let post_details = await db.collection('token_transactions').findOne(query_json, {fields : { _id:0} });
console.log(post_details);
//fixing token amount display for 3 digits
if (typeof post_details!= "undefined" && post_details!=null){
if (typeof post_details.token_count!= "undefined"){
return post_details.token_count;
}
}
//otherwise return no tokens
return 0;
}
/* end point for getting a post's reward */
app.get('/getPostReward', async function (req, res) {
if (typeof req.query.user!= "undefined" && req.query.user!=null
&& typeof req.query.url!= "undefined" && req.query.url!=null){
var user = req.query.user;
var url = req.query.url;
console.log('url:'+url);
//grab specific reward type for user and post
var token_count = await getPostRewardFunc(user, url, "Post");
res.header('Access-Control-Allow-Origin', '*');
res.send({token_count: token_count});
}else{
res.header('Access-Control-Allow-Origin', '*');
res.send({token_count: 0});
}
});
/* end point for retrieving a post's full AFIT Pay reward */
app.get('/getPostFullAFITPayReward', async function (req, res) {
if (typeof req.query.user!= "undefined" && req.query.user!=null
&& typeof req.query.url!= "undefined" && req.query.url!=null){
var user = req.query.user;
var url = req.query.url;
//for the full AFIT rewards, grab only permalink portion without the community and author name
url = url.substring(url.lastIndexOf('/')+1);
console.log('url:'+url);
//grab specific reward type for user and post
var token_count = await getPostRewardFunc(user, url, "Full AFIT Payout");
res.header('Access-Control-Allow-Origin', '*');
res.send({token_count: token_count});
}else{
res.header('Access-Control-Allow-Origin', '*');
res.send({token_count: 0});
}
});
/* end point for returning total number of rewarded tokens to charities based upon user activity, along with unique user count who donated */
app.get('/getCharityRewards', async function(req, res) {
await db.collection('token_transactions').aggregate([
{
$match: {reward_activity:'Charity Post'}
},
{
$group:
{
_id: null,
tokens_distributed: { $sum: "$token_count" },
user_count: { $sum: 1 }
}
}
]).toArray(function(err, results) {
var output = 'rewarded users:'+results[0].user_count+',';
output += 'tokens distributed:'+results[0].tokens_distributed;
res.header('Access-Control-Allow-Origin', '*');
res.send(results);
console.log(results);
});
});
/* end point for returning total number of AFIT tokens paid in return for full AFIT pay along with matching STEEM + SBD */
app.get('/getFullAFITPayStats', async function(req, res) {
await db.collection('token_transactions').aggregate([
{
$match: {"reward_activity": "Full AFIT Payout"}
},
{
$group:
{
_id: null,
afit_tokens: { $sum: "$token_count" },
orig_sbd_amount: { $sum: "$orig_sbd_amount" },
orig_steem_amount: { $sum: "$orig_steem_amount" },
transaction_count: { $sum: 1 }
}
}
]).toArray(function(err, results) {
res.header('Access-Control-Allow-Origin', '*');
res.send(results);
console.log(results);
});
});
/* end point for capturing moderator activity on a specific date and for a specific period (defaults today and a single day activity) */
app.get('/moderatorActivity', async function(req, res) {
let moderatorsList = await moderatorsListFunc();
//default today
var startDate = moment(moment().utc().startOf('date').toDate()).format('YYYY-MM-DD');
if (req.query.targetDate){
startDate = moment(moment(req.query.targetDate).utc().startOf('date').toDate()).format('YYYY-MM-DD');
}
//default single day
let days = 1;
if (!isNaN(req.query.days)){
days = req.query.days;
}
var endDate = moment(moment(startDate).utc().subtract(days, 'days').toDate()).format('YYYY-MM-DD');
console.log("startDate:"+startDate+" endDate:"+endDate);
await db.collection('team').aggregate([
{
$match:
{
title:'moderator',
status:'active'
}
},
{
$lookup:
{
from: "token_transactions",
localField: "name",
foreignField: "user",
as: "moderatorActivity"
}
},
{
$project:
{
'_id':0,
items:
{
$filter: {
input: "$moderatorActivity",
as: "singleEntry",
cond: { $and: [
{ "$lte": ["$$singleEntry.date", new Date(startDate)] },
{ "$gt": ["$$singleEntry.date", new Date(endDate)] }
] }
}
}
}
}
]).toArray(function(err, results) {
res.header('Access-Control-Allow-Origin', '*');
res.send(results);
console.log(results);
});
});
app.listen(process.env.PORT || 3000);