Files
habitica/website/common/script/ops/buy/buy.js
T
negue 54153ec299 Refactor Purchase API - Part 1 (#9714)
* move to shops/purchase

* move files to /buy/ instead of /purchase/

* refactor buy.js - add more itemtypes

* revert moving special purchases to buy

* only use buyOp from api-routes

* fix buying potion client-side

* undo import buy instead of purchase

* enable potion bulk purchase - use buyGear as fallback (as before)

* move quantity purchase inside buyHealthPotion

* move quantity purchase inside buyQuest

* move quantity purchase inside buySpecialSpell + add analytics

* remove unused quantity variable - set req.type on specialKeys

* fix `buyKnownKeys` on buy api

* test buy-special-spell if not enough gold

* more buy ops coverage

* fix lint

* buyMysterySet: test for window.confirm, buyQuest: check for Masterclassers unlock

* fix test & lint

* re-create package-lock.json to travis build ?

* use global.window instead of method argument

* add back canOwn checks

* remove buyMysterySet confirm request
2018-02-10 11:14:40 +01:00

68 lines
1.9 KiB
JavaScript

import i18n from '../../i18n';
import get from 'lodash/get';
import {
BadRequest,
} from '../../libs/errors';
import buyHealthPotion from './buyHealthPotion';
import buyArmoire from './buyArmoire';
import buyGear from './buyGear';
import buyMysterySet from './buyMysterySet';
import buyQuest from './buyQuest';
import buySpecialSpell from './buySpecialSpell';
import purchaseOp from './purchase';
import hourglassPurchase from './hourglassPurchase';
// @TODO: remove the req option style. Dependency on express structure is an anti-pattern
// We should either have more parms or a set structure validated by a Type checker
// @TODO: when we are sure buy is the only function used, let's move the buy files to a folder
module.exports = function buy (user, req = {}, analytics) {
let key = get(req, 'params.key');
if (!key) throw new BadRequest(i18n.t('missingKeyParam', req.language));
// @TODO: Slowly remove the need for key and use type instead
// This should evenutally be the 'factory' function with vendor classes
let type = get(req, 'type');
if (!type) type = get(req, 'params.type');
if (!type) type = key;
let buyRes;
switch (type) {
case 'armoire':
buyRes = buyArmoire(user, req, analytics);
break;
case 'mystery':
buyRes = buyMysterySet(user, req, analytics);
break;
case 'potion':
buyRes = buyHealthPotion(user, req, analytics);
break;
case 'eggs':
case 'hatchingPotions':
case 'food':
case 'quests':
case 'gear':
case 'bundles':
case 'gems':
buyRes = purchaseOp(user, req, analytics);
break;
case 'pets':
case 'mounts':
buyRes = hourglassPurchase(user, req, analytics);
break;
case 'quest':
buyRes = buyQuest(user, req, analytics);
break;
case 'special':
buyRes = buySpecialSpell(user, req, analytics);
break;
default:
buyRes = buyGear(user, req, analytics);
break;
}
return buyRes;
};