full
This commit is contained in:
145
controllers/panel/comments/academyCommentController.js
Normal file
145
controllers/panel/comments/academyCommentController.js
Normal file
@@ -0,0 +1,145 @@
|
||||
const CourseComment = require('../../../models/CourseComentModel')
|
||||
const CourseModel = require('../../../models/CourseModel')
|
||||
const UserModel = require('../../../models/UserModel')
|
||||
const jMoment = require('moment-jalaali')
|
||||
|
||||
const getAcademyComments = async (req, res, next) => {
|
||||
try {
|
||||
const { page = 1, limit = 20, startDate, endDate, status } = req.query
|
||||
const filter = {}
|
||||
|
||||
if (startDate || endDate) {
|
||||
filter.createdAt = {}
|
||||
if (startDate) filter.createdAt.$gte = new Date(startDate)
|
||||
if (endDate) filter.createdAt.$lte = new Date(endDate)
|
||||
}
|
||||
if (status) filter.status = status
|
||||
|
||||
const comments = await CourseComment.paginate(filter, {
|
||||
page: parseInt(page, 10),
|
||||
limit: parseInt(limit, 10),
|
||||
sort: { createdAt: -1 },
|
||||
})
|
||||
|
||||
const courseIds = [...new Set(comments.docs.map((c) => String(c.course_id)).filter(Boolean))]
|
||||
const userIds = [...new Set(comments.docs.map((c) => String(c.user_id)).filter(Boolean))]
|
||||
|
||||
const [courses, users] = await Promise.all([
|
||||
CourseModel.find({ _id: { $in: courseIds } }).select('cuorse_name user_id').lean(),
|
||||
UserModel.find({ _id: { $in: userIds } }).select('user_name first_name last_name profile_image').lean(),
|
||||
])
|
||||
|
||||
const courseMap = new Map(courses.map((c) => [String(c._id), c]))
|
||||
const userMap = new Map(users.map((u) => [String(u._id), u]))
|
||||
|
||||
const ownerIds = [...new Set(courses.map((c) => String(c.user_id)).filter(Boolean))]
|
||||
const owners = await UserModel.find({ _id: { $in: ownerIds } })
|
||||
.select('user_name first_name last_name')
|
||||
.lean()
|
||||
const ownerMap = new Map(owners.map((u) => [String(u._id), u]))
|
||||
|
||||
const commentList = comments.docs.map((comment) => {
|
||||
const course = courseMap.get(String(comment.course_id))
|
||||
const user = userMap.get(String(comment.user_id))
|
||||
const creator = course ? ownerMap.get(String(course.user_id)) : null
|
||||
|
||||
return {
|
||||
_id: comment._id,
|
||||
createdAt: jMoment(comment.createdAt).format('jYYYY-jMM-jDD HH:mm'),
|
||||
status: comment.status,
|
||||
comment: comment.comment,
|
||||
rating: comment.rate,
|
||||
course: course?.cuorse_name || '-',
|
||||
user: user
|
||||
? {
|
||||
_id: user._id,
|
||||
user_name: user.user_name,
|
||||
first_name: user.first_name,
|
||||
last_name: user.last_name,
|
||||
}
|
||||
: null,
|
||||
creator: creator
|
||||
? {
|
||||
_id: creator._id,
|
||||
user_name: creator.user_name,
|
||||
first_name: creator.first_name,
|
||||
last_name: creator.last_name,
|
||||
}
|
||||
: null,
|
||||
}
|
||||
})
|
||||
|
||||
res.status(200).json({
|
||||
comments: commentList,
|
||||
totalPages: comments.totalPages,
|
||||
totalItems: comments.totalDocs,
|
||||
})
|
||||
} catch (error) {
|
||||
next(error)
|
||||
}
|
||||
}
|
||||
|
||||
const getAcademyCommentDetail = async (req, res, next) => {
|
||||
try {
|
||||
const { comment_id: commentId } = req.query
|
||||
if (!commentId) {
|
||||
return res.status(422).json({ error: true, message: 'شناسه کامنت الزامی است' })
|
||||
}
|
||||
|
||||
const comment = await CourseComment.findById(commentId).lean()
|
||||
if (!comment) {
|
||||
return res.status(404).json({ error: true, message: 'کامنت یافت نشد' })
|
||||
}
|
||||
|
||||
const [course, user] = await Promise.all([
|
||||
CourseModel.findById(comment.course_id).select('cuorse_name user_id').lean(),
|
||||
UserModel.findById(comment.user_id).select('user_name first_name last_name profile_image').lean(),
|
||||
])
|
||||
const creator = course?.user_id
|
||||
? await UserModel.findById(course.user_id).select('user_name first_name last_name').lean()
|
||||
: null
|
||||
|
||||
res.status(200).json({
|
||||
comment: {
|
||||
_id: comment._id,
|
||||
comment: comment.comment,
|
||||
rating: comment.rate,
|
||||
status: comment.status,
|
||||
createdAt: jMoment(comment.createdAt).format('jYYYY-jMM-jDD HH:mm'),
|
||||
course: course?.cuorse_name || '-',
|
||||
user,
|
||||
creator,
|
||||
},
|
||||
})
|
||||
} catch (error) {
|
||||
next(error)
|
||||
}
|
||||
}
|
||||
|
||||
const acceptAcademyComment = async (req, res, next) => {
|
||||
try {
|
||||
const { id, status, text } = req.body
|
||||
if (!id || !status) {
|
||||
return res.status(422).json({ error: true, message: 'اطلاعات ارسالی اشتباه است' })
|
||||
}
|
||||
|
||||
const comment = await CourseComment.findById(id)
|
||||
if (!comment) {
|
||||
return res.status(404).json({ error: true, message: 'کامنت یافت نشد' })
|
||||
}
|
||||
|
||||
if (text) comment.comment = text
|
||||
comment.status = status
|
||||
await comment.save()
|
||||
|
||||
res.status(201).json({ message: 'وضعیت کامنت با موفقیت بهروزرسانی شد' })
|
||||
} catch (error) {
|
||||
next(error)
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
getAcademyComments,
|
||||
getAcademyCommentDetail,
|
||||
acceptAcademyComment,
|
||||
}
|
||||
Reference in New Issue
Block a user