From 3002c9b7fd8bd0a374cca22d2a4db5d9b6efca34 Mon Sep 17 00:00:00 2001 From: Matteo Pagliazzi Date: Sat, 26 Mar 2016 16:25:15 +0100 Subject: [PATCH] v3: port getBuyList and shared.updateStore --- common/script/libs/updateStore.js | 47 +++++++-------- tasks/gulp-eslint.js | 1 - .../user/GET-user_inventory_buy.test.js | 26 +++++++++ test/common/libs/updateStore.js | 57 +++++++++++++++++++ website/src/controllers/api-v3/user.js | 26 +++++++++ 5 files changed, 130 insertions(+), 27 deletions(-) create mode 100644 test/api/v3/integration/user/GET-user_inventory_buy.test.js create mode 100644 test/common/libs/updateStore.js diff --git a/common/script/libs/updateStore.js b/common/script/libs/updateStore.js index 05d009fd9d..f6593de8fd 100644 --- a/common/script/libs/updateStore.js +++ b/common/script/libs/updateStore.js @@ -1,36 +1,31 @@ import _ from 'lodash'; import content from '../content/index'; -/* - Update the in-browser store with new gear. FIXME this was in user.fns, but it was causing strange issues there - */ +// Return the list of gear items available for purchase -var sortOrder = _.reduce(content.gearTypes, (function(m, v, k) { - m[v] = k; - return m; -}), {}); +let sortOrder = _.reduce(content.gearTypes, (accumulator, val, key) => { + accumulator[val] = key; + return accumulator; +}, {}); -module.exports = function(user) { - var changes; - if (!user) { - return; - } - changes = []; - _.each(content.gearTypes, function(type) { - var found; - found = _.find(content.gear.tree[type][user.stats["class"]], function(item) { +module.exports = function updateStore (user) { + let changes = []; + + _.each(content.gearTypes, (type) => { + let found = _.find(content.gear.tree[type][user.stats.class], (item) => { return !user.items.gear.owned[item.key]; }); - if (found) { - changes.push(found); + + if (found) changes.push(found); + }); + + changes = changes.concat(_.filter(content.gear.flat, (val) => { + if (['special', 'mystery', 'armoire'].indexOf(val.klass) !== -1 && !user.items.gear.owned[val.key] && (val.canOwn ? val.canOwn(user) : false)) { + return true; + } else { + return false; } - return true; - }); - changes = changes.concat(_.filter(content.gear.flat, function(v) { - var ref; - return ((ref = v.klass) === 'special' || ref === 'mystery' || ref === 'armoire') && !user.items.gear.owned[v.key] && (typeof v.canOwn === "function" ? v.canOwn(user) : void 0); })); - return _.sortBy(changes, function(c) { - return sortOrder[c.type]; - }); + + return _.sortBy(changes, (change) => sortOrder[change.type]); }; diff --git a/tasks/gulp-eslint.js b/tasks/gulp-eslint.js index 1943e8bd78..6fb94f6d2b 100644 --- a/tasks/gulp-eslint.js +++ b/tasks/gulp-eslint.js @@ -74,7 +74,6 @@ const COMMON_FILES = [ '!./common/script/libs/splitWhitespace.js', '!./common/script/libs/taskClasses.js', '!./common/script/libs/taskDefaults.js', - '!./common/script/libs/updateStore.js', '!./common/script/libs/uuid.js', '!./common/script/public/**/*.js', ]; diff --git a/test/api/v3/integration/user/GET-user_inventory_buy.test.js b/test/api/v3/integration/user/GET-user_inventory_buy.test.js new file mode 100644 index 0000000000..fd2a25b4ee --- /dev/null +++ b/test/api/v3/integration/user/GET-user_inventory_buy.test.js @@ -0,0 +1,26 @@ +import { + generateUser, + translate as t, +} from '../../../../helpers/api-integration/v3'; + +describe('GET /user/inventory/buy', () => { + let user; + + before(async () => { + user = await generateUser(); + }); + + // More tests in common code unit tests + + it('returns the gear items available for purchase', async () => { + let buyList = await user.get('/user/inventory/buy'); + + expect(_.find(buyList, item => { + return item.text === t('armorWarrior1Text'); + })).to.exist; + + expect(_.find(buyList, item => { + return item.text === t('armorWarrior2Text'); + })).to.not.exist; + }); +}); diff --git a/test/common/libs/updateStore.js b/test/common/libs/updateStore.js new file mode 100644 index 0000000000..97d00076af --- /dev/null +++ b/test/common/libs/updateStore.js @@ -0,0 +1,57 @@ +import shared from '../../../common'; +import { + generateUser, +} from '../../helpers/common.helper'; +import i18n from '../../../common/script/i18n'; + +describe('updateStore', () => { + context('returns a list of gear items available for purchase', () => { + let user = generateUser(); + user.items.gear.owned.armor_armoire_lunarArmor = false; // eslint-disable-line camelcase + user.contributor.level = 2; + user.purchased.plan.mysteryItems = ['armor_mystery_201402']; + user.items.gear.owned.armor_mystery_201402 = false; // eslint-disable-line camelcase + + let list = shared.updateStore(user); + + it('contains the first item not purchased for each gear type', () => { + expect(_.find(list, item => { + return item.text() === i18n.t('armorWarrior1Text'); + })).to.exist; + + expect(_.find(list, item => { + return item.text() === i18n.t('armorWarrior2Text'); + })).to.not.exist; + }); + + it('contains mystery items the user can own', () => { + expect(_.find(list, item => { + return item.text() === i18n.t('armorMystery201402Text'); + })).to.exist; + + expect(_.find(list, item => { + return item.text() === i18n.t('armorMystery201403Text'); + })).to.not.exist; + }); + + it('contains special items the user can own', () => { + expect(_.find(list, item => { + return item.text() === i18n.t('armorSpecial1Text'); + })).to.exist; + + expect(_.find(list, item => { + return item.text() === i18n.t('headSpecial1Text'); + })).to.not.exist; + }); + + it('contains armoire items the user can own', () => { + expect(_.find(list, item => { + return item.text() === i18n.t('armorArmoireLunarArmorText'); + })).to.exist; + + expect(_.find(list, item => { + return item.text() === i18n.t('armorArmoireGladiatorArmorText'); + })).to.not.exist; + }); + }); +}); diff --git a/website/src/controllers/api-v3/user.js b/website/src/controllers/api-v3/user.js index fdba009372..d7fff0bd6f 100644 --- a/website/src/controllers/api-v3/user.js +++ b/website/src/controllers/api-v3/user.js @@ -46,6 +46,32 @@ api.getUser = { }, }; +/** + * @api {get} /user/inventory/buy Get the gear items available for purchase for the current user + * @apiVersion 3.0.0 + * @apiName UserGetBuyList + * @apiGroup User + * + * @apiSuccess {Object} list The buy list + */ +api.getBuyList = { + method: 'GET', + middlewares: [authWithHeaders(), cron], + url: '/user/inventory/buy', + async handler (req, res) { + let list = _.cloneDeep(common.updateStore(res.locals.user)); + + // return text and notes strings + _.each(list, item => { + _.each(item, (itemPropVal, itemPropKey) => { + if (_.isFunction(itemPropVal) && itemPropVal.i18nLangFunc) item[itemPropKey] = itemPropVal(req.language); + }); + }); + + res.respond(200, list); + }, +}; + /** * @api {delete} /user DELETE an authenticated user's profile * @apiVersion 3.0.0