diff --git a/packages/component-fixture-manager/src/fixtures/collections.js b/packages/component-fixture-manager/src/fixtures/collections.js index c0e6b797fa5259fffc927c1be6b11664cd6f5e59..d982f643c675c55a73ce3a1a4b57289935a9556b 100644 --- a/packages/component-fixture-manager/src/fixtures/collections.js +++ b/packages/component-fixture-manager/src/fixtures/collections.js @@ -205,6 +205,15 @@ const collections = { status: 'revisionRequested', 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 diff --git a/packages/component-manuscript-manager/src/tests/collections/get.test.js b/packages/component-manuscript-manager/src/tests/collections/get.test.js index 1d140a8c9de3aa2ce316012090ed7dd24a7613a7..fb88e5658444b5c3bad2f2cf5ac7760bf9900537 100644 --- a/packages/component-manuscript-manager/src/tests/collections/get.test.js +++ b/packages/component-manuscript-manager/src/tests/collections/get.test.js @@ -112,6 +112,8 @@ describe('Get collections route handler', () => { expect(res.statusCode).toBe(200) const data = JSON.parse(res._getData()) + delete testFixtures.collections.noFragmentsCollection + expect(data).toHaveLength(size(testFixtures.collections)) expect(data[0].type).toEqual('collection') expect(data[0]).toHaveProperty('visibleStatus') diff --git a/packages/xpub-faraday/config/authsome-mode.js b/packages/xpub-faraday/config/authsome-mode.js index e4129023dccead41ff08fec5a1b33d2f24066e42..fe678f981257b9e3a67647f062f3f15ef0f87f31 100644 --- a/packages/xpub-faraday/config/authsome-mode.js +++ b/packages/xpub-faraday/config/authsome-mode.js @@ -1,4 +1,5 @@ const config = require('config') +const logger = require('@pubsweet/logger') const { get, pickBy, last, has, pick } = require('lodash') const statuses = config.get('statuses') @@ -294,8 +295,13 @@ async function applyEditorInChiefPolicy(user, operation, object, context) { if (get(object, 'path') === '/api/collections') { const collections = await context.models.Collection.all() - return Promise.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, @@ -306,6 +312,8 @@ async function applyEditorInChiefPolicy(user, operation, object, context) { return coll }), ) + + return modifiedCollections.filter(Boolean) } } return true