-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcreate-top100-referral-collection.ts
More file actions
105 lines (91 loc) · 2.88 KB
/
Copy pathcreate-top100-referral-collection.ts
File metadata and controls
105 lines (91 loc) · 2.88 KB
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
import mongoose from 'mongoose';
import dotenv from 'dotenv';
import User from './src/models/User';
dotenv.config();
const MONGODB_URI = process.env.MONGODB_URI || 'mongodb://localhost:27017/startvibin';
// Define schema for the new collection
const top100ReferralSchema = new mongoose.Schema({
walletAddress: {
type: String,
required: true,
unique: true,
index: true
},
referralCode: {
type: String,
required: false, // Make it optional
default: 'NO_CODE'
},
totalPoints: {
type: Number,
required: true
},
rank: {
type: Number,
required: true
},
createdAt: {
type: Date,
default: Date.now
}
});
// Create model for the new collection
const Top100Referral = mongoose.model('Top100Referral', top100ReferralSchema, 'top100Referrals');
async function createTop100ReferralCollection() {
try {
await mongoose.connect(MONGODB_URI);
console.log('Connected to MongoDB');
// Get top 100 users sorted by totalPoints descending
const top100Users = await User.aggregate([
{
$addFields: {
totalPoints: {
$add: ['$gamePoints', '$referralPoints', '$socialPoints']
}
}
},
{
$sort: { totalPoints: -1 }
},
{
$limit: 100
},
{
$project: {
walletAddress: 1,
referralCode: 1,
totalPoints: 1
}
}
]);
console.log(`Found ${top100Users.length} top users`);
// Clear existing data in the collection
await Top100Referral.deleteMany({});
console.log('Cleared existing top100Referrals collection');
// Insert top 100 users into the new collection
const referralData = top100Users
.filter(user => user.referralCode && user.referralCode.trim() !== '') // Only include users with referral codes
.map((user, index) => ({
walletAddress: user.walletAddress,
referralCode: user.referralCode,
totalPoints: user.totalPoints,
rank: index + 1
}));
const result = await Top100Referral.insertMany(referralData);
console.log(`Successfully inserted ${result.length} users into top100Referrals collection`);
// Display the results
console.log('\nTop 100 Users Referral Codes:');
console.log('Rank | Wallet Address | Referral Code | Total Points');
console.log('-----|----------------|---------------|-------------');
referralData.forEach((user, index) => {
const truncatedWallet = user.walletAddress.slice(0, 6) + '...' + user.walletAddress.slice(-4);
console.log(`${(index + 1).toString().padStart(4)} | ${truncatedWallet} | ${user.referralCode.padEnd(12)} | ${user.totalPoints}`);
});
} catch (err) {
console.error('Error creating top 100 referral collection:', err);
} finally {
await mongoose.disconnect();
console.log('\nDisconnected from MongoDB');
}
}
createTop100ReferralCollection();