Skip to content
Snippets Groups Projects
Commit 0fb51174 authored by Mihail Hagiu's avatar Mihail Hagiu
Browse files

feat(authsome-mode):Refactor

parent ed05fe4f
No related branches found
No related tags found
3 merge requests!222Sprint #26,!217Sprint #26,!194Hin 1156 hide draft eic
const config = require('config') const config = require('config')
const logger = require('@pubsweet/logger')
const { get, pickBy, last, has, pick } = require('lodash') const { get, pickBy, last, has, pick } = require('lodash')
const statuses = config.get('statuses') const statuses = config.get('statuses')
...@@ -66,6 +65,10 @@ function unauthenticatedUser(operation, object, userId) { ...@@ -66,6 +65,10 @@ function unauthenticatedUser(operation, object, userId) {
return false return false
} }
const filterDraftCollections = c => get(c, 'status', 'draft') !== 'draft'
const filterNoFragmentCollections = c => c.fragments.length !== 0
const createPaths = ['/collections', '/collections/:collectionId/fragments'] const createPaths = ['/collections', '/collections/:collectionId/fragments']
async function applyAuthenticatedUserPolicy(user, operation, object, context) { async function applyAuthenticatedUserPolicy(user, operation, object, context) {
...@@ -309,25 +312,17 @@ async function applyAdminPolicy(user, operation, object, context) { ...@@ -309,25 +312,17 @@ async function applyAdminPolicy(user, operation, object, context) {
if (get(object, 'path') === '/api/collections') { if (get(object, 'path') === '/api/collections') {
const collections = await context.models.Collection.all() const collections = await context.models.Collection.all()
const modifiedCollections = await Promise.all( return Promise.all(
collections.map(async coll => { collections.filter(filterNoFragmentCollections).map(async coll => {
if (coll.fragments.length === 0) {
logger.error(`Collection ${coll.id} does not have any fragments!`)
return null
}
const latestFragmentId = coll.fragments[coll.fragments.length - 1] const latestFragmentId = coll.fragments[coll.fragments.length - 1]
coll.currentVersion = await context.models.Fragment.find( coll.currentVersion = await context.models.Fragment.find(
latestFragmentId, latestFragmentId,
) )
const status = get(coll, 'status', 'draft') const status = get(coll, 'status', 'draft')
coll.visibleStatus = get(statuses, `${status}.admin.label`) coll.visibleStatus = get(statuses, `${status}.admin.label`)
return coll return coll
}), }),
) )
return modifiedCollections.filter(Boolean)
} }
} }
return true return true
...@@ -357,23 +352,20 @@ async function applyEditorInChiefPolicy(user, operation, object, context) { ...@@ -357,23 +352,20 @@ async function applyEditorInChiefPolicy(user, operation, object, context) {
if (get(object, 'path') === '/api/collections') { if (get(object, 'path') === '/api/collections') {
const collections = await context.models.Collection.all() const collections = await context.models.Collection.all()
const modifiedCollections = await Promise.all( return Promise.all(
collections.map(async coll => { collections
if (coll.fragments.length === 0) { .filter(filterDraftCollections)
logger.error(`Collection ${coll.id} does not have any fragments!`) .filter(filterNoFragmentCollections)
return null .map(async coll => {
} const latestFragmentId = coll.fragments[coll.fragments.length - 1]
const latestFragmentId = coll.fragments[coll.fragments.length - 1] coll.currentVersion = await context.models.Fragment.find(
coll.currentVersion = await context.models.Fragment.find( latestFragmentId,
latestFragmentId, )
) const status = get(coll, 'status', 'draft')
const status = get(coll, 'status', 'draft') coll.visibleStatus = get(statuses, `${status}.editorInChief.label`)
if (status === 'draft') return null return coll
coll.visibleStatus = get(statuses, `${status}.editorInChief.label`) }),
return coll
}),
) )
return modifiedCollections.filter(Boolean)
} }
} }
return true return true
......
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