diff --git a/packages/component-user-manager/src/CollectionsUsers.js b/packages/component-user-manager/src/CollectionsUsers.js index 357e5ee528f71a6a153623364aa203446cde4772..93af60000657e6e2cad09e6698418c12bc15ff88 100644 --- a/packages/component-user-manager/src/CollectionsUsers.js +++ b/packages/component-user-manager/src/CollectionsUsers.js @@ -18,6 +18,11 @@ const CollectionsInvitations = app => { require(`${routePath}/delete`)(app.locals.models), ) app.get(basePath, authBearer, require(`${routePath}/get`)(app.locals.models)) + app.patch( + `${basePath}/:userId`, + authBearer, + require(`${routePath}/patch`)(app.locals.models), + ) } module.exports = CollectionsInvitations diff --git a/packages/component-user-manager/src/helpers/Team.js b/packages/component-user-manager/src/helpers/Team.js index 337eefadb40517fc23722c32be1e80b8b29ff143..dc3990cb2d173f804fba18a0640dd1bf42f3426c 100644 --- a/packages/component-user-manager/src/helpers/Team.js +++ b/packages/component-user-manager/src/helpers/Team.js @@ -81,17 +81,23 @@ const removeTeamMember = async (teamId, userId, TeamModel) => { const getTeamMembersByCollection = async (collectionId, role, TeamModel) => { const teams = await TeamModel.all() - const members = get( - teams.find( - team => - team.group === role && - team.object.type === 'collection' && - team.object.id === collectionId, - ), - 'members', + // const members = get( + // teams.find( + // team => + // team.group === role && + // team.object.type === 'collection' && + // team.object.id === collectionId, + // ), + // 'members', + // ) + const team = teams.find( + team => + team.group === role && + team.object.type === 'collection' && + team.object.id === collectionId, ) - return members + return team.members } const getTeamByGroupAndCollection = async (collectionId, role, TeamModel) => { diff --git a/packages/component-user-manager/src/routes/collectionsUsers/patch.js b/packages/component-user-manager/src/routes/collectionsUsers/patch.js new file mode 100644 index 0000000000000000000000000000000000000000..4aedb7f38492f4902acec391aba17894c217942f --- /dev/null +++ b/packages/component-user-manager/src/routes/collectionsUsers/patch.js @@ -0,0 +1,41 @@ +const logger = require('@pubsweet/logger') +const helpers = require('../../helpers/helpers') + +module.exports = models => async (req, res) => { + // TO DO: add authsome + const { collectionId, userId } = req.params + const { isSubmitting, isCorresponding } = req.body + + if (!helpers.checkForUndefinedParams(isSubmitting, isCorresponding)) { + res.status(400).json({ error: 'Missing parameters' }) + logger.error('some parameters are missing') + return + } + + try { + let collection = await models.Collection.find(collectionId) + if (collection.authors === undefined) { + return res.status(400).json({ + error: 'Collection does not have any authors', + }) + } + const user = await models.User.find(userId) + const matchingAuthor = collection.authors.find( + author => author.userId === user.id, + ) + if (matchingAuthor === undefined) { + return res.status(400).json({ + error: 'Collection and user do not match', + }) + } + matchingAuthor.isSubmitting = isSubmitting + matchingAuthor.isCorresponding = isCorresponding + collection = await collection.save() + res.status(200).json(collection) + } catch (e) { + const notFoundError = await helpers.handleNotFoundError(e, 'item') + return res.status(notFoundError.status).json({ + error: notFoundError.message, + }) + } +} diff --git a/packages/component-user-manager/src/tests/collectionsUsers/delete.test.js b/packages/component-user-manager/src/tests/collectionsUsers/delete.test.js index d9b36c6f4f830cdb12e5fc6a9637b96f2167bb23..7242ac5c76c8b75ce0e265ca9ef98981490fe1a0 100644 --- a/packages/component-user-manager/src/tests/collectionsUsers/delete.test.js +++ b/packages/component-user-manager/src/tests/collectionsUsers/delete.test.js @@ -7,10 +7,6 @@ const Model = require('./../helpers/Model') const models = Model.build() -const notFoundError = new Error() -notFoundError.name = 'NotFoundError' -notFoundError.status = 404 - const { author, submittingAuthor } = fixtures.users const { standardCollection } = fixtures.collections const { authorTeam } = fixtures.teams diff --git a/packages/component-user-manager/src/tests/collectionsUsers/patch.test.js b/packages/component-user-manager/src/tests/collectionsUsers/patch.test.js new file mode 100644 index 0000000000000000000000000000000000000000..e18c21f6f804f59ae7b9f53111fcf7cb56029c67 --- /dev/null +++ b/packages/component-user-manager/src/tests/collectionsUsers/patch.test.js @@ -0,0 +1,112 @@ +process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0' +process.env.SUPPRESS_NO_CONFIG_WARNING = true + +const httpMocks = require('node-mocks-http') +const fixtures = require('./../fixtures/fixtures') +const Model = require('./../helpers/Model') + +const models = Model.build() +jest.mock('pubsweet-component-mail-service', () => ({ + setupAssignEmail: jest.fn(), + setupHandlingEditorAgreedEmail: jest.fn(), + setupDeclineEmail: jest.fn(), +})) + +const { author, submittingAuthor } = fixtures.users +const { standardCollection, authorsCollection } = fixtures.collections +const body = { + isSubmitting: false, + isCorresponding: true, +} +const patchPath = '../../routes/collectionsUsers/patch' +describe('Patch collections users route handler', () => { + it('should return success when the request data is correct', async () => { + const req = httpMocks.createRequest({ + body, + }) + req.user = submittingAuthor.id + req.params.collectionId = standardCollection.id + req.params.userId = submittingAuthor.id + const res = httpMocks.createResponse() + await require(patchPath)(models)(req, res) + const data = JSON.parse(res._getData()) + expect(res.statusCode).toBe(200) + const matchingAuthor = data.authors.find( + author => author.userId === submittingAuthor.id, + ) + expect(matchingAuthor.isSubmitting).toBe(body.isSubmitting) + expect(matchingAuthor.isCorresponding).toBe(body.isCorresponding) + }) + it('should return an error when the params are missing', async () => { + delete body.isSubmitting + const req = httpMocks.createRequest({ + body, + }) + req.user = submittingAuthor.id + req.params.collectionId = standardCollection.id + req.params.userId = submittingAuthor.id + const res = httpMocks.createResponse() + await require(patchPath)(models)(req, res) + + expect(res.statusCode).toBe(400) + const data = JSON.parse(res._getData()) + expect(data.error).toEqual('Missing parameters') + body.isSubmitting = false + }) + it('should return an error if the collection does not exists', async () => { + const req = httpMocks.createRequest({ + body, + }) + req.user = submittingAuthor.id + req.params.collectionId = 'invalid-id' + req.params.userId = submittingAuthor.id + const res = httpMocks.createResponse() + await require(patchPath)(models)(req, res) + + expect(res.statusCode).toBe(404) + const data = JSON.parse(res._getData()) + expect(data.error).toEqual('item not found') + }) + it('should return an error when the user does not exist', async () => { + const req = httpMocks.createRequest({ + body, + }) + req.user = author.id + req.params.collectionId = standardCollection.id + req.params.userId = 'invalid-id' + const res = httpMocks.createResponse() + await require(patchPath)(models)(req, res) + + expect(res.statusCode).toBe(404) + const data = JSON.parse(res._getData()) + expect(data.error).toEqual('item not found') + }) + it('should return an error when the collection does not have authors', async () => { + const req = httpMocks.createRequest({ + body, + }) + req.user = submittingAuthor.id + req.params.collectionId = authorsCollection.id + req.params.userId = submittingAuthor.id + const res = httpMocks.createResponse() + await require(patchPath)(models)(req, res) + + expect(res.statusCode).toBe(400) + const data = JSON.parse(res._getData()) + expect(data.error).toEqual('Collection does not have any authors') + }) + it('should return an error when the collection and the user do not match', async () => { + const req = httpMocks.createRequest({ + body, + }) + req.user = submittingAuthor.id + req.params.collectionId = standardCollection.id + req.params.userId = author.id + const res = httpMocks.createResponse() + await require(patchPath)(models)(req, res) + + expect(res.statusCode).toBe(400) + const data = JSON.parse(res._getData()) + expect(data.error).toEqual('Collection and user do not match') + }) +}) diff --git a/packages/component-user-manager/src/tests/collectionsUsers/post.test.js b/packages/component-user-manager/src/tests/collectionsUsers/post.test.js index 2d3e09c16d627f75f28591b605dbe7dbb1892c80..14642466da9824cc291bb4b410d9d5ab9f2217a1 100644 --- a/packages/component-user-manager/src/tests/collectionsUsers/post.test.js +++ b/packages/component-user-manager/src/tests/collectionsUsers/post.test.js @@ -13,10 +13,6 @@ jest.mock('pubsweet-component-mail-service', () => ({ })) const chance = new Chance() -const notFoundError = new Error() -notFoundError.name = 'NotFoundError' -notFoundError.status = 404 - const { author, submittingAuthor } = fixtures.users const { standardCollection } = fixtures.collections const postPath = '../../routes/collectionsUsers/post' diff --git a/packages/component-user-manager/src/tests/fixtures/collections.js b/packages/component-user-manager/src/tests/fixtures/collections.js index b52062a067bb7e8375023b0856f18da55e63c719..bd50ad595d07a565ae5b799fb1a744217b92192d 100644 --- a/packages/component-user-manager/src/tests/fixtures/collections.js +++ b/packages/component-user-manager/src/tests/fixtures/collections.js @@ -2,14 +2,13 @@ const Chance = require('chance') const { submittingAuthor } = require('./userData') const chance = new Chance() -module.exports = { +const collections = { standardCollection: { id: chance.guid(), title: chance.sentence(), type: 'collection', fragments: [], owners: [submittingAuthor.id], - save: jest.fn(), authors: [ { userId: submittingAuthor.id, @@ -17,6 +16,7 @@ module.exports = { isCorresponding: false, }, ], + save: jest.fn(() => collections.standardCollection), }, authorsCollection: { id: chance.guid(), @@ -27,3 +27,5 @@ module.exports = { save: jest.fn(), }, } + +module.exports = collections