@duvdu-v1/duvdu 1.1.243 → 1.1.246
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,8 +12,11 @@ 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;
|
|
@@ -33,7 +36,6 @@ const updateRankForUser = (user) => __awaiter(void 0, void 0, void 0, function*
|
|
|
33
36
|
projectsCount: -1
|
|
34
37
|
})
|
|
35
38
|
.exec();
|
|
36
|
-
console.log('currentRank', currentRank);
|
|
37
39
|
// Find next possible rank
|
|
38
40
|
const nextRank = yield ranks_model_1.Rank.findOne({
|
|
39
41
|
$or: [
|
|
@@ -49,7 +51,6 @@ const updateRankForUser = (user) => __awaiter(void 0, void 0, void 0, function*
|
|
|
49
51
|
projectsCount: 1
|
|
50
52
|
})
|
|
51
53
|
.exec();
|
|
52
|
-
console.log('nextRank', nextRank);
|
|
53
54
|
if (currentRank) {
|
|
54
55
|
user.rank.title = currentRank.rank;
|
|
55
56
|
user.rank.color = currentRank.color;
|
|
@@ -73,6 +74,7 @@ const updateRankForUser = (user) => __awaiter(void 0, void 0, void 0, function*
|
|
|
73
74
|
const progress = criteriaProgress
|
|
74
75
|
.filter(c => c.needed > 0)
|
|
75
76
|
.map(c => (c.completed / c.needed) * 100);
|
|
77
|
+
console.log('progress', progress);
|
|
76
78
|
user.rank.nextRangPercentage = progress.length ? Math.max(...progress) : 0;
|
|
77
79
|
user.rank.nextRankTitle = nextRank.rank;
|
|
78
80
|
}
|
|
@@ -98,14 +100,87 @@ const recalculateAllUsersRanks = () => __awaiter(void 0, void 0, void 0, functio
|
|
|
98
100
|
while (true) {
|
|
99
101
|
const users = yield User_model_1.Users.find({})
|
|
100
102
|
.skip(processedCount)
|
|
101
|
-
.limit(BATCH_SIZE)
|
|
102
|
-
.lean();
|
|
103
|
+
.limit(BATCH_SIZE);
|
|
103
104
|
if (users.length === 0)
|
|
104
105
|
break;
|
|
105
106
|
// Update rank for each user in the batch
|
|
106
107
|
yield Promise.all(users.map((user) => __awaiter(void 0, void 0, void 0, function* () {
|
|
107
108
|
try {
|
|
108
|
-
|
|
109
|
+
const projectsCount = user.projectsCount;
|
|
110
|
+
const projectsLiked = user.likes;
|
|
111
|
+
const acceptedProjectsCounter = user.acceptedProjectsCounter;
|
|
112
|
+
console.log('acceptedProjectsCounter', acceptedProjectsCounter);
|
|
113
|
+
console.log('projectsLiked', projectsLiked);
|
|
114
|
+
console.log('projectsCount', projectsCount);
|
|
115
|
+
// Find current rank that matches ALL criteria
|
|
116
|
+
const currentRank = yield ranks_model_1.Rank.findOne({
|
|
117
|
+
actionCount: { $lte: acceptedProjectsCounter },
|
|
118
|
+
projectsLiked: { $lte: projectsLiked },
|
|
119
|
+
projectsCount: { $lte: projectsCount }
|
|
120
|
+
})
|
|
121
|
+
.sort({
|
|
122
|
+
actionCount: -1,
|
|
123
|
+
favoriteCount: -1,
|
|
124
|
+
projectsLiked: -1,
|
|
125
|
+
projectsCount: -1
|
|
126
|
+
})
|
|
127
|
+
.exec();
|
|
128
|
+
// Find next possible rank
|
|
129
|
+
const nextRank = yield ranks_model_1.Rank.findOne({
|
|
130
|
+
$or: [
|
|
131
|
+
{ actionCount: { $gt: acceptedProjectsCounter } },
|
|
132
|
+
{ projectsLiked: { $gt: projectsLiked } },
|
|
133
|
+
{ projectsCount: { $gt: projectsCount } }
|
|
134
|
+
]
|
|
135
|
+
})
|
|
136
|
+
.sort({
|
|
137
|
+
actionCount: 1,
|
|
138
|
+
favoriteCount: 1,
|
|
139
|
+
projectsLiked: 1,
|
|
140
|
+
projectsCount: 1
|
|
141
|
+
})
|
|
142
|
+
.exec();
|
|
143
|
+
if (currentRank) {
|
|
144
|
+
user.rank.title = currentRank.rank;
|
|
145
|
+
user.rank.color = currentRank.color;
|
|
146
|
+
if (nextRank) {
|
|
147
|
+
// Calculate progress based on the most relevant criterion
|
|
148
|
+
const criteriaProgress = [
|
|
149
|
+
{
|
|
150
|
+
completed: acceptedProjectsCounter - currentRank.actionCount,
|
|
151
|
+
needed: nextRank.actionCount - currentRank.actionCount
|
|
152
|
+
},
|
|
153
|
+
{
|
|
154
|
+
completed: projectsLiked - currentRank.projectsLiked,
|
|
155
|
+
needed: nextRank.projectsLiked - currentRank.projectsLiked
|
|
156
|
+
},
|
|
157
|
+
{
|
|
158
|
+
completed: projectsCount - currentRank.projectsCount,
|
|
159
|
+
needed: nextRank.projectsCount - currentRank.projectsCount
|
|
160
|
+
}
|
|
161
|
+
];
|
|
162
|
+
console.log('criteriaProgress', criteriaProgress);
|
|
163
|
+
// Find the criterion with the highest progress percentage
|
|
164
|
+
const progress = criteriaProgress
|
|
165
|
+
.filter(c => c.needed > 0)
|
|
166
|
+
.map(c => (c.completed / c.needed) * 100);
|
|
167
|
+
console.log('progress', progress);
|
|
168
|
+
console.log('progress.length', progress.length ? Math.max(...progress) : 0);
|
|
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;
|
|
109
184
|
}
|
|
110
185
|
catch (error) {
|
|
111
186
|
console.error(`Failed to update rank for user ${user._id}:`, error);
|