-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.js
216 lines (192 loc) · 6.46 KB
/
db.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
import { MongoClient } from "mongodb";
import bcrypt from "bcryptjs";
import { getRandomPassword } from "./util.js";
class Database {
constructor() {
this.dbClient = new MongoClient(process.env.DB_URL, {
ssl: true,
sslValidate: true,
});
this.dataCache = this.dbClient.db("encointer-kusama-accounting-backend-cache");
this.indexer = this.dbClient.db("encointer-kusama-pindex");
this.blocks = this.indexer.collection("blocks")
this.extrinsics = this.indexer.collection("extrinsics")
this.events = this.indexer.collection("events")
this.accountData = this.dataCache.collection("account_data");
this.rewardsData = this.dataCache.collection("rewards_data");
this.generalCache = this.dataCache.collection("general_cache");
this.main = this.dbClient.db("encointer-kusama-accounting");
this.users = this.main.collection("users");
this.communities = this.main.collection("communities");
this.vouchers = this.main.collection("vouchers");
this.knows_addresses = this.main.collection("known_addresses");
}
async insertIntoGeneralCache(cacheIdentifier, query, data) {
try {
console.debug('inserting into general cache', cacheIdentifier, query)
await this.generalCache.replaceOne(
{ ...query, cacheIdentifier },
{ ...query, cacheIdentifier, data },
{
upsert: true,
}
);
} catch (e) {
console.error(e);
}
}
async getFromGeneralCache(cacheIdentifier, query) {
return (
await (
await this.generalCache.find({ ...query, cacheIdentifier })
).toArray()
).map((e) => e.data);
}
async insertIntoAccountDataCache(account, year, month, cid, data) {
try {
console.debug('inserting into account data cache', account, year, month, cid)
await this.accountData.replaceOne(
{account, year, month, cid},
{account, year, month, cid, data},
{
upsert: true,
}
);
} catch (e) {
console.error(e);
}
}
async getFromAccountDataCache(account, year, cid) {
return (
await (
await this.accountData.find({ account, year, cid })
).toArray()
).map((e) => e.data);
}
async getFromAccountDataCacheByMonth(month, year, cid) {
return (
await (await this.accountData.find({ month, year, cid })).toArray()
).map((e) => e.data);
}
async insertIntoRewardsDataCache(cid, data) {
try {
console.debug('inserting into rewards data cache', cid)
await this.rewardsData.replaceOne(
{ cid },
{ cid, data },
{
upsert: true,
}
);
} catch (e) {
console.error(e);
}
}
async getFromRewardsDataCache(cid) {
return this.rewardsData.findOne({ cid });
}
async checkUserCredentials(address, password) {
const user = await this.users.findOne({ address });
if (!user) return null;
if (await bcrypt.compare(password, user.passwordHash)) return user;
return null;
}
async upsertUser(address, password, name, isAdmin = false, isReadonlyAdmin = false) {
await this.users.replaceOne(
{ address },
{
address,
name,
passwordHash: await bcrypt.hash(password, 10),
isAdmin,
isReadonlyAdmin,
},
{
upsert: true,
}
);
}
async setPassword(address, password) {
await this.users.updateOne(
{ address },
{ $set: { passwordHash: await bcrypt.hash(password, 10) } }
);
}
async addUserToCommunities(address, cids) {
await this.communities.updateMany(
{ cid: { $in: cids } },
{ $push: { accounts: address } }
);
}
async removeUserFromAllCommunities(address) {
await this.communities.updateMany({}, { $pull: { accounts: address } });
}
async createUser(address, name, cids) {
if (await this.getUser(address)) throw Error("User Exists");
const password = getRandomPassword();
this.upsertUser(address, password, name);
this.addUserToCommunities(address, cids);
return password;
}
async deleteUser(address) {
await this.users.deleteOne({ address });
await this.removeUserFromAllCommunities(address);
}
async getUser(address) {
return this.users.findOne(
{ address },
{ projection: { address: 1, name: 1, isAdmin: 1, _id: 0 } }
);
}
async updateUser(address, name, cids) {
await this.users.updateOne({ address }, { $set: { name } });
await this.removeUserFromAllCommunities(address);
await this.addUserToCommunities(address, cids);
}
async getAllUsers() {
return this.users
.find(
{isAdmin: false},
{ projection: { address: 1, name: 1, isAdmin: 1, _id: 0 } }
)
.toArray();
}
async getCommunityUsers(cid) {
const community = await this.getCommunity(cid);
return this.users
.find({ address: { $in: community.accounts } })
.toArray();
}
async getCommunity(cid) {
return this.communities.findOne({ cid });
}
async getAllCommunities() {
return this.communities
.find({}, { projection: { cid: 1, name: 1, _id: 0 } })
.toArray();
}
async getVoucherAddresses(cid) {
return (
await this.vouchers
.find({ cid }, { projection: { address: 1 } })
.toArray()
).map((e) => e.address);
}
async getGovAddresses(cid) {
return (
await this.knows_addresses
.find({ cid, type: "gov" }, { projection: { address: 1 } })
.toArray()
).map((e) => e.address);
}
async getAcceptancePointAddresses(cid) {
return (
await this.communities.findOne(
{ cid },
{ projection: { accounts: 1 } }
)
).accounts;
}
}
const db = new Database();
export default db;