Skip to content

Commit

Permalink
added actual and exclude optionals for v1
Browse files Browse the repository at this point in the history
  • Loading branch information
pjnovas committed Sep 9, 2016
1 parent 85db8a3 commit c3ea25e
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 10 deletions.
50 changes: 43 additions & 7 deletions src/controller/v1.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import Joi from 'joi';
import Boom from 'boom';
import chain from './chain';

import { holidaysV1 as reducer } from '../reducers';
import { holidaysV1 as loader } from '../loaders';
Expand All @@ -10,20 +11,55 @@ import holidays, { fijos, ref } from '../data/holidays';
exports.next = {
handler: (req, reply) => reply().code(204)
};
*/

exports.current = {
handler: (req, reply) => reply().code(204)
const setHolidays = (request, reply) => {
let plain = reducer(fijos, holidays[`h${request.year}`]);
request.holidays = loader(plain, ref);
reply.next();
};

const excludeOptionals = (request, reply) => {
if (request.query.excluir === 'opcional'){
request.holidays = request.holidays.filter( holiday => !holiday.opcional);
}

reply.next();
};
*/

exports.year = {
validate: {
params: {
year: Joi.number().min(2011).required()
},
query: {
excluir: Joi.any().only('opcional')
}
},
handler: ({ params }, reply) => {
let plain = reducer(fijos, holidays[`h${params.year}`]);
reply(loader(plain, ref)).code(200);
}
handler: chain(
(request, reply) => {
request.year = request.params.year;
reply.next();
},
setHolidays,
excludeOptionals,
(request, reply) => reply(request.holidays).code(200)
)
};

exports.current = {
validate: {
query: {
excluir: Joi.any().only('opcional')
}
},
handler: chain(
(request, reply) => {
request.year = new Date().getFullYear();
reply.next();
},
setHolidays,
excludeOptionals,
(request, reply) => reply(request.holidays).code(200)
)
};
4 changes: 2 additions & 2 deletions src/routes/api/v1.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@

import {
//next,
//current,
current,
year
} from '../../controller/v1';

const BASE = '/api/v1';

export default [
//{ method: 'GET', path: `${BASE}/proximo`, config: next },
//{ method: 'GET', path: `${BASE}/actual`, config: current },
{ method: 'GET', path: `${BASE}/actual`, config: current },
{ method: 'GET', path: `${BASE}/{year}`, config: year }
];
48 changes: 47 additions & 1 deletion test/api/v1.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,53 @@ describe('/api/v1/', () => {
await tryYear(nextYear, expected);
});

it('must NOT return optional holidays when filter "excluir" is opcional');
it('must NOT return optional holidays when filter "excluir" is opcional', done => {
const year = 2016;

const plain = reducer(fijos, holidays[`h${year}`]);
const result = loader(plain, ref);

const expected = result.filter( holiday => !holiday.opcional);

chai
.request(server.listener)
.get(`${baseURL}/${year}?excluir=opcional`)
.end((err, res) => {
expect(res.status).to.be.equal(200);
expect(res.body).to.be.an('array');
expect(_.isEqual(res.body, expected)).to.be.true;
done();
});
});

});

describe('GET /actual', () => {
const actualYear = new Date().getFullYear();

it('must return holidays for the current year', async () => {
const plain = reducer(fijos, holidays[`h${actualYear}`]);
const expected = loader(plain, ref);

await tryYear('actual', expected);
});

it('must NOT return optional holidays when filter "excluir" is opcional', done => {
const plain = reducer(fijos, holidays[`h${actualYear}`]);
const result = loader(plain, ref);

const expected = result.filter( holiday => !holiday.opcional);

chai
.request(server.listener)
.get(`${baseURL}/actual?excluir=opcional`)
.end((err, res) => {
expect(res.status).to.be.equal(200);
expect(res.body).to.be.an('array');
expect(_.isEqual(res.body, expected)).to.be.true;
done();
});
});

});

Expand Down

0 comments on commit c3ea25e

Please sign in to comment.