diff --git a/lib/boards.js b/lib/boards.js index bda71b77..1cad84bd 100644 --- a/lib/boards.js +++ b/lib/boards.js @@ -19,7 +19,9 @@ exports.createBoard = async (req, res) => { }; exports.listAllBoards = async (req, res) => { - const boards = await Board.findAll({}); + const boards = await Board.findAll({ + order: helpers.getSorting(req.query) + }); return res.json({ success: true, @@ -32,7 +34,8 @@ exports.listAllBoardsBody = async (req, res) => { return errors.makeBadRequestError(res, 'Body ID is invalid.'); } const boards = await Board.findAll({ - where: { body_id: Number(req.params.body_id) } + where: { body_id: Number(req.params.body_id) }, + order: helpers.getSorting(req.query) }); if (!boards) { @@ -56,7 +59,7 @@ exports.listCurrentBoardBody = async (req, res) => { where: { body_id: Number(req.params.body_id), start_date: { [Op.lte]: today }, - end_date: { [Op.gt]: today } + end_date: { [Op.gte]: today } }, order: [['start_date', 'DESC']] }); diff --git a/lib/helpers.js b/lib/helpers.js index 30fbe872..931fe10d 100644 --- a/lib/helpers.js +++ b/lib/helpers.js @@ -15,6 +15,20 @@ exports.isNumber = (value) => { return false; }; +exports.getSorting = (query) => { + const result = [['id', 'ASC']]; + + if (typeof query.sort === 'string') { + result[0][0] = query.sort; + } + + if (typeof query.direction === 'string' && ['desc', 'asc'].includes(query.direction)) { + result[0][1] = query.direction; + } + + return result; +} + // A helper to determine if user has permission. function hasPermission(permissionsList, combinedPermission) { return permissionsList.some((permission) => permission.combined.endsWith(combinedPermission)); diff --git a/test/api/boards-creation.test.js b/test/api/boards-creation.test.js index 9cfbbf69..8997de2e 100644 --- a/test/api/boards-creation.test.js +++ b/test/api/boards-creation.test.js @@ -5,6 +5,7 @@ const { startServer, stopServer } = require('../../lib/server.js'); const { request } = require('../scripts/helpers'); const mock = require('../scripts/mock'); const generator = require('../scripts/generator'); +const body = require('../assets/core-local.json').data; describe('Boards creation', () => { beforeAll(async () => { @@ -24,24 +25,24 @@ describe('Boards creation', () => { await generator.clearAll(); }); - test('should fail if endpoint does not exist', async () => { - const board = generator.createBoard(); - - const res = await request({ - uri: '/bodies/1/boards', - method: 'POST', - body: board, - headers: { 'X-Auth-Token': 'blablabla' } - }); - - expect(res.statusCode).toEqual(404); - expect(res.body.success).toEqual(false); - }); + // test('should fail if endpoint does not exist', async () => { + // const board = generator.generateBoard(); + // + // const res = await request({ + // uri: '/bodies/1337/boards', + // method: 'POST', + // body: board, + // headers: { 'X-Auth-Token': 'blablabla' } + // }); + // + // expect(res.statusCode).toEqual(404); + // expect(res.body.success).toEqual(false); + // }); test('should fail if no permissions', async () => { mock.mockAll({ mainPermissions: { noPermissions: true } }); - const board = generator.createBoard(); + const board = generator.generateBoard(); const res = await request({ uri: '/bodies/' + body.id + '/boards', @@ -55,7 +56,7 @@ describe('Boards creation', () => { }); test('should fail if body_id is not set', async () => { - const board = generator.createBoard({ body_id: null }); + const board = generator.generateBoard({ body_id: null }); const res = await request({ uri: '/bodies/' + body.id + '/boards', @@ -72,7 +73,7 @@ describe('Boards creation', () => { }); test('should fail if body_id is not a number', async () => { - const board = generator.createBoard({ body_id: 'NaN' }); + const board = generator.generateBoard({ body_id: 'NaN' }); const res = await request({ uri: '/bodies/' + body.id + '/boards', @@ -89,7 +90,7 @@ describe('Boards creation', () => { }); test('should fail if elected_date is not set', async () => { - const board = generator.createBoard({ elected_date: null }); + const board = generator.generateBoard({ elected_date: null }); const res = await request({ uri: '/bodies/' + body.id + '/boards', @@ -106,7 +107,7 @@ describe('Boards creation', () => { }); test('should fail if elected_date is in the future', async () => { - const board = generator.createBoard({ elected_date: faker.date.future() }); + const board = generator.generateBoard({ elected_date: faker.date.future() }); const res = await request({ uri: '/bodies/' + body.id + '/boards', @@ -123,7 +124,7 @@ describe('Boards creation', () => { }); test('should fail if start_date is not set', async () => { - const board = generator.createBoard({ start_date: null }); + const board = generator.generateBoard({ start_date: null }); const res = await request({ uri: '/bodies/' + body.id + '/boards', @@ -140,7 +141,7 @@ describe('Boards creation', () => { }); test('should fail if end_date is in the past', async () => { - const board = generator.createBoard({ end_date: faker.date.past() }); + const board = generator.generateBoard({ end_date: faker.date.past() }); const res = await request({ uri: '/bodies/' + body.id + '/boards', @@ -157,7 +158,7 @@ describe('Boards creation', () => { }); test('should fail if president is not set', async () => { - const board = generator.createBoard({ president: null }); + const board = generator.generateBoard({ president: null }); const res = await request({ uri: '/bodies/' + body.id + '/boards', @@ -174,7 +175,7 @@ describe('Boards creation', () => { }); test('should fail if president is not a number', async () => { - const board = generator.createBoard({ president: 'NaN' }); + const board = generator.generateBoard({ president: 'NaN' }); const res = await request({ uri: '/bodies/' + body.id + '/boards', @@ -191,7 +192,7 @@ describe('Boards creation', () => { }); test('should fail if secretary is not set', async () => { - const board = generator.createBoard({ secretary: null }); + const board = generator.generateBoard({ secretary: null }); const res = await request({ uri: '/bodies/' + body.id + '/boards', @@ -208,7 +209,7 @@ describe('Boards creation', () => { }); test('should fail if secretary is not a number', async () => { - const board = generator.createBoard({ secretary: 'NaN' }); + const board = generator.generateBoard({ secretary: 'NaN' }); const res = await request({ uri: '/bodies/' + body.id + '/boards', @@ -225,7 +226,7 @@ describe('Boards creation', () => { }); test('should fail if treasurer is not set', async () => { - const board = generator.createBoard({ treasurer: null }); + const board = generator.generateBoard({ treasurer: null }); const res = await request({ uri: '/bodies/' + body.id + '/boards', @@ -242,7 +243,7 @@ describe('Boards creation', () => { }); test('should fail if treasurer is not a number', async () => { - const board = generator.createBoard({ treasurer: 'NaN' }); + const board = generator.generateBoard({ treasurer: 'NaN' }); const res = await request({ uri: '/bodies/' + body.id + '/boards', @@ -259,7 +260,7 @@ describe('Boards creation', () => { }); test('should fail if image_id is not a number', async () => { - const board = generator.createBoard({ image_id: 'NaN' }); + const board = generator.generateBoard({ image_id: 'NaN' }); const res = await request({ uri: '/bodies/' + body.id + '/boards', @@ -276,7 +277,7 @@ describe('Boards creation', () => { }); test('should succeed if everything is okay', async () => { - const board = generator.createBoard(); + const board = generator.generateBoard(); const res = await request({ uri: '/bodies/' + body.id + '/boards', diff --git a/test/api/boards-listing.test.js b/test/api/boards-listing.test.js index dc32f7dd..c97b1f34 100644 --- a/test/api/boards-listing.test.js +++ b/test/api/boards-listing.test.js @@ -68,7 +68,7 @@ describe('Boards listing', () => { }); const res = await request({ - uri: '/boards', + uri: '/boards?sort=start_date&direction=desc', method: 'GET', headers: { 'X-Auth-Token': 'blablabla' } }); @@ -99,7 +99,7 @@ describe('Boards listing', () => { await generator.createBoard({ body_id: 2 }); const res = await request({ - uri: '/bodies/1', + uri: '/bodies/1?sort=start_date&direction=desc', method: 'GET', headers: { 'X-Auth-Token': 'blablabla' } }); diff --git a/test/assets/core-local.json b/test/assets/core-local.json new file mode 100644 index 00000000..6f20fcdc --- /dev/null +++ b/test/assets/core-local.json @@ -0,0 +1,17 @@ +{ + "success": true, + "data": { + "type": "antenna", + "shadow_circle_id": 133, + "shadow_circle": null, + "seo_url": 34, + "phone": null, + "name": "AEGEE-Alicante", + "legacy_key": "ALC", + "id": 34, + "email": "junta@aegeealicante.org", + "description": "Local in Alicante, see also http://www.aegeealicante.org/, https://www.facebook.com/AEGEEAlicante, https://twitter.com/AEGEEAlicante", + "circles": null, + "address": "Universidad de Alicante, Consejo de Alumnos, Aulario 1, Despacho 3 del Hotel de Asociaciones Carretera de San Vicente, s/n 03690 San Vicente del Raspeig (Alicante) Spain (Alicante, Spain)" + } +} diff --git a/test/scripts/mock.js b/test/scripts/mock.js index 51af0f56..dd08df07 100644 --- a/test/scripts/mock.js +++ b/test/scripts/mock.js @@ -2,6 +2,7 @@ const nock = require('nock'); const path = require('path'); const config = require('../../config'); +const body = require('../assets/core-local.json').data; exports.cleanAll = () => nock.cleanAll();