Skip to content
Snippets Groups Projects
Commit 5421f586 authored by Anca Ursachi's avatar Anca Ursachi
Browse files

fix(post(fragmentsRecommendation)): HE should be able to recommend to publish...

fix(post(fragmentsRecommendation)): HE should be able to recommend to publish a manuscript when a re
parent 3d758786
No related branches found
No related tags found
3 merge requests!162Sprint 23 Features,!161Deploy S23 features and fixes,!155Hin 1130
/* eslint-disable no-return-await */
const uuid = require('uuid') const uuid = require('uuid')
const { pick, get, set, has, isEmpty, last } = require('lodash') const { pick, get, set, has, isEmpty, last } = require('lodash')
const config = require('config') const config = require('config')
...@@ -42,7 +43,7 @@ module.exports = models => async (req, res) => { ...@@ -42,7 +43,7 @@ module.exports = models => async (req, res) => {
const { collectionId, fragmentId } = req.params const { collectionId, fragmentId } = req.params
let collection, fragment let collection, fragment, fragments
try { try {
collection = await models.Collection.find(collectionId) collection = await models.Collection.find(collectionId)
...@@ -58,6 +59,19 @@ module.exports = models => async (req, res) => { ...@@ -58,6 +59,19 @@ module.exports = models => async (req, res) => {
}) })
} }
try {
fragments = await Promise.all(
collection.fragments.map(
async fragment => await models.Fragment.find(fragment),
),
)
} catch (e) {
const notFoundError = await services.handleNotFoundError(e, 'Item')
fragments = []
return res.status(notFoundError.status).json({
error: notFoundError.message,
})
}
const authsome = authsomeHelper.getAuthsome(models) const authsome = authsomeHelper.getAuthsome(models)
const target = { const target = {
fragment, fragment,
...@@ -87,6 +101,13 @@ module.exports = models => async (req, res) => { ...@@ -87,6 +101,13 @@ module.exports = models => async (req, res) => {
.json({ error: 'Cannot write a review on an older version.' }) .json({ error: 'Cannot write a review on an older version.' })
} }
const lastRecommendationByHE = last(
get(fragments[fragments.length - 2], 'recommendations', []) ||
[].find(
recommendation =>
recommendation.recommendationType === 'editorRecommendation',
),
)
if ( if (
recommendation === recommendations.publish && recommendation === recommendations.publish &&
recommendationType === recommendations.type.editor && recommendationType === recommendations.type.editor &&
...@@ -94,10 +115,21 @@ module.exports = models => async (req, res) => { ...@@ -94,10 +115,21 @@ module.exports = models => async (req, res) => {
collection.handlingEditor.id === req.user collection.handlingEditor.id === req.user
) { ) {
const fragmentHelper = new Fragment({ fragment }) const fragmentHelper = new Fragment({ fragment })
if (!fragmentHelper.hasReviewReport()) { const canHeMakeRecommendationAfterMajor =
return res fragmentHelper.hasReviewReport() &&
.status(400) lastRecommendationByHE.recommendation === 'major'
.json({ error: 'Cannot publish without at least one reviewer report.' })
const collectionHasReview = fragments.find(fragment =>
new Fragment({ fragment }).hasReviewReport(),
)
const canHeMakeRecommendationAfterMinor =
collectionHasReview && lastRecommendationByHE.recommendation !== 'major'
if (
!(canHeMakeRecommendationAfterMajor || canHeMakeRecommendationAfterMinor)
) {
return res.status(400).json({
error: 'Cannot publish without at least one reviewer report.',
})
} }
} }
......
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