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