mirror of
https://github.com/HabitRPG/habitica.git
synced 2025-12-19 15:48:04 +01:00
* removing duplicate translation key * fixing typos * extracting quest prerequisite check. adding check for previous quest completion, if required * fixing (undoing) static change, adding tests * more typos * correcting test failures * honoring quest prerequisites in quest invite API call. updating format of il8n string replacement arg * no longer using apiError, use translate method instead (msg key was not defined) * adding @apiError to docblock as requested in issue * removing checks on quest invite method. small window of opportunity/low risk
89 lines
2.5 KiB
JavaScript
89 lines
2.5 KiB
JavaScript
import get from 'lodash/get';
|
|
import {
|
|
BadRequest,
|
|
} from '../../libs/errors';
|
|
import {BuyArmoireOperation} from './buyArmoire';
|
|
import {BuyHealthPotionOperation} from './buyHealthPotion';
|
|
import {BuyMarketGearOperation} from './buyMarketGear';
|
|
import buyMysterySet from './buyMysterySet';
|
|
import {BuyQuestWithGoldOperation} from './buyQuest';
|
|
import {BuySpellOperation} from './buySpell';
|
|
import purchaseOp from './purchase';
|
|
import hourglassPurchase from './hourglassPurchase';
|
|
import errorMessage from '../../libs/errorMessage';
|
|
import {BuyGemOperation} from './buyGem';
|
|
|
|
// @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(errorMessage('missingKeyParam'));
|
|
|
|
// @TODO: Slowly remove the need for key and use type instead
|
|
// This should eventually 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': {
|
|
const buyOp = new BuyArmoireOperation(user, req, analytics);
|
|
|
|
buyRes = buyOp.purchase();
|
|
break;
|
|
}
|
|
case 'mystery':
|
|
buyRes = buyMysterySet(user, req, analytics);
|
|
break;
|
|
case 'potion': {
|
|
const buyOp = new BuyHealthPotionOperation(user, req, analytics);
|
|
|
|
buyRes = buyOp.purchase();
|
|
break;
|
|
}
|
|
case 'gems': {
|
|
const buyOp = new BuyGemOperation(user, req, analytics);
|
|
|
|
buyRes = buyOp.purchase();
|
|
break;
|
|
}
|
|
case 'eggs':
|
|
case 'hatchingPotions':
|
|
case 'food':
|
|
case 'quests':
|
|
case 'gear':
|
|
case 'bundles':
|
|
buyRes = purchaseOp(user, req, analytics);
|
|
break;
|
|
case 'pets':
|
|
case 'mounts':
|
|
buyRes = hourglassPurchase(user, req, analytics);
|
|
break;
|
|
case 'quest': {
|
|
const buyOp = new BuyQuestWithGoldOperation(user, req, analytics);
|
|
|
|
buyRes = buyOp.purchase();
|
|
break;
|
|
}
|
|
case 'special': {
|
|
const buyOp = new BuySpellOperation(user, req, analytics);
|
|
|
|
buyRes = buyOp.purchase();
|
|
break;
|
|
}
|
|
default: {
|
|
const buyOp = new BuyMarketGearOperation(user, req, analytics);
|
|
|
|
buyRes = buyOp.purchase();
|
|
break;
|
|
}
|
|
}
|
|
|
|
return buyRes;
|
|
};
|