From 67d38595e42968c4ab2634886436ab8c405a883f Mon Sep 17 00:00:00 2001 From: Sebastian Mihalache <sebi.mihalache@gmail.com> Date: Mon, 5 Nov 2018 16:18:58 +0200 Subject: [PATCH] fix(hotfix): add handler for collections without fragments --- .../src/fixtures/collections.js | 9 +++++++++ .../src/tests/collections/get.test.js | 2 ++ packages/xpub-faraday/config/authsome-mode.js | 10 +++++++++- 3 files changed, 20 insertions(+), 1 deletion(-) diff --git a/packages/component-fixture-manager/src/fixtures/collections.js b/packages/component-fixture-manager/src/fixtures/collections.js index c0e6b797f..d982f643c 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 1d140a8c9..fb88e5658 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 e4129023d..fe678f981 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 -- GitLab