Skip to content
Snippets Groups Projects
Commit 3cfe1fa1 authored by Tania Fecheta's avatar Tania Fecheta
Browse files

fix(fragmentRecommendations): use findLast instead of two filters and cover the undefined case

parent 42345747
No related branches found
No related tags found
3 merge requests!176Sprint 24,!171Sprint 24,!157fix(fragmentRecommendation): allow HE to make another recommendation on the same…
const uuid = require('uuid')
const { pick, get, set, has, isEmpty, last } = require('lodash')
const { pick, get, set, has, isEmpty, last, findLast } = require('lodash')
const config = require('config')
const { v4 } = require('uuid')
const logger = require('@pubsweet/logger')
......@@ -41,11 +41,14 @@ module.exports = models => async (req, res) => {
})
}
const currentUserRecommendation = get(fragment, 'recommendations', []).filter(
r => r.userId === req.user,
)
const currentUserRecommendations = get(
fragment,
'recommendations',
[],
).filter(r => r.userId === req.user)
const returnToHERecommendations = get(fragment, 'recommendations', []).filter(
const returnToHERecommendation = findLast(
get(fragment, 'recommendations', []),
r => r.recommendation === 'return-to-handling-editor',
)
const authsome = authsomeHelper.getAuthsome(models)
......@@ -80,21 +83,22 @@ module.exports = models => async (req, res) => {
if (
last(collection.fragments) === fragmentId &&
!isEmpty(currentUserRecommendation)
!isEmpty(currentUserRecommendations)
) {
if (recommendationType === recommendations.type.review) {
return res
.status(400)
.json({ error: 'Cannot write another review on this version.' })
}
if (
} else if (
recommendationType === recommendations.type.editor &&
last(returnToHERecommendations).createdOn <
last(currentUserRecommendation).createdOn
((returnToHERecommendation &&
returnToHERecommendation.createdOn <=
last(currentUserRecommendations).createdOn) ||
!returnToHERecommendation)
) {
return res
.status(400)
.json({ error: 'Cannot make another recommendation on this version.' })
return res.status(400).json({
error: 'Cannot make another recommendation on this version.',
})
}
}
......
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