@duvdu-v1/duvdu 1.1.225 → 1.1.226
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.
|
@@ -87,8 +87,10 @@ const userSchema = new mongoose_1.Schema({
|
|
|
87
87
|
.index({ location: '2dsphere' });
|
|
88
88
|
userSchema.pre('save', function (next) {
|
|
89
89
|
return __awaiter(this, void 0, void 0, function* () {
|
|
90
|
-
if (this.isModified('acceptedProjectsCounter')
|
|
90
|
+
if (this.isModified('acceptedProjectsCounter') ||
|
|
91
|
+
this.isModified('likes')) {
|
|
91
92
|
yield (0, rank_service_1.updateRankForUser)(this);
|
|
93
|
+
}
|
|
92
94
|
next();
|
|
93
95
|
});
|
|
94
96
|
});
|
|
@@ -26,6 +26,9 @@
|
|
|
26
26
|
export interface Irank {
|
|
27
27
|
actionCount: number;
|
|
28
28
|
rank: string;
|
|
29
|
+
favoriteCount: number;
|
|
30
|
+
projectsLiked: number;
|
|
31
|
+
projectsCount: number;
|
|
29
32
|
color: string;
|
|
30
33
|
}
|
|
31
34
|
export declare const Rank: import("mongoose").Model<Irank, {}, {}, {}, import("mongoose").Document<unknown, {}, Irank> & Irank & {
|
|
@@ -7,4 +7,7 @@ exports.Rank = (0, mongoose_1.model)(model_names_1.MODELS.rank, new mongoose_1.S
|
|
|
7
7
|
actionCount: { type: Number, unique: true, default: 0 },
|
|
8
8
|
rank: { type: String, unique: true, default: null },
|
|
9
9
|
color: { type: String, default: null },
|
|
10
|
+
favoriteCount: { type: Number, default: 0 },
|
|
11
|
+
projectsLiked: { type: Number, default: 0 },
|
|
12
|
+
projectsCount: { type: Number, default: 0 },
|
|
10
13
|
}, { timestamps: true, collection: model_names_1.MODELS.rank }));
|
|
@@ -10,21 +10,73 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
10
10
|
};
|
|
11
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
12
|
exports.updateRankForUser = void 0;
|
|
13
|
+
const allProjects_model_1 = require("../models/allProjects.model");
|
|
14
|
+
const favourites_model_1 = require("../models/favourites.model");
|
|
13
15
|
const ranks_model_1 = require("../models/ranks.model");
|
|
14
16
|
const updateRankForUser = (user) => __awaiter(void 0, void 0, void 0, function* () {
|
|
15
|
-
|
|
16
|
-
|
|
17
|
+
// get all user stats
|
|
18
|
+
const projectsCount = yield allProjects_model_1.Project.countDocuments({ user: user._id });
|
|
19
|
+
const favoriteCount = yield favourites_model_1.Favourites.countDocuments({ user: user._id });
|
|
20
|
+
const projectsLiked = user.likes;
|
|
21
|
+
const acceptedProjectsCounter = user.acceptedProjectsCounter;
|
|
22
|
+
// Find current rank that matches ALL criteria
|
|
23
|
+
const currentRank = yield ranks_model_1.Rank.findOne({
|
|
24
|
+
actionCount: { $lte: acceptedProjectsCounter },
|
|
25
|
+
favoriteCount: { $lte: favoriteCount },
|
|
26
|
+
projectsLiked: { $lte: projectsLiked },
|
|
27
|
+
projectsCount: { $lte: projectsCount }
|
|
28
|
+
})
|
|
29
|
+
.sort({
|
|
30
|
+
actionCount: -1,
|
|
31
|
+
favoriteCount: -1,
|
|
32
|
+
projectsLiked: -1,
|
|
33
|
+
projectsCount: -1
|
|
34
|
+
})
|
|
17
35
|
.exec();
|
|
18
|
-
|
|
19
|
-
|
|
36
|
+
// Find next possible rank
|
|
37
|
+
const nextRank = yield ranks_model_1.Rank.findOne({
|
|
38
|
+
$or: [
|
|
39
|
+
{ actionCount: { $gt: acceptedProjectsCounter } },
|
|
40
|
+
{ favoriteCount: { $gt: favoriteCount } },
|
|
41
|
+
{ projectsLiked: { $gt: projectsLiked } },
|
|
42
|
+
{ projectsCount: { $gt: projectsCount } }
|
|
43
|
+
]
|
|
44
|
+
})
|
|
45
|
+
.sort({
|
|
46
|
+
actionCount: 1,
|
|
47
|
+
favoriteCount: 1,
|
|
48
|
+
projectsLiked: 1,
|
|
49
|
+
projectsCount: 1
|
|
50
|
+
})
|
|
20
51
|
.exec();
|
|
21
52
|
if (currentRank) {
|
|
22
53
|
user.rank.title = currentRank.rank;
|
|
23
54
|
user.rank.color = currentRank.color;
|
|
24
55
|
if (nextRank) {
|
|
25
|
-
|
|
26
|
-
const
|
|
27
|
-
|
|
56
|
+
// Calculate progress based on the most relevant criterion
|
|
57
|
+
const criteriaProgress = [
|
|
58
|
+
{
|
|
59
|
+
completed: acceptedProjectsCounter - currentRank.actionCount,
|
|
60
|
+
needed: nextRank.actionCount - currentRank.actionCount
|
|
61
|
+
},
|
|
62
|
+
{
|
|
63
|
+
completed: favoriteCount - currentRank.favoriteCount,
|
|
64
|
+
needed: nextRank.favoriteCount - currentRank.favoriteCount
|
|
65
|
+
},
|
|
66
|
+
{
|
|
67
|
+
completed: projectsLiked - currentRank.projectsLiked,
|
|
68
|
+
needed: nextRank.projectsLiked - currentRank.projectsLiked
|
|
69
|
+
},
|
|
70
|
+
{
|
|
71
|
+
completed: projectsCount - currentRank.projectsCount,
|
|
72
|
+
needed: nextRank.projectsCount - currentRank.projectsCount
|
|
73
|
+
}
|
|
74
|
+
];
|
|
75
|
+
// Find the criterion with the highest progress percentage
|
|
76
|
+
const progress = criteriaProgress
|
|
77
|
+
.filter(c => c.needed > 0)
|
|
78
|
+
.map(c => (c.completed / c.needed) * 100);
|
|
79
|
+
user.rank.nextRangPercentage = progress.length ? Math.max(...progress) : 0;
|
|
28
80
|
user.rank.nextRankTitle = nextRank.rank;
|
|
29
81
|
}
|
|
30
82
|
else {
|