mirror of
https://github.com/HabitRPG/habitica.git
synced 2025-12-17 22:57:21 +01:00
* 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>
78 lines
2.2 KiB
JavaScript
78 lines
2.2 KiB
JavaScript
import pick from 'lodash/pick';
|
|
import get from 'lodash/get';
|
|
import splitWhitespace from '../../libs/splitWhitespace';
|
|
import {
|
|
BadRequest,
|
|
NotAuthorized,
|
|
} from '../../libs/errors';
|
|
import { AbstractGoldItemOperation } from './abstractBuyOperation';
|
|
import planGemLimits from '../../libs/planGemLimits';
|
|
import updateUserBalance from '../updateUserBalance';
|
|
|
|
export class BuyGemOperation extends AbstractGoldItemOperation { // eslint-disable-line import/prefer-default-export, max-len
|
|
multiplePurchaseAllowed () { // eslint-disable-line class-methods-use-this
|
|
return true;
|
|
}
|
|
|
|
getItemValue () { // eslint-disable-line class-methods-use-this
|
|
return planGemLimits.convRate;
|
|
}
|
|
|
|
getItemKey () { // eslint-disable-line class-methods-use-this
|
|
return 'gem';
|
|
}
|
|
|
|
getItemType () { // eslint-disable-line class-methods-use-this
|
|
return 'gems';
|
|
}
|
|
|
|
extractAndValidateParams (user, req) {
|
|
this.key = get(req, 'params.key');
|
|
const { key } = this;
|
|
if (!key) throw new BadRequest(this.i18n('missingKeyParam'));
|
|
|
|
let { convCap } = planGemLimits;
|
|
convCap += user.purchased.plan.consecutive.gemCapExtra;
|
|
|
|
// todo better name?
|
|
this.convCap = convCap;
|
|
|
|
this.canUserPurchase(user);
|
|
}
|
|
|
|
canUserPurchase (user, item) {
|
|
if (!user.purchased || !user.purchased.plan || !user.purchased.plan.customerId) {
|
|
throw new NotAuthorized(this.i18n('mustSubscribeToPurchaseGems'));
|
|
}
|
|
|
|
super.canUserPurchase(user, item);
|
|
|
|
if (user.purchased.plan.gemsBought >= this.convCap) {
|
|
throw new NotAuthorized(this.i18n('maxBuyGems', { convCap: this.convCap }));
|
|
}
|
|
|
|
if (user.purchased.plan.gemsBought + this.quantity > this.convCap) {
|
|
throw new NotAuthorized(this.i18n('reachedGoldToGemCapQuantity', {
|
|
convCap: this.convCap,
|
|
quantity: this.quantity,
|
|
}));
|
|
}
|
|
}
|
|
|
|
executeChanges (user, item) {
|
|
updateUserBalance(user, 0.25 * this.quantity, 'buy_gold');
|
|
user.purchased.plan.gemsBought += this.quantity;
|
|
|
|
this.subtractCurrency(user, item);
|
|
|
|
return [
|
|
pick(user, splitWhitespace('stats balance')),
|
|
this.i18n('plusGem', { count: this.quantity }),
|
|
];
|
|
}
|
|
|
|
analyticsLabel () { // eslint-disable-line class-methods-use-this
|
|
return 'purchase gems';
|
|
}
|
|
}
|