-
Notifications
You must be signed in to change notification settings - Fork 2
/
updateUserVote.js
38 lines (35 loc) · 1.31 KB
/
updateUserVote.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
const db = require('./models')
async function main() {
let lastRow = await db.UserVoteAmount.findOne().sort({epoch: -1})
if (lastRow) {
let lastEpoch = lastRow.epoch
for (let i = 0; i<= lastEpoch; i++) {
console.log('Process epoch', i)
let voteInEpoch = await db.UserVoteAmount.find({epoch: i})
let data = []
for (let j = 0; j < voteInEpoch.length; j++) {
if (i < lastEpoch) {
let nextEpoch = await db.UserVoteAmount.findOne({
voter: voteInEpoch[j].voter,
epoch: voteInEpoch[j].epoch + 1,
candidate: voteInEpoch[j].candidate
})
if (!nextEpoch) {
data.push({
voter: voteInEpoch[j].voter,
epoch: voteInEpoch[j].epoch + 1,
voteAmount: voteInEpoch[j].voteAmount,
candidate: voteInEpoch[j].candidate
})
}
}
}
if (data.length > 0) {
console.log(' Duplicate in next epoch')
await db.UserVoteAmount.insertMany(data)
}
}
}
process.exit(1)
}
main()