-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathstats-moderators-shares.js
87 lines (76 loc) · 3.2 KB
/
stats-moderators-shares.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
import Moderator from './server/models/moderator.model';
import Stats from './server/models/stats.model';
import Post from './server/models/post.model';
import config from './config/config';
const mongoose = require('mongoose');
mongoose.Promise = require('bluebird');
mongoose.connect(`${config.mongo.host}`);
const conn = mongoose.connection;
conn.once('open', function ()
{
const dedicatedPercentageModerators = 5;
const query = {
reviewed: true,
moderator: {
$exists : true
}
};
Post
.countAll({ query })
.then(countPosts => {
Post
.list({ skip: 0, limit: countPosts, query })
.then(posts => {
if(posts.length > 0) {
posts.forEach((post, indexPost) => {
Moderator.get(post.moderator).then(moderator => {
let moderatorObj = moderator;
if (!moderator) {
moderatorObj = new Moderator({
account: post.moderator,
total_paid_rewards: 0,
should_receive_rewards: 0,
total_moderated: 1,
percentage_total_rewards_moderators: 0,
})
}
const queryTotalModerated = {
moderator: moderatorObj.account,
reviewed: true,
};
Post
.countAll({ query: queryTotalModerated })
.then(currentModerated => {
Stats.get()
.then(stats => {
const percentageTotalShares = (currentModerated / countPosts) * 100;
const total_paid_authors = stats.total_paid_authors;
const totalDedicatedModerators = (total_paid_authors * dedicatedPercentageModerators) / 100;
const shouldHaveReceivedRewards = (percentageTotalShares * totalDedicatedModerators) / 100;
const total_paid_rewards = moderatorObj.total_paid_rewards;
if (shouldHaveReceivedRewards > total_paid_rewards) {
const mustReceiveRewards = shouldHaveReceivedRewards - total_paid_rewards;
moderatorObj.should_receive_rewards = mustReceiveRewards;
}
if (shouldHaveReceivedRewards < total_paid_rewards) {
const waitForNextRewards = 0;
moderatorObj.should_receive_rewards = waitForNextRewards;
}
moderatorObj.total_moderated = currentModerated;
moderatorObj.percentage_total_rewards_moderators = percentageTotalShares;
moderatorObj.save(savedModerator => {
if ((indexPost + 1) === posts.length) {
conn.close();
process.exit(0);
}
});
});
});
});
});
} else {
process.exit(0);
}
});
});
});