Skip to content
Snippets Groups Projects
Commit c737b758 authored by Sebastian Mihalache's avatar Sebastian Mihalache :hammer_pick:
Browse files

refactor(manuscript-manager): separate recommendations into strategies

parent 1031606d
No related branches found
No related tags found
3 merge requests!196S25 - EiC submit revision,!189S25,!177Hin 230 eic request revision
const { findLast, isEmpty, maxBy, get, flatMap } = require('lodash') const config = require('config')
const { findLast, isEmpty, maxBy, get, flatMap, last } = require('lodash')
const { features = {}, recommendations: configRecommendations } = config
const Fragment = require('./Fragment') const Fragment = require('./Fragment')
...@@ -169,6 +172,10 @@ class Collection { ...@@ -169,6 +172,10 @@ class Collection {
), ),
) )
} }
isLatestVersion(fragmentId) {
return last(this.collection.fragments) === fragmentId
}
} }
module.exports = Collection module.exports = Collection
const { get, remove, findLast, last } = require('lodash') const { get, remove, findLast } = require('lodash')
const config = require('config') const config = require('config')
const User = require('./User') const User = require('./User')
...@@ -150,16 +150,18 @@ class Fragment { ...@@ -150,16 +150,18 @@ class Fragment {
) )
} }
canHEMakeAnotherRecommendation(currentUserRecommendations) { canHEMakeAnotherRecommendation(lastHERecommendation) {
const lastHERecommendation = last(currentUserRecommendations)
const { fragment: { recommendations = [] } } = this const { fragment: { recommendations = [] } } = this
const returnToHERecommendation = findLast( const returnToHERecommendation = findLast(
recommendations, recommendations,
r => r.recommendation === 'return-to-handling-editor', r => r.recommendation === 'return-to-handling-editor',
) )
if (!returnToHERecommendation) return false if (!returnToHERecommendation) return false
return returnToHERecommendation.createdOn > lastHERecommendation.createdOn return returnToHERecommendation.createdOn > lastHERecommendation.createdOn
} }
async getReviewersAndEditorsData({ collection, UserModel }) { async getReviewersAndEditorsData({ collection, UserModel }) {
const { const {
invitations = [], invitations = [],
......
const uuid = require('uuid') const uuid = require('uuid')
const { pick, get, set, has, isEmpty, last, chain } = require('lodash') const {
pick,
get,
set,
has,
isEmpty,
last,
chain,
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')
...@@ -14,10 +23,11 @@ const { ...@@ -14,10 +23,11 @@ const {
const { features = {}, recommendations } = config const { features = {}, recommendations } = config
const Notification = require('../../notifications/notification') const Notification = require('../../notifications/notification')
const publishAsHE = require('./strategies/hePublish')
module.exports = models => async (req, res) => { module.exports = models => async (req, res) => {
const { recommendation, comments, recommendationType } = req.body const { recommendation, comments, recommendationType } = req.body
if (!services.checkForUndefinedParams(recommendationType)) if (!services.checkForUndefinedParams(recommendationType, recommendation))
return res.status(400).json({ error: 'Recommendation type is required.' }) return res.status(400).json({ error: 'Recommendation type is required.' })
const reqUser = await models.User.find(req.user) const reqUser = await models.User.find(req.user)
...@@ -54,16 +64,6 @@ module.exports = models => async (req, res) => { ...@@ -54,16 +64,6 @@ module.exports = models => async (req, res) => {
error: notFoundError.message, error: notFoundError.message,
}) })
} }
const currentUserRecommendations = get(
fragment,
'recommendations',
[],
).filter(r => r.userId === req.user)
const lastFragmentRecommendation = chain(fragment)
.get('recommendations', [])
.last()
.value()
const authsome = authsomeHelper.getAuthsome(models) const authsome = authsomeHelper.getAuthsome(models)
const target = { const target = {
...@@ -77,41 +77,47 @@ module.exports = models => async (req, res) => { ...@@ -77,41 +77,47 @@ module.exports = models => async (req, res) => {
}) })
const fragmentHelper = new Fragment({ fragment }) const fragmentHelper = new Fragment({ fragment })
if (
recommendationType === recommendations.type.editor &&
last(collection.fragments) !== fragmentId
) {
return res
.status(400)
.json({ error: 'Cannot make a recommendation on an older version.' })
}
if ( if (!collectionHelper.isLatestVersion(fragmentId)) {
recommendationType === recommendations.type.review && const error =
last(collection.fragments) !== fragmentId recommendationType === recommendations.type.editor
) { ? 'Cannot make a recommendation on an older version.'
return res : 'Cannot write a review on an older version.'
.status(400) return res.status(400).json({ error })
.json({ error: 'Cannot write a review on an older version.' })
} }
if (
last(collection.fragments) === fragmentId && // const currentUserRecommendations = get(
!isEmpty(currentUserRecommendations) // fragment,
) { // 'recommendations',
// [],
// ).filter(r => r.userId === req.user)
const latestUserRecommendation = findLast(
fragment.recommendations,
r => r.userId === req.user,
)
if (latestUserRecommendation) {
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.' })
} }
if ( if (
recommendationType === recommendations.type.editor && recommendationType === recommendations.type.editor &&
!isEditorInChief && !isEditorInChief &&
!fragmentHelper.canHEMakeAnotherRecommendation(currentUserRecommendations) !fragmentHelper.canHEMakeAnotherRecommendation(latestUserRecommendation)
) { ) {
return res.status(400).json({ return res.status(400).json({
error: 'Cannot make another recommendation on this version.', error: 'Cannot make another recommendation on this version.',
}) })
} }
const lastFragmentRecommendation = chain(fragment)
.get('recommendations', [])
.last()
.value()
if ( if (
recommendationType === recommendations.type.editor && recommendationType === recommendations.type.editor &&
isEditorInChief && isEditorInChief &&
...@@ -124,18 +130,34 @@ module.exports = models => async (req, res) => { ...@@ -124,18 +130,34 @@ module.exports = models => async (req, res) => {
} }
} }
if ( const strategies = {
recommendation === recommendations.publish && he: {
recommendationType === recommendations.type.editor && publish: publishAsHE,
collection.handlingEditor && },
collection.handlingEditor.id === req.user
) {
if (!collectionHelper.canHEMakeRecommendation(fragments, fragmentHelper)) {
return res.status(400).json({
error: 'Cannot publish without at least one reviewer report.',
})
}
} }
const role = 'he'
try {
strategies[role][recommendation].execute({
collectionHelper,
fragments,
fragmentHelper,
})
} catch (e) {
return res.status(400).json({ error: e.message })
}
// if (
// recommendation === recommendations.publish &&
// recommendationType === recommendations.type.editor &&
// collection.handlingEditor &&
// collection.handlingEditor.id === req.user
// ) {
// if (!collectionHelper.canHEMakeRecommendation(fragments, fragmentHelper)) {
// return res.status(400).json({
// error: 'Cannot publish without at least one reviewer report.',
// })
// }
// }
fragment.recommendations = fragment.recommendations || [] fragment.recommendations = fragment.recommendations || []
const newRecommendation = { const newRecommendation = {
...@@ -146,8 +168,8 @@ module.exports = models => async (req, res) => { ...@@ -146,8 +168,8 @@ module.exports = models => async (req, res) => {
recommendationType, recommendationType,
} }
newRecommendation.recommendation = recommendation || undefined // newRecommendation.recommendation = recommendation || undefined
newRecommendation.comments = comments || undefined newRecommendation.comments = comments || []
if (recommendationType === 'editorRecommendation') { if (recommendationType === 'editorRecommendation') {
await collectionHelper.updateStatusOnRecommendation({ await collectionHelper.updateStatusOnRecommendation({
......
module.exports = {
execute: ({ fragment, collection, recommendation }) => {},
}
module.exports = {
execute: ({ collectionHelper, fragments, fragmentHelper }) => {
if (!collectionHelper.canHEMakeRecommendation(fragments, fragmentHelper)) {
throw new 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