Commit 2667c317 authored by Sam Galson's avatar Sam Galson

refactor to use every and rename func

parent 16b49024
......@@ -14,7 +14,7 @@ const express = require('express')
const api = express.Router()
const passport = require('passport')
const sse = require('pubsweet-sse')
const { filterByValues, objectId, buildChangeData, fieldSelector, authorizationError, getTeams } = require('./util')
const { createFilterFromQuery, objectId, buildChangeData, fieldSelector, authorizationError, getTeams } = require('./util')
const authBearer = passport.authenticate('bearer', { session: false })
const authBearerAndPublic = passport.authenticate(['bearer', 'anonymous'], { session: false })
......@@ -95,7 +95,7 @@ api.get('/collections', authBearerAndPublic, async (req, res, next) => {
const properties = await applyPermissionFilter(req, collection)
return fieldSelector(req)(properties)
})))
.filter(filterByValues(req))
.filter(createFilterFromQuery(req.query))
res.status(STATUS.OK).json(collectionsWithSelectedFields)
} catch (err) {
......@@ -239,7 +239,7 @@ api.get('/collections/:collectionId/fragments', authBearerAndPublic, async (req,
fragments = fragments
.map(fieldSelector(req))
.filter(filterByValues(req))
.filter(createFilterFromQuery(req.query))
return res.status(STATUS.OK).json(fragments)
} catch (err) {
......@@ -260,7 +260,7 @@ api.get('/collections/:collectionId/teams', authBearerAndPublic, async (req, res
id: collection.id,
type: 'collection'
}))
.filter(filterByValues(req))
.filter(createFilterFromQuery(req.query))
res.status(STATUS.OK).json(teams)
} catch (err) {
......@@ -331,7 +331,7 @@ api.get('/collections/:collectionId/fragments/:fragmentId/teams', authBearerAndP
id: fragment.id,
type: 'fragment'
}))
.filter(filterByValues(req))
.filter(createFilterFromQuery(req.query))
res.status(STATUS.OK).json(teams)
} catch (err) {
......
......@@ -5,7 +5,7 @@ const passport = require('passport')
const authsome = require('../helpers/authsome')
const Team = require('../models/Team')
const { filterByValues, authorizationError } = require('./util')
const { createFilterFromQuery, authorizationError } = require('./util')
const authBearer = passport.authenticate('bearer', { session: false })
const api = express.Router({mergeParams: true})
......@@ -23,7 +23,7 @@ api.get('/teams', authBearer, async (req, res, next) => {
}
const teams = (await Team.all())
.filter(filterByValues(req))
.filter(createFilterFromQuery(req.query))
res.status(STATUS.OK).json(teams)
} catch (err) {
......
......@@ -7,7 +7,7 @@ const express = require('express')
const User = require('../models/User')
const authsome = require('../helpers/authsome')
const { filterByValues, authorizationError } = require('./util')
const { createFilterFromQuery, authorizationError } = require('./util')
const Team = require('../models/Team')
const AuthorizationError = require('../errors/AuthorizationError')
......@@ -64,7 +64,7 @@ api.get('/users', authBearer, async (req, res, next) => {
}
const users = (await User.all())
.filter(filterByValues(req))
.filter(createFilterFromQuery(req.query))
return res.status(STATUS.OK).json({users: users})
} catch (err) {
......
......@@ -24,21 +24,14 @@ module.exports = {
return data
},
filterByValues: req => {
const filterPaths = _.difference(_.keys(req.query), ['fields'])
createFilterFromQuery: query => {
const filterPaths = _.difference(_.keys(query), ['fields'])
return (item) => {
for (let filterPath of filterPaths) {
const filterValue = req.query[filterPath]
const filterValueMatch = _.has(item, filterPath) && _.get(item, filterPath) === filterValue
if ( !filterValueMatch ) {
return false
}
}
return true
return filterPaths.every(filterPath => {
return _.has(item, filterPath) && _.get(item, filterPath) === query[filterPath]
})
}
},
fieldSelector: req => {
const fields = req.query.fields ? req.query.fields.split(/\s*,\s*/) : null
......
Markdown is supported
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