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

fix(hotfix):

add handler for collections without fragments
parent a003ebef
No related branches found
No related tags found
2 merge requests!160Update staging with master features,!150Develop
...@@ -205,6 +205,15 @@ const collections = { ...@@ -205,6 +205,15 @@ const collections = {
status: 'revisionRequested', status: 'revisionRequested',
customId: chance.natural({ min: 999999, max: 9999999 }), customId: chance.natural({ min: 999999, max: 9999999 }),
}, },
noFragmentsCollection: {
id: chance.guid(),
title: chance.sentence(),
type: 'collection',
fragments: [],
owners: [user.id],
save: jest.fn(() => collections.collection),
customId: chance.natural({ min: 999999, max: 9999999 }),
},
} }
module.exports = collections module.exports = collections
...@@ -112,6 +112,8 @@ describe('Get collections route handler', () => { ...@@ -112,6 +112,8 @@ describe('Get collections route handler', () => {
expect(res.statusCode).toBe(200) expect(res.statusCode).toBe(200)
const data = JSON.parse(res._getData()) const data = JSON.parse(res._getData())
delete testFixtures.collections.noFragmentsCollection
expect(data).toHaveLength(size(testFixtures.collections)) expect(data).toHaveLength(size(testFixtures.collections))
expect(data[0].type).toEqual('collection') expect(data[0].type).toEqual('collection')
expect(data[0]).toHaveProperty('visibleStatus') expect(data[0]).toHaveProperty('visibleStatus')
......
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')
...@@ -294,8 +295,13 @@ async function applyEditorInChiefPolicy(user, operation, object, context) { ...@@ -294,8 +295,13 @@ 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()
return Promise.all( const modifiedCollections = await Promise.all(
collections.map(async coll => { 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] const latestFragmentId = coll.fragments[coll.fragments.length - 1]
coll.currentVersion = await context.models.Fragment.find( coll.currentVersion = await context.models.Fragment.find(
latestFragmentId, latestFragmentId,
...@@ -306,6 +312,8 @@ async function applyEditorInChiefPolicy(user, operation, object, context) { ...@@ -306,6 +312,8 @@ async function applyEditorInChiefPolicy(user, operation, object, context) {
return coll 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