Files
habitica/website/common/script/ops/buy/buyQuest.js
Brian Fenton ac90a40be5 Api quest restrictions - no purchase/start without fulfilling eligibility requirements (#10387)
* 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
2018-05-27 16:41:56 +02:00

78 lines
2.1 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'));
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.checkPrerequisites(user, key);
this.canUserPurchase(user, item);
}
checkPrerequisites (user, questKey) {
const item = content.quests[questKey];
if (questKey === 'lostMasterclasser1' && !this.userAbleToStartMasterClasser(user)) {
throw new NotAuthorized(this.i18n('questUnlockLostMasterclasser'));
}
if (item && item.previous && !user.achievements.quests[item.previous]) {
throw new NotAuthorized(this.i18n('mustComplete', {quest: item.previous}));
}
}
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),
}),
];
}
}