Skip to content

Commit

Permalink
Merge pull request #84 from nearform/pagination
Browse files Browse the repository at this point in the history
Added pagination implementation on list user service. See #80
  • Loading branch information
mcdonnelldean committed Jan 27, 2016
2 parents 162996e + cb71072 commit 0d0c1cb
Show file tree
Hide file tree
Showing 3 changed files with 139 additions and 5 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"history": "1.17.x",
"inert": "3.x.x",
"influx": "4.0.x",
"jsonic": "0.2.2",
"lodash": "3.10.1",
"material-ui": "0.14.x",
"moment": "2.11.x",
Expand Down
33 changes: 31 additions & 2 deletions server/concorda.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

const _ = require('lodash')
const Async = require('async')
var Jsonic = require('jsonic')

module.exports = function (opts) {
var seneca = this
Expand All @@ -12,7 +13,35 @@ module.exports = function (opts) {
options = _.extend(options, opts || {})

function listUsers (msg, response) {
this.make$('sys', 'user').list$({}, function (err, users) {
// @ToDo - change msg.req$.query.* to msg.* after seneca-web is published with latest version
var limit = msg.req$.query.limit
var skip = msg.req$.query.skip
var orderBy = msg.req$.query.order

var q = {}

if (limit) {
q['limit$'] = limit
}
if (skip) {
q['skip$'] = skip
}

if (orderBy) {
if (_.isObject(orderBy)) {
q['sort$'] = orderBy
}
else {
try {
orderBy = orderBy.replace(/%22/g, '\"').replace(/%20/g, ' ')
q['sort$'] = Jsonic(orderBy)
}
catch (e) {
}
}
}

this.make$('sys', 'user').list$(q, function (err, users) {
if (err) {
return response(null, {ok: false, why: err})
}
Expand Down Expand Up @@ -111,7 +140,7 @@ module.exports = function (opts) {
}

seneca
.use('mesh',{auto:true, pin:'role:user'})
.use('mesh', {auto: true, pin: 'role: user'})

seneca
.add({role: options.name, cmd: 'closeSession'}, closeUserSessions)
Expand Down
110 changes: 107 additions & 3 deletions test/hapi.user.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ suite('Hapi user suite tests ', () => {
}, (res) => {
Assert.equal(200, res.statusCode)

Assert.equal(false, JSON.parse(res.payload).err)
Assert.equal(true, JSON.parse(res.payload).ok)
Assert(JSON.parse(res.payload).data)
Assert.equal(user2.name, JSON.parse(res.payload).data.name)

Expand All @@ -92,10 +92,10 @@ suite('Hapi user suite tests ', () => {
})
})

let newName = 'newName'
test('update another user test', (done) => {
let url = '/api/user'

let newName = 'newName'
user2.name = newName
server.inject({
url: url,
Expand All @@ -105,11 +105,115 @@ suite('Hapi user suite tests ', () => {
}, (res) => {
Assert.equal(200, res.statusCode)

Assert.equal(false, JSON.parse(res.payload).err)
Assert.equal(true, JSON.parse(res.payload).ok)
Assert(JSON.parse(res.payload).data)
Assert.equal(newName, JSON.parse(res.payload).data.name)

done()
})
})

test('list user test with name sorted ASC', (done) => {
let url = '/api/user?order={name: 1}'

server.inject({
url: url,
method: 'GET',
headers: { cookie: 'seneca-login=' + cookie }
}, function (res) {
Assert.equal(200, res.statusCode)
Assert.equal(2, JSON.parse(res.payload).data.length)
Assert.equal(2, JSON.parse(res.payload).count)

Assert.equal(newName, JSON.parse(res.payload).data[0].name)

done()
})
})

test('list user test with name sorted DESC', (done) => {
let url = '/api/user?order={name: -1}'

server.inject({
url: url,
method: 'GET',
headers: { cookie: 'seneca-login=' + cookie }
}, function (res) {
Assert.equal(200, res.statusCode)
Assert.equal(2, JSON.parse(res.payload).data.length)
Assert.equal(2, JSON.parse(res.payload).count)

Assert.equal(user.name, JSON.parse(res.payload).data[0].name)

done()
})
})

test('list user test with name sorted DESC and limit 1', (done) => {
let url = '/api/user?order={name: -1}&limit=1'

server.inject({
url: url,
method: 'GET',
headers: { cookie: 'seneca-login=' + cookie }
}, function (res) {
Assert.equal(200, res.statusCode)
Assert.equal(1, JSON.parse(res.payload).data.length)
Assert.equal(1, JSON.parse(res.payload).count)

Assert.equal(user.name, JSON.parse(res.payload).data[0].name)

done()
})
})

test('list user test with name sorted DESC and limit 1 and skip 1', (done) => {
let url = '/api/user?order={name: -1}&limit=1&skip=1'

server.inject({
url: url,
method: 'GET',
headers: { cookie: 'seneca-login=' + cookie }
}, function (res) {
Assert.equal(200, res.statusCode)
Assert.equal(1, JSON.parse(res.payload).data.length)
Assert.equal(1, JSON.parse(res.payload).count)

Assert.equal(newName, JSON.parse(res.payload).data[0].name)

done()
})
})

test('list user test with name sorted DESC and limit 1 and skip 10', (done) => {
let url = '/api/user?order={name: -1}&limit=1&skip=10'

server.inject({
url: url,
method: 'GET',
headers: { cookie: 'seneca-login=' + cookie }
}, function (res) {
Assert.equal(200, res.statusCode)
Assert.equal(0, JSON.parse(res.payload).data.length)
Assert.equal(0, JSON.parse(res.payload).count)

done()
})
})

test('list user test with name sorted DESC and limit 1 and skip 10', (done) => {
let url = '/api/user?order={name: -1}&limit=1&skip=10'

server.inject({
url: url,
method: 'GET',
headers: { cookie: 'seneca-login=' + cookie }
}, function (res) {
Assert.equal(200, res.statusCode)
Assert.equal(0, JSON.parse(res.payload).data.length)
Assert.equal(0, JSON.parse(res.payload).count)

done()
})
})
})

0 comments on commit 0d0c1cb

Please sign in to comment.