-
Sebastian Mihalache authored1d2f901a
Code owners
Assign users and groups as approvers for specific file changes. Learn more.
const helpers = require('../../helpers/helpers')
const uuid = require('uuid')
module.exports = models => async (req, res) => {
const { recommendation, comments, recommendationType } = req.body
if (
!helpers.checkForUndefinedParams(
recommendation,
comments,
recommendationType,
)
)
return res.status(400).json({ error: 'Parameters are missing.' })
const reqUser = await models.User.find(req.user)
const { collectionId, fragmentId } = req.params
let collection, fragment
try {
collection = await models.Collection.find(collectionId)
if (!collection.fragments.includes(fragmentId))
return res.status(400).json({
error: `Collection and fragment do not match.`,
})
fragment = await models.Fragment.find(fragmentId)
} catch (e) {
const notFoundError = await helpers.handleNotFoundError(e, 'Item')
return res.status(notFoundError.status).json({
error: notFoundError.message,
})
}
fragment.recommendations = fragment.recommendations || []
const newRecommendation = {
id: uuid.v4(),
userId: reqUser.id,
submittedOn: new Date(),
recommendationType,
recommendation,
comments,
}
fragment.recommendations.push(newRecommendation)
await fragment.save()
return res.status(200).json(newRecommendation)
}