diff --git a/packages/component-fixture-manager/src/fixtures/collections.js b/packages/component-fixture-manager/src/fixtures/collections.js index 38495a9e8186e3403f127da38d414ca2c84cf6ef..0fbff658ca84fc9266a31674461679e769a1ff13 100644 --- a/packages/component-fixture-manager/src/fixtures/collections.js +++ b/packages/component-fixture-manager/src/fixtures/collections.js @@ -31,6 +31,7 @@ const collections = { fragments: [fragment.id], owners: [user.id], save: jest.fn(() => collections.collection), + getFragments: jest.fn(() => [fragment]), invitations: [ { id: chance.guid(), @@ -74,6 +75,7 @@ const collections = { fragments: [fragment.id], owners: [user.id], save: jest.fn(() => collections.collection), + getFragments: jest.fn(() => [fragment]), invitations: [ { id: chance.guid(), @@ -116,6 +118,7 @@ const collections = { fragments: [fragment1.id, noInvitesFragment.id], owners: [user.id], save: jest.fn(() => collections.collection2), + getFragments: jest.fn(() => [fragment1, noInvitesFragment]), invitations: [ { id: chance.guid(), @@ -160,6 +163,7 @@ const collections = { created: chance.timestamp(), customId: '0000001', fragments: [reviewCompletedFragment.id], + getFragments: jest.fn(() => [reviewCompletedFragment]), invitations: [ { id: chance.guid(), @@ -190,6 +194,7 @@ const collections = { fragments: [fragment.id, reviewCompletedFragment.id], owners: [user.id], save: jest.fn(() => collections.collection), + getFragments: jest.fn(() => [fragment, reviewCompletedFragment]), invitations: [ { id: chance.guid(), @@ -220,6 +225,7 @@ const collections = { fragments: [], owners: [user.id], save: jest.fn(() => collections.collection), + getFragments: jest.fn(() => []), customId: chance.natural({ min: 999999, max: 9999999 }), }, noEditorRecomedationCollection: { @@ -229,6 +235,7 @@ const collections = { fragments: [noEditorRecomedationFragment.id], owners: [user.id], save: jest.fn(() => collections.noEditorRecomedationCollection), + getFragments: jest.fn(() => [noEditorRecomedationFragment]), invitations: [ { id: chance.guid(), @@ -271,6 +278,7 @@ const collections = { fragments: [reviewCompletedFragment.id, noInvitesFragment.id], owners: [user.id], save: jest.fn(() => collections.collection), + getFragments: jest.fn(() => [reviewCompletedFragment, noInvitesFragment]), invitations: [ { id: chance.guid(), diff --git a/packages/component-helper-service/src/services/Collection.js b/packages/component-helper-service/src/services/Collection.js index 90faee9ffa288e6fe165d678a578ad0f4a580819..5ead71c258d5cad06b646bd6b9639df96b59e42a 100644 --- a/packages/component-helper-service/src/services/Collection.js +++ b/packages/component-helper-service/src/services/Collection.js @@ -108,20 +108,8 @@ class Collection { return lastName || firstName } - async getAllCollectionFragments({ FragmentModel }) { - const allCollectionFragments = await Promise.all( - get(this.collection, 'fragments', []).map(async fragmentId => { - const fragment = await FragmentModel.find(fragmentId) - return fragment - }), - ) - return allCollectionFragments - } - - async getReviewerNumber({ userId, FragmentModel }) { - const allCollectionFragments = await this.getAllCollectionFragments({ - FragmentModel, - }) + async getReviewerNumber({ userId }) { + const allCollectionFragments = await this.collection.getFragments() const allCollectionInvitations = flatMap( allCollectionFragments, fragment => fragment.invitations, diff --git a/packages/component-helper-service/src/tests/collection.test.js b/packages/component-helper-service/src/tests/collection.test.js index 30315ad22e5616910b55751765a8d4b9723ba468..ebf35e9d666f30daf27cdce4c55c57f6637c3cf1 100644 --- a/packages/component-helper-service/src/tests/collection.test.js +++ b/packages/component-helper-service/src/tests/collection.test.js @@ -4,48 +4,13 @@ process.env.SUPPRESS_NO_CONFIG_WARNING = true const { cloneDeep } = require('lodash') const fixturesService = require('pubsweet-component-fixture-service') -const { fixtures, Model } = fixturesService +const { fixtures } = fixturesService const { Collection } = require('../Helper') describe('Collection helper', () => { let testFixtures = {} - let models beforeEach(() => { testFixtures = cloneDeep(fixtures) - models = Model.build(testFixtures) - }) - - describe('getAllCollectionFragments', () => { - it('should return an array with all collection fragments', async () => { - const { collection } = testFixtures.collections - const collectionHelper = new Collection({ collection }) - const FragmentModel = models.Fragment - - const numberOfFragments = collection.fragments.length - const allCollectionFragments = await collectionHelper.getAllCollectionFragments( - { - FragmentModel, - }, - ) - - expect(allCollectionFragments).toHaveLength(numberOfFragments) - expect(typeof allCollectionFragments[0]).toBe('object') - expect(allCollectionFragments[0].type).toBe('fragment') - }) - it('should return an empty array when no fragments exist', async () => { - const { noFragmentsCollection } = testFixtures.collections - const collectionHelper = new Collection({ noFragmentsCollection }) - const FragmentModel = models.Fragment - - const allCollectionFragments = await collectionHelper.getAllCollectionFragments( - { - FragmentModel, - }, - ) - - expect(allCollectionFragments).toHaveLength(0) - expect(typeof allCollectionFragments[0]).toBe('undefined') - }) }) describe('getReviewerNumber', () => { @@ -53,11 +18,9 @@ describe('Collection helper', () => { const { collection } = testFixtures.collections const { reviewer } = testFixtures.users const collectionHelper = new Collection({ collection }) - const FragmentModel = models.Fragment const reviewerNumber = await collectionHelper.getReviewerNumber({ userId: reviewer.id, - FragmentModel, }) expect(reviewerNumber).toBe(1) @@ -68,11 +31,9 @@ describe('Collection helper', () => { const collectionHelper = new Collection({ collection: collectionReviewCompleted, }) - const FragmentModel = models.Fragment const reviewerNumber = await collectionHelper.getReviewerNumber({ userId: reviewer.id, - FragmentModel, }) expect(reviewerNumber).toBe(3) @@ -83,11 +44,9 @@ describe('Collection helper', () => { const collectionHelper = new Collection({ collection: oneReviewedFragmentCollection, }) - const FragmentModel = models.Fragment const reviewerNumber = await collectionHelper.getReviewerNumber({ userId: answerReviewer.id, - FragmentModel, }) expect(reviewerNumber).toBe(2) @@ -98,11 +57,9 @@ describe('Collection helper', () => { const collectionHelper = new Collection({ collection: oneReviewedFragmentCollection, }) - const FragmentModel = models.Fragment const reviewerNumber = await collectionHelper.getReviewerNumber({ userId: reviewer.id, - FragmentModel, }) expect(reviewerNumber).toBe(3) diff --git a/packages/component-manuscript-manager/src/routes/fragmentsRecommendations/patch.js b/packages/component-manuscript-manager/src/routes/fragmentsRecommendations/patch.js index 7b03107b5f04aa5b56d17b9bab0b53dfbf41505b..a973956cfb0e01d956a74168bebe3e81a65b62ca 100644 --- a/packages/component-manuscript-manager/src/routes/fragmentsRecommendations/patch.js +++ b/packages/component-manuscript-manager/src/routes/fragmentsRecommendations/patch.js @@ -66,11 +66,10 @@ module.exports = models => async (req, res) => { } const collectionHelper = new Collection({ collection }) - const FragmentModel = models.Fragment const reviewerNumber = await collectionHelper.getReviewerNumber({ userId: req.authInfo.id, - FragmentModel, }) + find(fragment.invitations, [ 'userId', userId,