@duvdu-v1/duvdu 1.1.243 → 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,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;
|
|
@@ -98,14 +101,86 @@ const recalculateAllUsersRanks = () => __awaiter(void 0, void 0, void 0, functio
|
|
|
98
101
|
while (true) {
|
|
99
102
|
const users = yield User_model_1.Users.find({})
|
|
100
103
|
.skip(processedCount)
|
|
101
|
-
.limit(BATCH_SIZE)
|
|
102
|
-
.lean();
|
|
104
|
+
.limit(BATCH_SIZE);
|
|
103
105
|
if (users.length === 0)
|
|
104
106
|
break;
|
|
105
107
|
// Update rank for each user in the batch
|
|
106
108
|
yield Promise.all(users.map((user) => __awaiter(void 0, void 0, void 0, function* () {
|
|
107
109
|
try {
|
|
108
|
-
|
|
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;
|
|
109
184
|
}
|
|
110
185
|
catch (error) {
|
|
111
186
|
console.error(`Failed to update rank for user ${user._id}:`, error);
|