Files
habitica/website/common/script/ops/buy/buyQuestGem.js
Phillip Thelen 6e43d4dc79 Add Transaction log for gem and hourglass changes (#13589)
* Log all gem transactions to database

* Also store hourglass transactions

* Fix tests

* Display transaction history in hall of heroes for admins

* add tests to new API call

* hide transaction settings tab for non admins

* fix(lint): remove console

* fix(lint): various automatic corrections

* fix(transactions): use enum expected pluralizations

* fix api unit tests

* fix lint

* fix failing test

* Fix minor inconsistencies

* Log all gem transactions to database

* Also store hourglass transactions

* Fix tests

* Display transaction history in hall of heroes for admins

* add tests to new API call

* hide transaction settings tab for non admins

* fix(lint): remove console

* fix(lint): various automatic corrections

* fix(transactions): use enum expected pluralizations

* fix api unit tests

* fix lint

* Fix minor inconsistencies

Co-authored-by: Sabe Jones <sabrecat@gmail.com>
2022-01-31 15:36:15 -06:00

66 lines
1.7 KiB
JavaScript

import get from 'lodash/get';
import {
BadRequest,
NotAuthorized,
NotFound,
} from '../../libs/errors';
import content from '../../content/index';
import errorMessage from '../../libs/errorMessage';
import { AbstractGemItemOperation } from './abstractBuyOperation';
export class BuyQuestWithGemOperation extends AbstractGemItemOperation { // eslint-disable-line import/prefer-default-export, max-len
multiplePurchaseAllowed () { // eslint-disable-line class-methods-use-this
return true;
}
getItemKey () {
return this.key;
}
getItemValue (item) { // eslint-disable-line class-methods-use-this
return item.value / 4;
}
getItemType () { // eslint-disable-line class-methods-use-this
return 'quest';
}
extractAndValidateParams (user, req) {
this.key = get(req, 'params.key');
const { key } = this;
if (!key) throw new BadRequest(errorMessage('missingKeyParam'));
const 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);
}
async executeChanges (user, item, req) {
if (
!user.items.quests[item.key]
|| user.items.quests[item.key] < 0
) user.items.quests[item.key] = 0;
user.items.quests = {
...user.items.quests,
[item.key]: user.items.quests[item.key] + this.quantity,
};
if (user.markModified) user.markModified('items.quests');
await this.subtractCurrency(user, item, this.quantity);
return [
user.items.quests,
this.i18n('messageBought', {
itemText: item.text(req.language),
}),
];
}
}