Commit 2cc795d4 authored by rlacko's avatar rlacko 💬
Browse files

fix activity controller

parent 245edf82
Pipeline #4674 passed with stages
in 1 minute and 33 seconds
......@@ -17,36 +17,6 @@ const pickedKeys = [
'comment',
]
const activitySelector =
'_id title description date type createdAt updatedAt attendance comment'
const commentSelector = '_id creator _creator text createdAt'
const attendanceSelector = '_id user state'
const creatorSelector = '-_id fullName nickName schacc'
// Overwrites the given field with the populated User
// field: populate on this
// byPopulate: user schacc field name
async function populateUsersOnField(fieldToPopulate, byPopulate) {
let newField = []
for (let field of fieldToPopulate) {
const populated = await User.findOne({
schacc: field[byPopulate],
})
.lean()
.exec()
field[byPopulate] = pick(populated, creatorSelector.split(' '))
newField.push(field)
}
return newField
}
async function populateCommentsandAttendances(activity) {
activity.comment = await populateUsersOnField(activity.comment, 'creator')
activity.attendance = await populateUsersOnField(activity.attendance, 'user')
return activity
}
// Controller
module.exports.createOne = async function createOne(req, res) {
......@@ -87,21 +57,6 @@ module.exports.createOne = async function createOne(req, res) {
.exec()
}
await activity
.populate({
path: 'comment',
select: commentSelector,
})
.populate({
path: 'attendance',
select: attendanceSelector,
})
.execPopulate()
const populatedActivity = await populateCommentsandAttendances(
activity.toObject()
)
return res
.status(200)
.json({
......@@ -125,24 +80,36 @@ module.exports.createOne = async function createOne(req, res) {
module.exports.getOne = async function getOne(req, res) {
try {
const activity = await Activity.findOne({ _id: req.params.id })
.populate({
path: 'comment',
select: commentSelector,
})
.populate({
path: 'attendance',
select: attendanceSelector,
})
.lean()
.exec()
if (!activity)
return res.status(404).json({ messages: ['No such activity.'] })
const populatedActivity = await populateCommentsandAttendances(activity)
return res.status(200).json({ data: pick(populatedActivity, pickedKeys) })
if (req.user.role == 'mentor')
return res.status(200).json({
data: pick(activity, pickedKeys),
})
return res.status(200).json({
data: pick(activity, [
'_id',
'title',
'description',
'date',
'type',
'createdAt',
'updatedAt',
]),
})
} catch (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 })
}
......@@ -150,31 +117,25 @@ module.exports.getOne = async function getOne(req, res) {
module.exports.getMany = async function getMany(req, res) {
try {
const activity = await Activity.find()
.populate({
path: 'comment',
select: '_id creator text createdAt',
})
.populate({
path: 'attendance',
select: '_id user state',
})
.select('-__v')
.lean()
.exec()
const activity = await Activity.find().select('-__v').lean().exec()
if (!activity)
return res.status(404).json({ message: 'Activity not found!' })
res.status(200).json({
data: await Promise.all(
activity.map(async function pickKeys(doc) {
const populatedActivity = await populateCommentsandAttendances(doc)
return pick(populatedActivity, pickedKeys)
})
),
data: activity.map(function pickKeys(doc) {
return pick(populatedActivity, pickedKeys)
}),
})
} catch (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)
res.status(500).json({ message: err.message })
}
......@@ -185,17 +146,11 @@ module.exports.removeOne = async function removeOne(req, res) {
const activity = await Activity.findByIdAndDelete({
_id: req.params.id,
})
.populate({
path: 'comment',
select: '_id creator text createdAt',
})
.select('-__v')
.lean()
.exec()
if (!activity) {
if (!activity)
return res.status(404).json({ message: 'Activity not found!' })
}
await Attendance.deleteMany({
_id: activity.attendance.map(function getAttendanceIds(element) {
......@@ -211,6 +166,14 @@ module.exports.removeOne = async function removeOne(req, res) {
return res.status(200).json({ data: pick(activity, pickedKeys) })
} catch (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 })
}
......@@ -223,11 +186,6 @@ module.exports.updateOne = async function updateOne(req, res) {
omit(req.body, ['attendance', 'comment']),
{ new: true }
)
.populate({
path: 'comment',
select: '_id creator text createdAt',
})
.select('-__v')
.lean()
.exec()
......@@ -236,6 +194,14 @@ module.exports.updateOne = async function updateOne(req, res) {
res.status(200).json({ data: pick(activity, pickedKeys) })
} catch (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)
res.status(500).json({ message: err.message })
}
......
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