Skip to content
Snippets Groups Projects
Commit fd5d9f93 authored by Sebastian's avatar Sebastian
Browse files

feat(component-user-manager): add patch to coll users

parent 9f783b80
No related branches found
No related tags found
1 merge request!5Refactor component invite
...@@ -18,6 +18,11 @@ const CollectionsInvitations = app => { ...@@ -18,6 +18,11 @@ const CollectionsInvitations = app => {
require(`${routePath}/delete`)(app.locals.models), require(`${routePath}/delete`)(app.locals.models),
) )
app.get(basePath, authBearer, require(`${routePath}/get`)(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 module.exports = CollectionsInvitations
...@@ -81,17 +81,23 @@ const removeTeamMember = async (teamId, userId, TeamModel) => { ...@@ -81,17 +81,23 @@ const removeTeamMember = async (teamId, userId, TeamModel) => {
const getTeamMembersByCollection = async (collectionId, role, TeamModel) => { const getTeamMembersByCollection = async (collectionId, role, TeamModel) => {
const teams = await TeamModel.all() const teams = await TeamModel.all()
const members = get( // const members = get(
teams.find( // teams.find(
team => // team =>
team.group === role && // team.group === role &&
team.object.type === 'collection' && // team.object.type === 'collection' &&
team.object.id === collectionId, // team.object.id === collectionId,
), // ),
'members', // '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) => { const getTeamByGroupAndCollection = async (collectionId, role, TeamModel) => {
......
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,
})
}
}
...@@ -7,10 +7,6 @@ const Model = require('./../helpers/Model') ...@@ -7,10 +7,6 @@ const Model = require('./../helpers/Model')
const models = Model.build() const models = Model.build()
const notFoundError = new Error()
notFoundError.name = 'NotFoundError'
notFoundError.status = 404
const { author, submittingAuthor } = fixtures.users const { author, submittingAuthor } = fixtures.users
const { standardCollection } = fixtures.collections const { standardCollection } = fixtures.collections
const { authorTeam } = fixtures.teams const { authorTeam } = fixtures.teams
......
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')
})
})
...@@ -13,10 +13,6 @@ jest.mock('pubsweet-component-mail-service', () => ({ ...@@ -13,10 +13,6 @@ jest.mock('pubsweet-component-mail-service', () => ({
})) }))
const chance = new Chance() const chance = new Chance()
const notFoundError = new Error()
notFoundError.name = 'NotFoundError'
notFoundError.status = 404
const { author, submittingAuthor } = fixtures.users const { author, submittingAuthor } = fixtures.users
const { standardCollection } = fixtures.collections const { standardCollection } = fixtures.collections
const postPath = '../../routes/collectionsUsers/post' const postPath = '../../routes/collectionsUsers/post'
......
...@@ -2,14 +2,13 @@ const Chance = require('chance') ...@@ -2,14 +2,13 @@ const Chance = require('chance')
const { submittingAuthor } = require('./userData') const { submittingAuthor } = require('./userData')
const chance = new Chance() const chance = new Chance()
module.exports = { const collections = {
standardCollection: { standardCollection: {
id: chance.guid(), id: chance.guid(),
title: chance.sentence(), title: chance.sentence(),
type: 'collection', type: 'collection',
fragments: [], fragments: [],
owners: [submittingAuthor.id], owners: [submittingAuthor.id],
save: jest.fn(),
authors: [ authors: [
{ {
userId: submittingAuthor.id, userId: submittingAuthor.id,
...@@ -17,6 +16,7 @@ module.exports = { ...@@ -17,6 +16,7 @@ module.exports = {
isCorresponding: false, isCorresponding: false,
}, },
], ],
save: jest.fn(() => collections.standardCollection),
}, },
authorsCollection: { authorsCollection: {
id: chance.guid(), id: chance.guid(),
...@@ -27,3 +27,5 @@ module.exports = { ...@@ -27,3 +27,5 @@ module.exports = {
save: jest.fn(), save: jest.fn(),
}, },
} }
module.exports = collections
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