Initial commit

This commit is contained in:
payacom
2026-07-08 21:01:30 +03:30
parent 59c7c8e11c
commit 0b01b262fa
38 changed files with 1415 additions and 288 deletions

View File

@@ -6,6 +6,46 @@ const { check, validationResult } = require('express-validator')
const { ProvinceModel, CityModel } = require('../../../models/StateCity')
const NotificationModel = require('../../../models/NotificationModel')
const CommentModel = require('../../../models/CommentModel')
const {
resolveRatingForComment,
recalculateUserRating
} = require('../../../utils/commentRating')
const saveProjectComment = async ({
targetUserId,
projectId,
creatorId,
comment,
rate,
commentFor
}) => {
const { ratingValue, isNewRating, requiresRating } = await resolveRatingForComment({
creatorId,
targetUserId,
rate
})
if (requiresRating) {
return { error: { status: 400, message: 'Rating must be between 1 and 5' } }
}
const newComment = new CommentModel({
user: targetUserId,
project: projectId,
creator: creatorId,
rating: ratingValue,
comment,
comment_for: commentFor,
status: 'pending'
})
await newComment.save()
if (isNewRating && commentFor === 'user') {
await recalculateUserRating(targetUserId)
}
return { ratingValue, isNewRating }
}
const doneProject = async (req, res, next) => {
try {
@@ -16,7 +56,7 @@ const doneProject = async (req, res, next) => {
const userId = decodedToken.id
const { project_id, comment, rate, user_id } = req.body
if (!project_id || !comment || !rate || !user_id) {
if (!project_id || !comment || !user_id) {
return res.status(400).send({ message: 'All fields are required' })
}
@@ -42,43 +82,20 @@ const doneProject = async (req, res, next) => {
project.status = 'done'
await project.save()
const newComment = new CommentModel({
user: user_id,
project: project_id,
creator: userId,
rating: rate,
const commentResult = await saveProjectComment({
targetUserId: user_id,
projectId: project_id,
creatorId: userId,
comment,
comment_for: 'user',
status: 'pending'
rate,
commentFor: 'user'
})
await newComment.save()
const user = await UserModel.findById(user_id)
// پیدا کردن کامنت‌ها و محاسبه‌ی امتیاز کل
const comments = await CommentModel.find({ user: user_id, comment_for: 'user' })
const totalRating = Number(comments.reduce((acc, comment) => acc + comment.rating, 0))
let userLevel
if (user.expertise === 'مدل') {
if (totalRating <= 50) userLevel = 'تازه وارد'
else if (totalRating <= 100) userLevel = 'استاندارد'
else if (totalRating <= 300) userLevel = 'حرفه‌ای'
else userLevel = 'استاد'
} else if (['زیبایی', 'عکاس'].includes(user.expertise)) {
if (totalRating <= 50) userLevel = 'تازه وارد'
else if (totalRating <= 100) userLevel = 'استاندارد'
else if (totalRating <= 300) userLevel = 'حرفه‌ای'
else userLevel = 'استاد'
if (commentResult.error) {
return res.status(commentResult.error.status).send({ message: commentResult.error.message })
}
user.user_score = totalRating
user.user_level = userLevel
const totalComments = await CommentModel.countDocuments({ user: user_id, comment_for: 'user' })
const averageRating = totalRating / totalComments
user.rate = averageRating.toFixed(1)
await user.save()
const user = await UserModel.findById(user_id)
const notification = new NotificationModel({
user_id: user._id,
@@ -105,7 +122,7 @@ const doneProjectWeb = async (req, res, next) => {
const userId = decodedToken.id
const { project_id, comment, rate, user_id } = req.body
if (!project_id || !comment || !rate || !user_id) {
if (!project_id || !comment || !user_id) {
return res.status(400).send({ message: 'All fields are required' })
}
@@ -117,43 +134,20 @@ const doneProjectWeb = async (req, res, next) => {
project.status = 'done'
await project.save()
const newComment = new CommentModel({
user: user_id,
project: project_id,
creator: userId,
rating: rate,
const commentResult = await saveProjectComment({
targetUserId: user_id,
projectId: project_id,
creatorId: userId,
comment,
comment_for: 'user',
status: 'pending'
rate,
commentFor: 'user'
})
await newComment.save()
const user = await UserModel.findById(user_id)
// پیدا کردن کامنت‌ها و محاسبه‌ی امتیاز کل
const comments = await CommentModel.find({ user: user_id, comment_for: 'user' })
const totalRating = Number(comments.reduce((acc, comment) => acc + comment.rating, 0))
let userLevel
if (user.expertise === 'مدل') {
if (totalRating <= 50) userLevel = 'تازه وارد'
else if (totalRating <= 100) userLevel = 'استاندارد'
else if (totalRating <= 300) userLevel = 'حرفه‌ای'
else userLevel = 'استاد'
} else if (['زیبایی', 'عکاس'].includes(user.expertise)) {
if (totalRating <= 50) userLevel = 'تازه وارد'
else if (totalRating <= 100) userLevel = 'استاندارد'
else if (totalRating <= 300) userLevel = 'حرفه‌ای'
else userLevel = 'استاد'
if (commentResult.error) {
return res.status(commentResult.error.status).send({ message: commentResult.error.message })
}
user.user_score = totalRating
user.user_level = userLevel
const totalComments = await CommentModel.countDocuments({ user: user_id, comment_for: 'user' })
const averageRating = totalRating / totalComments
user.rate = averageRating.toFixed(1)
await user.save()
const user = await UserModel.findById(user_id)
const notification = new NotificationModel({
user_id: user._id,
@@ -182,7 +176,7 @@ const cancleProject = async (req, res, next) => {
const { project_id, comment, rate, user_id } = req.body
if (!project_id || !comment || !rate || !user_id) {
if (!project_id || !comment || !user_id) {
return res.status(400).send({ message: 'All fields are required' })
}
@@ -194,16 +188,18 @@ const cancleProject = async (req, res, next) => {
project.status = 'cancled'
await project.save()
const newComment = new CommentModel({
user: user_id,
project: project_id,
creator: userId,
rating: rate,
const commentResult = await saveProjectComment({
targetUserId: user_id,
projectId: project_id,
creatorId: userId,
comment,
comment_for: 'project',
status: 'pending'
rate,
commentFor: 'project'
})
await newComment.save()
if (commentResult.error) {
return res.status(commentResult.error.status).send({ message: commentResult.error.message })
}
res.status(200).json({ message: 'پروژه با موفقیت تغییر یافت شد' })
} catch (error) {
@@ -363,6 +359,14 @@ const setRateProject = async (req, res, next) => {
if (!project) {
return res.status(403).send({ message: 'Access denied: You are not the creator of this project.' })
}
const alreadyRated = project.ratings.some((item) =>
item.creator_id.toString() === userId.toString()
)
if (alreadyRated) {
return res.status(400).send({ message: 'شما قبلاً به این پروژه امتیاز داده‌اید' })
}
project.ratings.push({
project_id,
rating: rate,