Commit f7c3c401 authored by rlacko's avatar rlacko 💬
Browse files

remove activity

parent b8b7aa0d
Pipeline #4662 passed with stages
in 1 minute and 32 seconds
......@@ -5,7 +5,7 @@ const { Attendance } = require('../attendance/attendanceModel')
const { Comment } = require('../comment/commentModel')
const { omit, pick } = require('lodash')
const defaultKeys = [
const pickedKeys = [
'_id',
'title',
'description',
......@@ -16,7 +16,7 @@ const defaultKeys = [
'comment',
]
exports.default = crudControllers(Activity, defaultKeys)
exports.default = crudControllers(Activity, pickedKeys)
exports.default.createOne = async (req, res) => {
try {
......@@ -74,7 +74,7 @@ exports.default.createOne = async (req, res) => {
res
.status(200)
.json({
data: pick(objActivity, defaultKeys),
data: pick(objActivity, pickedKeys),
})
.end()
} catch (err) {
......@@ -102,14 +102,14 @@ exports.default.getOne = async (req, res) => {
path: 'attendance',
select: '_id user state',
})
.select('-_id -__v')
.select('-__v')
.lean()
.exec()
if (!activity)
return res.status(404).json({ messages: ['No such activity.'] })
return res.status(200).json({ data: pick(activity, defaultKeys) })
return res.status(200).json({ data: pick(activity, pickedKeys) })
} catch (err) {
console.error(err)
return res.status(500).json({ message: err.message })
......@@ -127,7 +127,7 @@ exports.default.getMany = async (req, res) => {
path: 'attendance',
select: '_id user state',
})
.select('-__v -_id')
.select('-__v')
.lean()
.exec()
......@@ -136,7 +136,7 @@ exports.default.getMany = async (req, res) => {
res.status(200).json({
data: activity.map(function pickKeys(doc) {
return pick(doc, defaultKeys)
return pick(doc, pickedKeys)
}),
})
} catch (err) {
......@@ -145,34 +145,42 @@ exports.default.getMany = async (req, res) => {
}
}
//TODO req.user.role === 'mentor'
exports.default.removeOne = async (req, res) => {
//validate
try {
const activity = await Activity.findByIdAndDelete({
_id: req.params.id,
})
.populate({
path: 'comment',
populate: { path: '_creator', select: '-_id fullName nickName' },
select: '_id creator text createdAt',
})
.select('-__v')
.lean()
.exec()
if (!activity) {
res.status(404).json({ message: 'Activity not found!' }).end()
return res.status(404).json({ message: 'Activity not found!' }).end()
}
await Attendance.deleteMany({
_id: activity.attendance.map(function getAttendanceIds(element) {
return element._id
}),
})
await Comment.deleteMany({
_id: activity.comment.map(function getCommentIds(element) {
return element._id
}),
})
res.status(200).json({ data: activity }).end()
return res
.status(200)
.json({ data: pick(activity, pickedKeys) })
.end()
} catch (err) {
res.status(500).json({ message: err.message }).end()
console.error(err)
return res.status(500).json({ message: err.message }).end()
}
}
......
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