Skip to content

Commit

Permalink
Create new API endpoints
Browse files Browse the repository at this point in the history
- Create new endpoint for retrieving a post's full AFIT Pay reward. This allows filtering based on user and post url
- Create new endpoint for gathering total number of AFIT tokens paid out based on charity-based activity
- Create new endpoint for retrieving number of AFIT tokens rewarded in return for STEEM/SBD based on Full AFIT Pay option 
- Refactor post reward functionality to allow code reuse across new end points.
  • Loading branch information
mcfarhat committed Nov 9, 2018
1 parent d7b1e46 commit 9a097fc
Showing 1 changed file with 93 additions and 13 deletions.
106 changes: 93 additions & 13 deletions app.js
Expand Up @@ -473,7 +473,7 @@ app.get('/getRank/:user', async function (req, res) {
[4,0.40],
[6,0.60],
[8,0.80],
[10,1]
[9,1]
]

var user_rank = 0;
Expand Down Expand Up @@ -613,17 +613,10 @@ app.get('/getAltAccountStatus/:user?', async function (req, res) {
res.send(delegator_info);
});

/* 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);
//default query
/* 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": "Post",
"reward_activity": reward_activity,
"user": user,
"url":url
};
Expand All @@ -633,19 +626,106 @@ app.get('/getPostReward', async function (req, res) {
//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: post_details.token_count});
}
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();
Expand Down

0 comments on commit 9a097fc

Please sign in to comment.