full
This commit is contained in:
@@ -7,7 +7,7 @@ const findExistingUserRating = (creatorId, targetUserId) => {
|
||||
return CommentModel.findOne({
|
||||
creator: creatorId,
|
||||
user: targetUserId,
|
||||
rating: RATED_FILTER
|
||||
rating: RATED_FILTER,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -31,28 +31,25 @@ const resolveRatingForComment = async ({ creatorId, targetUserId, rate }) => {
|
||||
}
|
||||
|
||||
if (!rate || rate < 1 || rate > 5) {
|
||||
return { ratingValue: null, isNewRating: false, requiresRating: true }
|
||||
return { ratingValue: null, isNewRating: false, requiresRating: false }
|
||||
}
|
||||
|
||||
return { ratingValue: rate, isNewRating: true }
|
||||
}
|
||||
|
||||
const getUserLevel = (user, totalRating) => {
|
||||
if (user.expertise === 'مدل') {
|
||||
if (totalRating <= 50) return 'تازه وارد'
|
||||
if (totalRating <= 100) return 'استاندارد'
|
||||
if (totalRating <= 300) return 'حرفهای'
|
||||
/** rate = مجموع ستارههای دریافتی | user_score = میانگین ۱ تا ۵ */
|
||||
const getUserLevel = (user, totalStars) => {
|
||||
if (totalStars <= 500) return 'تازه وارد'
|
||||
if (totalStars <= 2000) return 'استاندارد'
|
||||
if (totalStars <= 10000) return 'حرفهای'
|
||||
|
||||
if (user.expertise === 'مدل') return 'سپورمدل'
|
||||
|
||||
if (['زیبایی', 'عکاس', 'آرایشگر'].includes(user.expertise)) {
|
||||
return 'استاد'
|
||||
}
|
||||
|
||||
if (['زیبایی', 'عکاس'].includes(user.expertise)) {
|
||||
if (totalRating <= 50) return 'تازه وارد'
|
||||
if (totalRating <= 100) return 'استاندارد'
|
||||
if (totalRating <= 300) return 'حرفهای'
|
||||
return 'استاد'
|
||||
}
|
||||
|
||||
return user.user_level
|
||||
return 'استاد'
|
||||
}
|
||||
|
||||
const recalculateUserRating = async (targetUserId) => {
|
||||
@@ -61,7 +58,7 @@ const recalculateUserRating = async (targetUserId) => {
|
||||
|
||||
const ratedComments = await CommentModel.find({
|
||||
user: targetUserId,
|
||||
rating: RATED_FILTER
|
||||
rating: RATED_FILTER,
|
||||
}).sort({ createdAt: 1 })
|
||||
|
||||
const uniqueRatingsByCreator = new Map()
|
||||
@@ -73,12 +70,13 @@ const recalculateUserRating = async (targetUserId) => {
|
||||
}
|
||||
|
||||
const ratings = Array.from(uniqueRatingsByCreator.values())
|
||||
const totalRating = ratings.reduce((acc, rating) => acc + rating, 0)
|
||||
const totalStars = ratings.reduce((acc, rating) => acc + rating, 0)
|
||||
const ratedCount = ratings.length
|
||||
|
||||
user.user_score = totalRating
|
||||
user.user_level = getUserLevel(user, totalRating)
|
||||
user.rate = ratedCount > 0 ? (totalRating / ratedCount).toFixed(1) : '0'
|
||||
user.rate = String(totalStars)
|
||||
user.user_score =
|
||||
ratedCount > 0 ? (totalStars / ratedCount).toFixed(1) : '0'
|
||||
user.user_level = getUserLevel(user, totalStars)
|
||||
await user.save()
|
||||
}
|
||||
|
||||
@@ -87,5 +85,6 @@ module.exports = {
|
||||
findExistingUserRating,
|
||||
hasCreatorRatedUser,
|
||||
resolveRatingForComment,
|
||||
recalculateUserRating
|
||||
recalculateUserRating,
|
||||
getUserLevel,
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user