Commit 96c1f46f authored by rlacko's avatar rlacko 💬
Browse files

initial comment system

parent 72284342
......@@ -14,7 +14,7 @@ const pickedKeys = [
'createdAt',
'updatedAt',
'attendance',
'comment',
'comments',
]
// Controller
......@@ -159,7 +159,7 @@ module.exports.removeOne = async function removeOne(req, res) {
})
await Comment.deleteMany({
_id: activity.comment.map(function getCommentIds(element) {
_id: activity.comments.map(function getCommentIds(element) {
return element._id
}),
})
......@@ -183,7 +183,7 @@ module.exports.updateOne = async function updateOne(req, res) {
try {
const activity = await Activity.findOneAndUpdate(
{ _id: req.params.id },
omit(req.body, ['attendance', 'comment']),
omit(req.body, ['attendance', 'comments']),
{ new: true }
)
.lean()
......
......@@ -25,7 +25,7 @@ const ActivitySchema = new mongoose.Schema(
ref: 'attendance',
},
],
comment: [
comments: [
{
type: mongoose.Schema.Types.ObjectId,
ref: 'comment',
......
......@@ -4,15 +4,16 @@ const { isLoggedIn, isMentor } = require('../../middlewares/auth')
const router = Router()
// /api/application
// /api/v1/application
router
.route('/')
.get(isLoggedIn, isMentor, controllers.default.getMany)
.post(isLoggedIn, controllers.default.createOne)
// /api/application/id/:id
// /api/v1/application/id/:id
router.route('/id/:id').get(isLoggedIn, controllers.default.getOneByID)
// /api/application/schacc/:schacc
// /api/v1/application/schacc/:schacc
router
.route('/schacc/:schacc')
.get(isLoggedIn, controllers.default.getOneBySchacc)
......
......@@ -2,7 +2,7 @@ const { crudControllers } = require('../../utils/crud')
const { Attendance } = require('./attendanceModel')
const { pick } = require('lodash')
const pickedKeys = ['_id', 'activity', 'user', 'state', 'comment']
const pickedKeys = ['_id', 'activity', 'user', 'state', 'comments']
module.exports = crudControllers(Attendance, pickedKeys)
......@@ -12,7 +12,7 @@ module.exports.getOne = async (req, res) => {
try {
const attendance = await Attendance.findOne({ _id: req.params.id })
.populate('_user', ['_id', 'fullName', 'schacc'])
.populate('comment', ['creator', 'date', 'text'])
.populate('comments', ['creator', 'date', 'text'])
.lean()
.exec()
......@@ -36,7 +36,7 @@ module.exports.getMany = async (req, res) => {
try {
const attendances = await Attendance.find()
.populate('_user', ['_id', 'fullName', 'schacc'])
.populate('comment', ['creator', 'date', 'text'])
.populate('comments', ['creator', 'date', 'text'])
.lean()
.exec()
......@@ -62,7 +62,7 @@ module.exports.updateOne = async (req, res) => {
{ new: true, runValidators: true }
)
.populate('_user', ['_id', 'fullName', 'schacc'])
.populate('comment', ['creator', 'date', 'text'])
.populate('comments', ['creator', 'date', 'text'])
.lean()
.exec()
......
......@@ -16,7 +16,7 @@ const AttendanceSchema = new mongoose.Schema(
default: 'absent',
required: true,
},
comment: [
comments: [
{
type: mongoose.Schema.Types.ObjectId,
ref: 'comment',
......
const { crudControllers } = require('../../utils/crud')
const { Comment } = require('./commentModel')
const { Solution } = require('../solution/solutionModel')
const { Task } = require('../task/taskModel')
const { Attendance } = require('../attendance/attendanceModel')
const { Activity } = require('../activity/activityModel')
const { omit, pick } = require('lodash')
exports.default = crudControllers(Comment, ['creator', 'text', 'date'])
function invalidIdResponse(res) {
return res
.status(403)
.json({ messages: ['You cannot modify other users comment.'] })
.end()
const pickedKeys = [
'_id',
'parentId',
'parentType',
'creator',
'text',
'createdAt',
'updatedAt',
]
exports.default = crudControllers(Comment, pickedKeys)
function getParentModel(comment) {
switch (comment.parentType) {
case 'solution':
return Solution
case 'task':
return Task
case 'attendance':
return Attendance
case 'activity':
return Activity
default:
throw { message: `Invalid Parent Type: ${comment.parentType}` }
}
}
exports.default.getOne = async (req, res) => {
......@@ -16,72 +38,131 @@ exports.default.getOne = async (req, res) => {
const comment = await Comment.findOne({
_id: req.params.id,
})
.populate('creator', ['fullName', 'nickName'])
.populate('creator', ['fullName', 'nickName', 'schacc'])
.lean()
.exec()
if (comment.creator._id !== req.user._id && req.user.role !== 'mentor') {
return invalidIdResponse(res)
if (
comment.creator.schacc !== req.user.schacc &&
req.user.role !== 'mentor'
) {
if (
comment.parentType == 'attendance' ||
comment.parentType == 'activity'
)
return res
.status(403)
.json({ messages: ['You cannot get other users comment.'] })
.end()
// Check if own solution
if (comment.parentType == 'solution') {
const solution = await Solution.findById(comment.parentId).lean().exec()
if (solution.creator != req.user.schacc)
return res
.status(403)
.json({ messages: ['You cannot get other users comment.'] })
.end()
}
}
if (comment.isAnonim) comment.creator = ''
return res
.status(200)
.json({ data: omit(comment, ['_id', '__v', 'creator._id']) })
.json({ data: pick(comment, pickedKeys) })
.end()
} catch (err) {
res.json(err)
if (err.name == 'ValidationError') {
// Throwed by Mongoose
let messages = []
for (field in err.errors) {
messages.push(err.errors[field].message)
}
return res.status(422).json({ messages })
}
console.error(err)
return res.status(500).json({ message: err.message })
}
}
exports.default.getMany = async (req, res) => {
try {
const comments = await Comment.find()
.populate('_creator', '-_id fullName nickName')
.populate('_creator', 'fullName nickName schacc')
.select(['-__v'])
.lean()
.exec()
return res.status(200).json({ data: comments }).end()
return res
.status(200)
.json({
data: comments.map((e) => {
if (e.isAnonim) e.creator = ''
else e.creator = e._creator
return pick(e, pickedKeys)
}),
})
.end()
} catch (err) {
console.log(err)
res.json(err)
if (err.name == 'ValidationError') {
// Throwed by Mongoose
let messages = []
for (field in err.errors) {
messages.push(err.errors[field].message)
}
return res.status(422).json({ messages })
}
console.error(err)
return res.status(500).json({ message: err.message })
}
}
exports.default.createOne = async (req, res) => {
try {
var comment = await Comment.create({
creator: req.user.schacc,
text: req.body.text,
}).catch((err) => console.log(err))
...req.body,
})
const retComment = await Comment.findById(comment._id)
.populate('_creator', '-_id fullName nickName schacc')
.lean()
.exec()
comment = await comment
.populate('_creator', 'fullName nickName schacc')
.execPopulate()
let retComment = comment.toObject()
retComment.creator = retComment._creator
if (retComment.isAnonim) retComment.creator = ''
return res
.status(200)
.json({
data: pick(retComment, ['_id', 'creator', 'text', 'createdAt']),
data: pick(retComment, pickedKeys),
})
.end()
} catch (err) {
console.log(err)
if (err.name == 'ValidationError') {
// Throwed by Mongoose
let messages = []
for (field in err.errors) {
messages.push(err.errors[field].message)
}
return res.status(422).json({ messages })
}
console.error(err)
return res.status(500).json({ message: err.message })
}
}
exports.default.updateOne = async (req, res) => {
try {
var comment = await Comment.findById(req.params.id).populate('creator')
const comment = await Comment.findById(req.params.id).lean().exec()
if (comment.creator._id !== req.user._id) {
return invalidIdResponse(res)
}
if (comment.creator !== req.user.schacc && req.user.role != 'mentor')
return res
.status(403)
.json({ messages: ['You cannot update other users comment.'] })
.end()
var updatedComment = await Comment.findOneAndUpdate(
const updatedComment = await Comment.findOneAndUpdate(
{ _id: req.params.id },
{ text: req.body.text },
{ new: true }
......@@ -89,31 +170,60 @@ exports.default.updateOne = async (req, res) => {
.lean()
.exec()
return res.status(200).json({ data: updatedComment }).end()
if (updatedComment.isAnonim) updatedComment.creator = ''
return res.status(200).json({ data: pick(updatedComment, pickedKeys) })
} catch (err) {
console.log(err)
if (err.name == 'ValidationError') {
// Throwed by Mongoose
let messages = []
for (field in err.errors) {
messages.push(err.errors[field].message)
}
return res.status(422).json({ messages })
}
console.error(err)
return res.status(500).json({ message: err.message })
}
}
// TODO on delete remove from other models
exports.default.removeOne = async (req, res) => {
try {
const comment = await Comment.findById({ _id: req.params.id })
.populate('creator')
.lean()
.exec()
const comment = await Comment.findById({ _id: req.params.id }).lean().exec()
if (comment.creator._id !== req.user._id) {
return invalidIdResponse(res)
}
if (comment.creator !== req.user.schacc && req.user.role != 'mentor')
return res
.status(403)
.json({ messages: ['You cannot delete other users comment.'] })
.end()
const removed = await Comment.findByIdAndRemove({ _id: req.params.id })
if (!removed) {
return res.status(404).end()
}
return res.status(200).json({ data: comment }).end()
// remove from parent model
await getParentModel(removed).updateMany(
{},
{ $pull: { comments: { $in: removed._id } } }
)
if (removed.isAnonim) removed.creator = ''
return res
.status(200)
.json({ data: pick(removed, pickedKeys) })
.end()
} catch (err) {
console.log(err)
if (err.name == 'ValidationError') {
// Throwed by Mongoose
let messages = []
for (field in err.errors) {
messages.push(err.errors[field].message)
}
return res.status(422).json({ messages })
}
console.error(err)
return res.status(500).json({ message: err.message })
}
}
......@@ -2,6 +2,16 @@ const mongoose = require('mongoose')
const CommentSchema = new mongoose.Schema(
{
parentId: {
// can be an objectId (no ref)
type: mongoose.Schema.Types.ObjectId,
required: true,
},
parentType: {
type: mongoose.Schema.Types.String,
required: true,
enum: ['solution', 'task', 'attendance', 'activity'],
},
creator: {
type: String,
},
......
......@@ -12,7 +12,7 @@ router
// /api/item/:id
router
.route('/:id')
.route('/id/:id')
.get(isLoggedIn, controllers.default.getOne)
.put(isLoggedIn, controllers.default.updateOne)
.delete(isLoggedIn, controllers.default.removeOne)
......
......@@ -27,7 +27,7 @@ exports.default.createOne = async (req, res) => {
// Someone creates a solution
if (req.user.role != 'mentor') {
delete req.body.isAccepted
delete req.body.comment
delete req.body.comments
}
if (task.deadline < new Date())
return res.status(400).json({
......@@ -54,7 +54,7 @@ exports.default.createOne = async (req, res) => {
let retSolution = await Solution.findById({ _id: solution._id })
.populate('_creator', '-_id fullName nickName schacc')
.populate('comment', 'text creator createdAt')
.populate('comments', 'text creator createdAt')
.lean()
.exec()
......@@ -67,7 +67,7 @@ exports.default.createOne = async (req, res) => {
'description',
'file',
'task',
'comment',
'comments',
'createdAt',
'updatedAt',
'isAccepted',
......@@ -94,14 +94,14 @@ exports.default.getMany = async (req, res) => {
if (req.user.role === 'mentor')
solutions = await Solution.find()
.populate('_creator', '-_id fullName nickName schacc')
.populate('comment', 'text creator createdAt')
.populate('comments', 'text creator createdAt')
.select('-__v')
.lean()
.exec()
else
solutions = await Solution.find({ user: req.user._id })
.populate('_creator', '-_id fullName nickName')
.populate('comment', '-_id text creator createdAt')
.populate('comments', '-_id text creator createdAt')
.select('-__v')
.lean()
.exec()
......@@ -116,7 +116,7 @@ exports.default.getMany = async (req, res) => {
'description',
'file',
'task',
'comment',
'comments',
'createdAt',
'updatedAt',
'isAccepted',
......@@ -141,7 +141,7 @@ exports.default.getOne = async (req, res) => {
try {
let solution = await Solution.findById({ _id: req.params.id })
.populate('_creator', '-_id fullName nickName')
.populate('comment', 'text creator createdAt')
.populate('comments', 'text creator createdAt')
.select('-__v')
.lean()
.exec()
......@@ -163,7 +163,7 @@ exports.default.getOne = async (req, res) => {
'description',
'file',
'task',
'comment',
'comments',
'createdAt',
'updatedAt',
'isAccepted',
......
......@@ -23,7 +23,7 @@ const SolutionSchema = new mongoose.Schema(
type: String,
required: true,
},
comment: [
comments: [
{
type: mongoose.Schema.Types.ObjectId,
ref: 'comment',
......
......@@ -18,6 +18,7 @@ exports.default.getOne = async (req, res) => {
path: '_creator',
select: '-_id schacc fullName secondaryEmail',
})
.populate('comments', 'text creator createdAt')
.lean()
.exec()
......@@ -56,6 +57,7 @@ exports.default.getMany = async (req, res) => {
path: '_creator',
select: '-_id schacc fullName secondaryEmail',
})
.populate('comments', 'text creator createdAt')
.lean()
.exec()
......@@ -76,7 +78,7 @@ exports.default.getMany = async (req, res) => {
})
} catch (err) {
console.error(err)
res, status(400).end()
res.status(400).end()
}
}
exports.default.createOne = async (req, res) => {
......
......@@ -32,6 +32,13 @@ const TaskSchema = new mongoose.Schema(
required: true,
},
],
comments: [
{
type: mongoose.Schema.Types.ObjectId,
ref: 'comment',
required: true,
},
],
},
{ timestamps: true }
)
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment