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 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 config = require('config')
const { v4 } = require('uuid') const { v4 } = require('uuid')
const logger = require('@pubsweet/logger') const logger = require('@pubsweet/logger')
...@@ -41,11 +41,14 @@ module.exports = models => async (req, res) => { ...@@ -41,11 +41,14 @@ module.exports = models => async (req, res) => {
}) })
} }
const currentUserRecommendation = get(fragment, 'recommendations', []).filter( const currentUserRecommendations = get(
r => r.userId === req.user, 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', r => r.recommendation === 'return-to-handling-editor',
) )
const authsome = authsomeHelper.getAuthsome(models) const authsome = authsomeHelper.getAuthsome(models)
...@@ -80,21 +83,22 @@ module.exports = models => async (req, res) => { ...@@ -80,21 +83,22 @@ module.exports = models => async (req, res) => {
if ( if (
last(collection.fragments) === fragmentId && last(collection.fragments) === fragmentId &&
!isEmpty(currentUserRecommendation) !isEmpty(currentUserRecommendations)
) { ) {
if (recommendationType === recommendations.type.review) { if (recommendationType === recommendations.type.review) {
return res return res
.status(400) .status(400)
.json({ error: 'Cannot write another review on this version.' }) .json({ error: 'Cannot write another review on this version.' })
} } else if (
if (
recommendationType === recommendations.type.editor && recommendationType === recommendations.type.editor &&
last(returnToHERecommendations).createdOn < ((returnToHERecommendation &&
last(currentUserRecommendation).createdOn returnToHERecommendation.createdOn <=
last(currentUserRecommendations).createdOn) ||
!returnToHERecommendation)
) { ) {
return res return res.status(400).json({
.status(400) error: 'Cannot make another recommendation on this version.',
.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