@duvdu-v1/duvdu 1.1.242 → 1.1.245
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -91,7 +91,7 @@ userSchema.pre('save', function (next) {
|
|
|
91
91
|
if (this.isModified('acceptedProjectsCounter') ||
|
|
92
92
|
this.isModified('likes') ||
|
|
93
93
|
this.isModified('projectsCount')) {
|
|
94
|
-
yield (0, rank_service_1.updateRankForUser)(this);
|
|
94
|
+
yield (0, rank_service_1.updateRankForUser)(this._id.toString());
|
|
95
95
|
}
|
|
96
96
|
next();
|
|
97
97
|
});
|
|
@@ -25,5 +25,7 @@
|
|
|
25
25
|
import { Document } from 'mongoose';
|
|
26
26
|
import { Iuser } from '../types/User';
|
|
27
27
|
export type UserDocument = Document & Iuser;
|
|
28
|
-
export declare const updateRankForUser: (
|
|
28
|
+
export declare const updateRankForUser: (userId: string) => Promise<(Document<unknown, {}, Iuser> & Iuser & {
|
|
29
|
+
_id: import("mongoose").Types.ObjectId;
|
|
30
|
+
}) | undefined>;
|
|
29
31
|
export declare const recalculateAllUsersRanks: () => Promise<void>;
|
|
@@ -12,11 +12,17 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
12
12
|
exports.recalculateAllUsersRanks = exports.updateRankForUser = void 0;
|
|
13
13
|
const ranks_model_1 = require("../models/ranks.model");
|
|
14
14
|
const User_model_1 = require("../models/User.model");
|
|
15
|
-
const updateRankForUser = (
|
|
15
|
+
const updateRankForUser = (userId) => __awaiter(void 0, void 0, void 0, function* () {
|
|
16
16
|
// get all user stats
|
|
17
|
+
const user = yield User_model_1.Users.findById(userId);
|
|
18
|
+
if (!user)
|
|
19
|
+
return;
|
|
17
20
|
const projectsCount = user.projectsCount;
|
|
18
21
|
const projectsLiked = user.likes;
|
|
19
22
|
const acceptedProjectsCounter = user.acceptedProjectsCounter;
|
|
23
|
+
console.log('acceptedProjectsCounter', acceptedProjectsCounter);
|
|
24
|
+
console.log('projectsLiked', projectsLiked);
|
|
25
|
+
console.log('projectsCount', projectsCount);
|
|
20
26
|
// Find current rank that matches ALL criteria
|
|
21
27
|
const currentRank = yield ranks_model_1.Rank.findOne({
|
|
22
28
|
actionCount: { $lte: acceptedProjectsCounter },
|
|
@@ -30,6 +36,7 @@ const updateRankForUser = (user) => __awaiter(void 0, void 0, void 0, function*
|
|
|
30
36
|
projectsCount: -1
|
|
31
37
|
})
|
|
32
38
|
.exec();
|
|
39
|
+
console.log('currentRank', currentRank);
|
|
33
40
|
// Find next possible rank
|
|
34
41
|
const nextRank = yield ranks_model_1.Rank.findOne({
|
|
35
42
|
$or: [
|
|
@@ -45,6 +52,7 @@ const updateRankForUser = (user) => __awaiter(void 0, void 0, void 0, function*
|
|
|
45
52
|
projectsCount: 1
|
|
46
53
|
})
|
|
47
54
|
.exec();
|
|
55
|
+
console.log('nextRank', nextRank);
|
|
48
56
|
if (currentRank) {
|
|
49
57
|
user.rank.title = currentRank.rank;
|
|
50
58
|
user.rank.color = currentRank.color;
|
|
@@ -81,7 +89,8 @@ const updateRankForUser = (user) => __awaiter(void 0, void 0, void 0, function*
|
|
|
81
89
|
user.rank.nextRankTitle = null;
|
|
82
90
|
user.rank.color = null;
|
|
83
91
|
}
|
|
84
|
-
|
|
92
|
+
yield user.save();
|
|
93
|
+
return user;
|
|
85
94
|
});
|
|
86
95
|
exports.updateRankForUser = updateRankForUser;
|
|
87
96
|
const recalculateAllUsersRanks = () => __awaiter(void 0, void 0, void 0, function* () {
|
|
@@ -92,14 +101,86 @@ const recalculateAllUsersRanks = () => __awaiter(void 0, void 0, void 0, functio
|
|
|
92
101
|
while (true) {
|
|
93
102
|
const users = yield User_model_1.Users.find({})
|
|
94
103
|
.skip(processedCount)
|
|
95
|
-
.limit(BATCH_SIZE)
|
|
96
|
-
.lean();
|
|
104
|
+
.limit(BATCH_SIZE);
|
|
97
105
|
if (users.length === 0)
|
|
98
106
|
break;
|
|
99
107
|
// Update rank for each user in the batch
|
|
100
108
|
yield Promise.all(users.map((user) => __awaiter(void 0, void 0, void 0, function* () {
|
|
101
109
|
try {
|
|
102
|
-
|
|
110
|
+
const projectsCount = user.projectsCount;
|
|
111
|
+
const projectsLiked = user.likes;
|
|
112
|
+
const acceptedProjectsCounter = user.acceptedProjectsCounter;
|
|
113
|
+
console.log('acceptedProjectsCounter', acceptedProjectsCounter);
|
|
114
|
+
console.log('projectsLiked', projectsLiked);
|
|
115
|
+
console.log('projectsCount', projectsCount);
|
|
116
|
+
// Find current rank that matches ALL criteria
|
|
117
|
+
const currentRank = yield ranks_model_1.Rank.findOne({
|
|
118
|
+
actionCount: { $lte: acceptedProjectsCounter },
|
|
119
|
+
projectsLiked: { $lte: projectsLiked },
|
|
120
|
+
projectsCount: { $lte: projectsCount }
|
|
121
|
+
})
|
|
122
|
+
.sort({
|
|
123
|
+
actionCount: -1,
|
|
124
|
+
favoriteCount: -1,
|
|
125
|
+
projectsLiked: -1,
|
|
126
|
+
projectsCount: -1
|
|
127
|
+
})
|
|
128
|
+
.exec();
|
|
129
|
+
console.log('currentRank', currentRank);
|
|
130
|
+
// Find next possible rank
|
|
131
|
+
const nextRank = yield ranks_model_1.Rank.findOne({
|
|
132
|
+
$or: [
|
|
133
|
+
{ actionCount: { $gt: acceptedProjectsCounter } },
|
|
134
|
+
{ projectsLiked: { $gt: projectsLiked } },
|
|
135
|
+
{ projectsCount: { $gt: projectsCount } }
|
|
136
|
+
]
|
|
137
|
+
})
|
|
138
|
+
.sort({
|
|
139
|
+
actionCount: 1,
|
|
140
|
+
favoriteCount: 1,
|
|
141
|
+
projectsLiked: 1,
|
|
142
|
+
projectsCount: 1
|
|
143
|
+
})
|
|
144
|
+
.exec();
|
|
145
|
+
console.log('nextRank', nextRank);
|
|
146
|
+
if (currentRank) {
|
|
147
|
+
user.rank.title = currentRank.rank;
|
|
148
|
+
user.rank.color = currentRank.color;
|
|
149
|
+
if (nextRank) {
|
|
150
|
+
// Calculate progress based on the most relevant criterion
|
|
151
|
+
const criteriaProgress = [
|
|
152
|
+
{
|
|
153
|
+
completed: acceptedProjectsCounter - currentRank.actionCount,
|
|
154
|
+
needed: nextRank.actionCount - currentRank.actionCount
|
|
155
|
+
},
|
|
156
|
+
{
|
|
157
|
+
completed: projectsLiked - currentRank.projectsLiked,
|
|
158
|
+
needed: nextRank.projectsLiked - currentRank.projectsLiked
|
|
159
|
+
},
|
|
160
|
+
{
|
|
161
|
+
completed: projectsCount - currentRank.projectsCount,
|
|
162
|
+
needed: nextRank.projectsCount - currentRank.projectsCount
|
|
163
|
+
}
|
|
164
|
+
];
|
|
165
|
+
// Find the criterion with the highest progress percentage
|
|
166
|
+
const progress = criteriaProgress
|
|
167
|
+
.filter(c => c.needed > 0)
|
|
168
|
+
.map(c => (c.completed / c.needed) * 100);
|
|
169
|
+
user.rank.nextRangPercentage = progress.length ? Math.max(...progress) : 0;
|
|
170
|
+
user.rank.nextRankTitle = nextRank.rank;
|
|
171
|
+
}
|
|
172
|
+
else {
|
|
173
|
+
user.rank.nextRangPercentage = 100;
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
else {
|
|
177
|
+
user.rank.title = null;
|
|
178
|
+
user.rank.nextRangPercentage = 0;
|
|
179
|
+
user.rank.nextRankTitle = null;
|
|
180
|
+
user.rank.color = null;
|
|
181
|
+
}
|
|
182
|
+
yield user.save();
|
|
183
|
+
return user;
|
|
103
184
|
}
|
|
104
185
|
catch (error) {
|
|
105
186
|
console.error(`Failed to update rank for user ${user._id}:`, error);
|