Initial commit
This commit is contained in:
@@ -4,6 +4,10 @@ const ProjectModel = require('../../../models/ProjectModel')
|
||||
const UserModel = require('../../../models/UserModel')
|
||||
const { check, validationResult } = require('express-validator')
|
||||
const jwt = require('jsonwebtoken')
|
||||
const {
|
||||
canCreateProject,
|
||||
respondProjectProfileIncomplete
|
||||
} = require('../../../utils/projectProfile')
|
||||
const createProjectValidationRules = () => {
|
||||
return [
|
||||
check('title').notEmpty().withMessage('عنوان نمیتواند خالی باشد'),
|
||||
@@ -59,11 +63,8 @@ const createProject = async (req, res, next) => {
|
||||
message: 'شما دسترسی به این بخش ندارید'
|
||||
})
|
||||
}
|
||||
if (user.is_verified !== 'verified') {
|
||||
return res.status(422).json({
|
||||
error: true,
|
||||
message: 'مدارک شما تایید نشده است'
|
||||
})
|
||||
if (!canCreateProject(user)) {
|
||||
return respondProjectProfileIncomplete(res)
|
||||
}
|
||||
// اعتبارسنجی درخواست
|
||||
const errors = validationResult(req)
|
||||
|
||||
@@ -24,7 +24,7 @@ const requestProject = async (req, res, next) => {
|
||||
if (!token) return res.status(401).send('Access Denied')
|
||||
const decodedToken = jwt.verify(token, process.env.APP_SECRET)
|
||||
const user = await UserModel.findById(decodedToken.id)
|
||||
if (!user || user.user_type !== 'user') {
|
||||
if (!user) {
|
||||
return res.status(422).json({
|
||||
error: true, message: 'شما دسترسی به این بخش ندارید'
|
||||
})
|
||||
@@ -47,6 +47,21 @@ const requestProject = async (req, res, next) => {
|
||||
message: 'اطلاعات ارسالی اشتباه است'
|
||||
})
|
||||
}
|
||||
|
||||
const project = await ProjectModel.findById(project_id)
|
||||
if (!project) {
|
||||
return res.status(404).json({
|
||||
error: true,
|
||||
message: 'پروژه یافت نشد'
|
||||
})
|
||||
}
|
||||
if (String(project.creator_id) === String(user._id)) {
|
||||
return res.status(422).json({
|
||||
error: true,
|
||||
message: 'نمیتوانید برای پروژه خودتان درخواست ارسال کنید'
|
||||
})
|
||||
}
|
||||
|
||||
// چک کردن برای وجود درخواست قبلی
|
||||
const existingRequest = await RequestModel.findOne({ project: project_id, user: user._id })
|
||||
if (existingRequest) {
|
||||
@@ -65,7 +80,6 @@ const requestProject = async (req, res, next) => {
|
||||
await request.save()
|
||||
|
||||
// ثبت اعلان برای سازنده پروژه
|
||||
const project = await ProjectModel.findById(project_id)
|
||||
const creatorId = project.creator_id
|
||||
|
||||
const notification = new NotificationModel({
|
||||
@@ -130,7 +144,7 @@ const editRequestProject = async (req, res, next) => {
|
||||
if (!token) return res.status(401).send('Access Denied')
|
||||
const decodedToken = jwt.verify(token, process.env.APP_SECRET)
|
||||
const user = await UserModel.findById(decodedToken.id)
|
||||
if (!user || user.user_type !== 'user') {
|
||||
if (!user) {
|
||||
return res.status(422).json({
|
||||
error: true, message: 'شما دسترسی به این بخش ندارید'
|
||||
})
|
||||
|
||||
@@ -10,6 +10,10 @@ const {
|
||||
resolveRatingForComment,
|
||||
recalculateUserRating
|
||||
} = require('../../../utils/commentRating')
|
||||
const {
|
||||
canCreateProject,
|
||||
respondProjectProfileIncomplete
|
||||
} = require('../../../utils/projectProfile')
|
||||
|
||||
const saveProjectComment = async ({
|
||||
targetUserId,
|
||||
@@ -19,12 +23,16 @@ const saveProjectComment = async ({
|
||||
rate,
|
||||
commentFor
|
||||
}) => {
|
||||
const { ratingValue, isNewRating, requiresRating } = await resolveRatingForComment({
|
||||
const { ratingValue, isNewRating, requiresRating, alreadyRated } = await resolveRatingForComment({
|
||||
creatorId,
|
||||
targetUserId,
|
||||
rate
|
||||
})
|
||||
|
||||
if (alreadyRated) {
|
||||
return { error: { status: 400, message: 'شما قبلاً به این کاربر امتیاز دادهاید.' } }
|
||||
}
|
||||
|
||||
if (requiresRating) {
|
||||
return { error: { status: 400, message: 'Rating must be between 1 and 5' } }
|
||||
}
|
||||
@@ -256,11 +264,8 @@ const editProject = async (req, res, next) => {
|
||||
message: 'شما دسترسی به این بخش ندارید'
|
||||
})
|
||||
}
|
||||
if (user.is_verified !== 'verified') {
|
||||
return res.status(422).json({
|
||||
error: true,
|
||||
message: 'مدارک شما تایید نشده است'
|
||||
})
|
||||
if (!canCreateProject(user)) {
|
||||
return respondProjectProfileIncomplete(res)
|
||||
}
|
||||
|
||||
const errors = validationResult(req)
|
||||
|
||||
Reference in New Issue
Block a user