Skip to content
Snippets Groups Projects
Commit 099dde64 authored by Andrei Cioromila's avatar Andrei Cioromila
Browse files

Merge branch 'HIN-1130' of gitlab.coko.foundation:xpub/xpub-faraday into...

Merge branch 'HIN-1130' of gitlab.coko.foundation:xpub/xpub-faraday into HIN-1153-status-after-review
parents e269bca1 1e93d08f
No related branches found
No related tags found
3 merge requests!162Sprint 23 Features,!161Deploy S23 features and fixes,!159Hin 1153 status after review
const { findLast, get } = require('lodash')
const Fragment = require('./Fragment')
class Collection {
constructor({ collection = {} }) {
this.collection = collection
......@@ -103,6 +107,36 @@ class Collection {
const [firstName, lastName] = this.collection.handlingEditor.name.split(' ')
return lastName || firstName
}
// eslint-disable-next-line class-methods-use-this
hasAtLeastOneReviewReport(fragments) {
return fragments.some(fragment =>
new Fragment({ fragment }).hasReviewReport(),
)
}
canHEMakeRecommendation(fragments, fragmentHelper) {
if (this.collection.fragments.length === 1) {
return fragmentHelper.hasReviewReport()
}
const previousVersionRecommendations = get(
fragments[fragments.length - 2],
'recommendations',
[],
)
const lastRecommendationByHE = findLast(
previousVersionRecommendations,
recommendation =>
recommendation.userId === this.collection.handlingEditor.id &&
recommendation.recommendationType === 'editorRecommendation',
)
if (lastRecommendationByHE.recommendation === 'minor') {
return this.hasAtLeastOneReviewReport(fragments)
} else if (lastRecommendationByHE.recommendation === 'major') {
return fragmentHelper.hasReviewReport()
}
}
}
module.exports = Collection
......@@ -42,6 +42,8 @@ module.exports = models => async (req, res) => {
})
}
const collectionHelper = new Collection({ collection })
try {
fragments = await Promise.all(
collection.fragments.map(
......@@ -102,38 +104,13 @@ module.exports = models => async (req, res) => {
.json({ error: 'Cannot make another recommendation on this version.' })
}
function canHEMakeRecommendation(fragmentHelper) {
if (collection.fragments.length === 1) {
return fragmentHelper.hasReviewReport()
}
const previousVersionRecommendations = get(
fragments[fragments.length - 2],
'recommendations',
[],
)
const lastRecommendationByHE = findLast(
previousVersionRecommendations,
recommendation =>
recommendation.userId === collection.handlingEditor.id &&
recommendation.recommendationType === 'editorRecommendation',
)
if (lastRecommendationByHE.recommendation === 'minor') {
return fragments.find(fragment =>
new Fragment({ fragment }).hasReviewReport(),
)
} else if (lastRecommendationByHE.recommendation === 'major') {
return fragmentHelper.hasReviewReport()
}
}
if (
recommendation === recommendations.publish &&
recommendationType === recommendations.type.editor &&
collection.handlingEditor &&
collection.handlingEditor.id === req.user
) {
if (!canHEMakeRecommendation(fragmentHelper)) {
if (!collectionHelper.canHEMakeRecommendation(fragments, fragmentHelper)) {
return res.status(400).json({
error: 'Cannot publish without at least one reviewer report.',
})
......@@ -153,8 +130,6 @@ module.exports = models => async (req, res) => {
newRecommendation.comments = comments || undefined
if (recommendationType === 'editorRecommendation') {
const collectionHelper = new Collection({ collection })
collectionHelper.updateStatusOnRecommendation({
isEditorInChief,
recommendation,
......
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