Files
habitica/website/common/script/ops/buy/buyQuestGem.js
Matteo Pagliazzi 0b8ce63c76 WIP: Improve User model performances (#10832)
* wip: define items as mixed objects

* add default owned gear

* mark modified

* more mark modified

* more mark modified

* more mark modified

* more mark modified

* fix common tests

* fix common tests

* update mongoose

* add itemsUtils

* use new util function in hall controller

* add tests for items utils

* update website/server to mark all items as modified

* start updating common code

* update login incentives

* update unlock

* remove changes to package-lock.json

* remove changes to package.json
2019-04-01 19:24:18 +02:00

63 lines
1.4 KiB
JavaScript

import {
BadRequest,
NotAuthorized,
NotFound,
} from '../../libs/errors';
import content from '../../content/index';
import get from 'lodash/get';
import errorMessage from '../../libs/errorMessage';
import {AbstractGemItemOperation} from './abstractBuyOperation';
export class BuyQuestWithGemOperation extends AbstractGemItemOperation {
constructor (user, req, analytics) {
super(user, req, analytics);
}
multiplePurchaseAllowed () {
return true;
}
getItemKey () {
return this.key;
}
getItemValue (item) {
return item.value / 4;
}
getItemType () {
return 'quest';
}
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') {
throw new NotAuthorized(this.i18n('questNotGemPurchasable', {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;
if (user.markModified) user.markModified('items.quests');
this.subtractCurrency(user, item, this.quantity);
return [
user.items.quests,
this.i18n('messageBought', {
itemText: item.text(req.language),
}),
];
}
}