Skip to content
Snippets Groups Projects
Commit e8f887be authored by Sebastian Mihalache's avatar Sebastian Mihalache :hammer_pick:
Browse files

feat(manuscript-manager): add get collections endpoint

parent ac489063
No related branches found
No related tags found
2 merge requests!110Sprint 21 Features,!66Hin 1020
......@@ -36,7 +36,7 @@ const teams = {
type: 'fragment',
id: fragment.id,
},
members: [reviewer.id, inactiveReviewer.id, handlingEditor.id],
members: [reviewer.id, inactiveReviewer.id],
save: jest.fn(() => teams.revTeam),
updateProperties: jest.fn(() => teams.revTeam),
id: revTeamID,
......
......@@ -19,7 +19,6 @@ module.exports = models => async (req, res) => {
res.status(400).json({ error: `Role ${role} is invalid` })
return
}
const { collectionId, fragmentId } = req.params
const teamHelper = new Team({
TeamModel: models.Team,
......
......@@ -3,5 +3,6 @@ module.exports = {
require('./src/FragmentsRecommendations')(app)
require('./src/Fragments')(app)
require('./src/TechnicalChecks')(app)
require('./src/Collections')(app)
},
}
const bodyParser = require('body-parser')
const Collections = app => {
app.use(bodyParser.json())
const routePath = './routes/collections'
const authBearer = app.locals.passport.authenticate('bearer', {
session: false,
})
/**
* @api {get} /api/fragments Get latest fragments
* @apiGroup Fragments
* @apiSuccessExample {json} Success
* HTTP/1.1 200 OK
* [
* {
* "type": "collection",
* "status": "draft",
* ...,
* currentVersion:
* {
* "type": "fragment",
* "files": {},
* "owners": [
* "2494cbdf-06a8-4403-999b-ae93e23f32c8",
* "ed39345d-a95f-46d8-b5e1-75a2744beefe"
* ],
* ...
* }
* }
* ]
* @apiErrorExample {json} Get fragments errors
* HTTP/1.1 403 Forbidden
*/
app.get(
'/api/collections',
authBearer,
require(`${routePath}/get`)(app.locals.models),
)
}
module.exports = Collections
......@@ -49,29 +49,6 @@ const Fragments = app => {
authBearer,
require(`${routePath}/post`)(app.locals.models),
)
/**
* @api {get} /api/fragments Get latest fragments
* @apiGroup Fragments
* @apiSuccessExample {json} Success
* HTTP/1.1 200 OK
* [
* {
* "type": "fragment",
* "files": {},
* "owners": [
* "2494cbdf-06a8-4403-999b-ae93e23f32c8",
* "ed39345d-a95f-46d8-b5e1-75a2744beefe"
* ],
* }
* ]
* @apiErrorExample {json} Get fragments errors
* HTTP/1.1 403 Forbidden
*/
app.get(
'/api/fragments',
authBearer,
require(`${routePath}/get`)(app.locals.models),
)
}
module.exports = Fragments
......@@ -8,13 +8,13 @@ module.exports = models => async (req, res) => {
path: req.route.path,
}
const fragments = await authsome.can(req.user, 'GET', target)
const collections = await authsome.can(req.user, 'GET', target)
if (!fragments) {
if (!collections) {
return res.status(403).json({
error: 'Unauthorized.',
})
}
res.status(200).json(fragments)
res.status(200).json(collections)
}
......@@ -10,11 +10,11 @@ jest.mock('@pubsweet/component-send-email', () => ({
send: jest.fn(),
}))
const path = '../routes/fragments/get'
const path = '../routes/collections/get'
const route = {
path: '/api/fragments',
path: '/api/collections',
}
describe('Get fragments route handler', () => {
describe('Get collections route handler', () => {
let testFixtures = {}
let models
beforeEach(() => {
......@@ -22,7 +22,7 @@ describe('Get fragments route handler', () => {
models = Model.build(testFixtures)
})
it('should return latest fragments if the request user is HE', async () => {
it('should return collections with the latest fragments if the request user is HE', async () => {
const { handlingEditor } = testFixtures.users
const res = await requests.sendRequest({
......@@ -36,11 +36,13 @@ describe('Get fragments route handler', () => {
const data = JSON.parse(res._getData())
expect(data).toHaveLength(1)
expect(data[0].type).toEqual('fragment')
expect(data[0].recommendations).toHaveLength(3)
expect(data[0].type).toEqual('collection')
expect(data[0]).toHaveProperty('currentVersion')
expect(data[0].currentVersion.type).toEqual('fragment')
expect(data[0].currentVersion.recommendations).toHaveLength(3)
})
it('should return latest fragments if the request user is reviewer', async () => {
it('should return collections with the latest fragments if the request user is reviewer', async () => {
const { recReviewer } = testFixtures.users
const res = await requests.sendRequest({
......@@ -54,9 +56,9 @@ describe('Get fragments route handler', () => {
const data = JSON.parse(res._getData())
expect(data).toHaveLength(1)
expect(data[0].type).toEqual('fragment')
expect(data[0].recommendations).toHaveLength(1)
expect(data[0].authors[0]).not.toHaveProperty('email')
expect(data[0].type).toEqual('collection')
expect(data[0].currentVersion.recommendations).toHaveLength(1)
expect(data[0].currentVersion.authors[0]).not.toHaveProperty('email')
})
it('should return an error if the request user is unauthenticated', async () => {
......@@ -72,7 +74,7 @@ describe('Get fragments route handler', () => {
expect(data.error).toEqual('Unauthorized.')
})
it('should return all fragments if the request user is admin/EiC', async () => {
it('should return all collections with the latest fragments if the request user is admin/EiC', async () => {
const { editorInChief } = testFixtures.users
const res = await requests.sendRequest({
......@@ -86,7 +88,7 @@ describe('Get fragments route handler', () => {
const data = JSON.parse(res._getData())
expect(data).toHaveLength(2)
expect(data[0].type).toEqual('fragment')
expect(data[0].recommendations).toHaveLength(3)
expect(data[0].type).toEqual('collection')
expect(data[0].currentVersion.recommendations).toHaveLength(3)
})
})
......@@ -203,21 +203,29 @@ const getUsersList = async ({ UserModel, user }) => {
const parseUser = ({ user }) => omit(user, sensitiveUserProperties)
const getFragments = async ({ user, models }) => {
const getCollections = async ({ user, models }) => {
const userPermisssions = await getUserPermissions({ user, Team: models.Team })
return Promise.all(
userPermisssions.map(async up => {
let fragment = {}
let collection = {}
if (up.objectType === 'collection') {
const coll = await models.Collection.find(up.objectId)
const latestFragmentId = coll.fragments[coll.fragments.length - 1]
fragment = await models.Fragment.find(latestFragmentId)
collection = await models.Collection.find(up.objectId)
const latestFragmentId =
collection.fragments[collection.fragments.length - 1]
collection.currentVersion = await models.Fragment.find(latestFragmentId)
} else {
fragment = await models.Fragment.find(up.objectId)
collection = await models.Collection.find(fragment.collectionId)
collection.currentVersion = stripeFragmentByRole({
fragment,
role: up.role,
user,
})
}
return stripeFragmentByRole({ fragment, role: up.role, user })
return collection
}),
)
}
......@@ -239,5 +247,5 @@ module.exports = {
stripeFragmentByRole,
getUsersList,
parseUser,
getFragments,
getCollections,
}
......@@ -173,8 +173,8 @@ async function applyAuthenticatedUserPolicy(user, operation, object, context) {
return helpers.parseUser({ user: object })
}
if (get(object, 'path') === '/api/fragments') {
return helpers.getFragments({
if (get(object, 'path') === '/api/collections') {
return helpers.getCollections({
user,
models: context.models,
})
......@@ -307,12 +307,15 @@ async function applyEditorInChiefPolicy(user, operation, object, context) {
return helpers.parseUser({ user: object })
}
if (get(object, 'path') === '/api/fragments') {
if (get(object, 'path') === '/api/collections') {
const collections = await context.models.Collection.all()
return Promise.all(
collections.map(coll => {
collections.map(async coll => {
const latestFragmentId = coll.fragments[coll.fragments.length - 1]
return context.models.Fragment.find(latestFragmentId)
coll.currentVersion = await context.models.Fragment.find(
latestFragmentId,
)
return coll
}),
)
}
......
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