Files
habitica/website/common/script/ops/buy/buyQuest.js
T
negue 3d39718048 Purchase API Refactoring: Spells [Gold] (#10305)
* convert buySpell operation

* remove purchaseWithSpell - change purchaseType 'special' to 'spells' - fix lint

* fix tests

* rollback 'spells' to 'special'
2018-05-18 17:00:39 +02:00

69 lines
1.8 KiB
JavaScript

import {
BadRequest,
NotAuthorized,
NotFound,
} from '../../libs/errors';
import content from '../../content/index';
import get from 'lodash/get';
import {AbstractGoldItemOperation} from './abstractBuyOperation';
import errorMessage from '../../libs/errorMessage';
export class BuyQuestWithGoldOperation extends AbstractGoldItemOperation {
constructor (user, req, analytics) {
super(user, req, analytics);
}
multiplePurchaseAllowed () {
return true;
}
userAbleToStartMasterClasser (user) {
return user.achievements.quests.dilatoryDistress3 &&
user.achievements.quests.mayhemMistiflying3 &&
user.achievements.quests.stoikalmCalamity3 &&
user.achievements.quests.taskwoodsTerror3;
}
getItemKey () {
return this.key;
}
getItemValue (item) {
return item.goldValue;
}
extractAndValidateParams (user, req) {
let key = this.key = get(req, 'params.key');
if (!key) throw new BadRequest(errorMessage('missingKeyParam'));
if (key === 'lostMasterclasser1' && !this.userAbleToStartMasterClasser(user)) {
throw new NotAuthorized(this.i18n('questUnlockLostMasterclasser'));
}
let item = content.quests[key];
if (!item) throw new NotFound(errorMessage('questNotFound', {key}));
if (!(item.category === 'gold' && item.goldValue)) {
throw new NotAuthorized(this.i18n('questNotGoldPurchasable', {key}));
}
this.canUserPurchase(user, item);
}
executeChanges (user, item, req) {
user.items.quests[item.key] = user.items.quests[item.key] || 0;
user.items.quests[item.key] += this.quantity;
this.subtractCurrency(user, item, this.quantity);
return [
user.items.quests,
this.i18n('messageBought', {
itemText: item.text(req.language),
}),
];
}
}