diff --git a/src/index.js b/src/index.js index 3936205..895ddbd 100644 --- a/src/index.js +++ b/src/index.js @@ -73,7 +73,7 @@ let checkTags = (listTags) => { return isValid; }; -exports.findMenu = (opts = DEFAULT_MENUS_OPTIONS) => { +let findMenu = (opts = DEFAULT_MENUS_OPTIONS) => { opts.language = opts.language || DEFAULT_MENUS_OPTIONS.language; opts.partOfDay = opts.partOfDay || DEFAULT_MENUS_OPTIONS.partOfDay; @@ -97,7 +97,22 @@ exports.findMenu = (opts = DEFAULT_MENUS_OPTIONS) => { opts.tags = listTags.join(','); } - const url = buildMenuUrl(opts); + return getMenu(opts); +}; + +let getMenu = async function (opts) { + if (opts.restoId && isNaN(opts.restoId)) { + return Promise.reject(new TypeError('Not a valid restoId')); + } + let resto = await findResto(opts.restoId); + if (resto.length > 0) { + const url = buildMenuUrl(opts); + return promGetMenu(url); + } + return Promise.reject(new TypeError('Not a valid restoId')); +}; + +let promGetMenu = (url) => { return new Promise((resolve, reject) => { got(url).then((response) => { let jsonString = escapeTab(response.body); @@ -107,7 +122,7 @@ exports.findMenu = (opts = DEFAULT_MENUS_OPTIONS) => { }); }; -exports.findResto = (id) => { +let findResto = (id) => { const url = buildRestoUrl(id); return new Promise((resolve, reject) => { got(url).then((response) => { @@ -133,3 +148,6 @@ exports.translateTags = (strTags) => { } return translatedList.join(','); }; + +exports.findMenu = findMenu; +exports.findResto = findResto; diff --git a/test/test-findMenu.js b/test/test-findMenu.js index 6bd22d7..205e53e 100644 --- a/test/test-findMenu.js +++ b/test/test-findMenu.js @@ -11,21 +11,21 @@ const epflMenuApi = require('../src/index.js'); describe('epfl-menu-api findMenu', function () { this.timeout(10000); - it('should throw an exception with an invalid date', () => { + it('should throw an exception with the date "30/02/2019"', () => { return epflMenuApi.findMenu({ date: '30/02/2019' }).then(() => { }).catch((err) => err.message.should.equal('Not a valid date')); }); - it('should throw an exception with a language not supported', () => { + it('should throw an exception with the language "de"', () => { return epflMenuApi.findMenu({ language: 'de' }).then(() => { }).catch((err) => err.message.should.equal('Language not supported')); }); - it('should throw an exception with a part of the day not supported', () => { + it('should throw an exception with the part of the day "morning"', () => { return epflMenuApi.findMenu({ 'partOfDay': 'morning' }).then(() => { @@ -34,7 +34,7 @@ describe('epfl-menu-api findMenu', function () { ); }); - it('should throw an exception with a tag not supported', () => { + it('should throw an exception with the tag "Cheese"', () => { return epflMenuApi.findMenu({ 'tags': 'Cheese' }).then(() => { @@ -43,7 +43,25 @@ describe('epfl-menu-api findMenu', function () { ); }); - it('should contains at least 5 menus', function () { + it('should throw an exception with the restoId "Cafétéria BC"', () => { + return epflMenuApi.findMenu({ + 'restoId': 'Cafétéria BC' + }).then(() => { + }).catch( + (err) => err.message.should.equal('Not a valid restoId') + ); + }); + + it('should throw an exception with the restoId "999999"', () => { + return epflMenuApi.findMenu({ + 'restoId': 999999 + }).then(() => { + }).catch( + (err) => err.message.should.equal('Not a valid restoId') + ); + }); + + it('should contains at least 5 menus on 18/04/2019', function () { return epflMenuApi.findMenu({ date: '18/04/2019', restoId: '22',