-
Notifications
You must be signed in to change notification settings - Fork 11
/
profile-helper.js
115 lines (95 loc) · 2.91 KB
/
profile-helper.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
const null_profile = {
description: "",
email: "",
familyName: "",
gender: "",
givenName: "",
image: "",
sameAs: [],
timezone: "0",
url: ""
};
async function getMemberType(account, dac_id, db){
let member_type = 0;
const coll = db.collection('memberstats');
const res = await coll.findOne({account, dac_id});
if (res){
member_type = res.member_type;
}
return member_type;
}
async function getProfiles(db, dac_config, dac_id, accounts, legacy = false) {
const collection = db.collection('actions');
const cust_contract = dac_config.accounts.get(2);
const query = {"action.account": cust_contract, "action.name": "stprofile", "action.data.dac_id":dac_id, "action.data.cand": {$in: accounts}};
if (legacy){
query['action.data.dac_id'] = {$in: [dac_id, null]};
query['action.name'] = {$in: ['stprofileuns', 'stprofile']};
}
const pipeline = [
{$match: query},
{'$sort': {block_num: -1}},
{
'$group': {
_id: {cand: "$action.data.cand"},
block_num: {'$first': "$block_num"},
profile: {'$first': "$action.data.profile"},
account: {'$first': "$action.data.cand"}
}
},
{'$sort': {block_num: -1}},
{
'$facet': {
results: [{$match: {}}],
count: [{'$count': 'count'}]
}
}
];
const res = await collection.aggregate(pipeline);
const found_accounts = [];
const result = await res.next();
result.results = result.results.map((row) => {
// console.log(row.profile)
if (typeof row.profile === 'string') {
try {
row.profile = JSON.parse(row.profile);
}
catch (e) {
const desc = row.profile;
row.profile = null_profile;
row.profile.description = desc;
}
}
delete row._id;
found_accounts.push(row.account);
return row
});
const missing_accounts = [];
accounts.forEach((account_name) => {
if (!found_accounts.includes(account_name)){
missing_accounts.push(account_name);
}
});
accounts.forEach((account) => {
if (missing_accounts.includes(account)){
result.results.push({
account,
block_num: 0,
profile: null_profile
});
}
});
// Calculate member type
for (let r=0;r<result.results.length;r++){
const data = result.results[r];
data.member_type = await getMemberType(data.account, dac_id, db);
// console.log(data);
result.results[r] = data;
// console.log(data);
}
if (result.count.length) {
result.count = result.results.length
}
return result;
}
module.exports = { getProfiles }