Skip to content

Commit a1c9630

Browse files
committed
More fixes with pagination and the header response
1 parent b176e6d commit a1c9630

File tree

3 files changed

+19
-7
lines changed

3 files changed

+19
-7
lines changed

config/default.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ module.exports = {
66
LOG_LEVEL: process.env.LOG_LEVEL || 'debug',
77
PORT: process.env.PORT || 3000,
88
API_VERSION: process.env.API_VERSION || 'v5',
9+
DEFAULT_PAGE_SIZE: process.env.DEFAULT_PAGE_SIZE || 1000,
910
// used to properly set the header response to api calls for services behind a load balancer
1011
API_BASE_URL: process.env.API_BASE_URL || `http://localhost:3000`,
1112

src/controllers/ResourceController.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ const helper = require('../common/helper')
1313
async function getResources (req, res) {
1414
const result = await service.getResources(req.authUser, req.query.challengeId, req.query.roleId, req.query.page, req.query.perPage)
1515
helper.setResHeaders(req, res, result)
16-
res.send(result)
16+
res.send(result.data)
1717
}
1818

1919
/**
@@ -44,7 +44,7 @@ async function deleteResource (req, res) {
4444
async function listChallengesByMember (req, res) {
4545
const result = await service.listChallengesByMember(req.params.memberId, req.query)
4646
helper.setResHeaders(req, res, result)
47-
res.send(result)
47+
res.send(result.data)
4848
}
4949

5050
module.exports = {

src/services/ResourceService.js

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ async function getResources (currentUser, challengeId, roleId, page, perPage) {
5050
const boolQuery = []
5151
const mustQuery = []
5252
page = page || 1
53-
perPage = perPage || 20
53+
perPage = perPage || config.DEFAULT_PAGE_SIZE
5454

5555
boolQuery.push({ match: { challengeId } })
5656

@@ -81,9 +81,9 @@ async function getResources (currentUser, challengeId, roleId, page, perPage) {
8181
}
8282
}
8383
}
84-
8584
const esClient = await helper.getESClient()
8685
let docs
86+
logger.info(`ES Query ${JSON.stringify(esQuery)}`)
8787
try {
8888
docs = await esClient.search(esQuery)
8989
} catch (e) {
@@ -125,7 +125,12 @@ async function getResources (currentUser, challengeId, roleId, page, perPage) {
125125
}
126126
}
127127

128-
return completeResources
128+
return {
129+
data: completeResources,
130+
total: docs.hits.total,
131+
page,
132+
perPage
133+
}
129134
}
130135

131136
getResources.schema = {
@@ -346,7 +351,7 @@ async function listChallengesByMember (memberId, criteria) {
346351

347352
const boolQuery = []
348353
const mustQuery = []
349-
const perPage = criteria.perPage || 50
354+
const perPage = criteria.perPage || config.DEFAULT_PAGE_SIZE
350355
const page = criteria.page || 1
351356
boolQuery.push({ match: { memberId } })
352357
if (criteria.resourceRoleId) boolQuery.push({ match: { roleId: criteria.resourceRoleId } })
@@ -393,7 +398,13 @@ async function listChallengesByMember (memberId, criteria) {
393398
}
394399
// Extract data from hits
395400
let result = _.map(docs.hits.hits, item => item._source)
396-
return _.uniq(_.map(result, 'challengeId'))
401+
const arr = _.uniq(_.map(result, 'challengeId'))
402+
return {
403+
data: arr,
404+
total: docs.hits.total,
405+
page,
406+
perPage
407+
}
397408
}
398409

399410
listChallengesByMember.schema = {

0 commit comments

Comments
 (0)