2018-04-27 17:29:26 +00:00
|
|
|
import pick from 'lodash/pick';
|
2019-10-08 14:57:10 +00:00
|
|
|
import get from 'lodash/get';
|
2018-04-27 17:29:26 +00:00
|
|
|
import splitWhitespace from '../../libs/splitWhitespace';
|
|
|
|
|
import {
|
|
|
|
|
BadRequest,
|
|
|
|
|
NotAuthorized,
|
|
|
|
|
} from '../../libs/errors';
|
2019-10-08 14:57:10 +00:00
|
|
|
import { AbstractGoldItemOperation } from './abstractBuyOperation';
|
2018-04-27 17:29:26 +00:00
|
|
|
import planGemLimits from '../../libs/planGemLimits';
|
2022-01-31 21:36:15 +00:00
|
|
|
import updateUserBalance from '../updateUserBalance';
|
2018-04-27 17:29:26 +00:00
|
|
|
|
2019-10-09 14:51:17 +00:00
|
|
|
export class BuyGemOperation extends AbstractGoldItemOperation { // eslint-disable-line import/prefer-default-export, max-len
|
2019-10-09 18:08:36 +00:00
|
|
|
multiplePurchaseAllowed () { // eslint-disable-line class-methods-use-this
|
2018-04-27 17:29:26 +00:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-09 18:08:36 +00:00
|
|
|
getItemValue () { // eslint-disable-line class-methods-use-this
|
2018-04-27 17:29:26 +00:00
|
|
|
return planGemLimits.convRate;
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-09 18:08:36 +00:00
|
|
|
getItemKey () { // eslint-disable-line class-methods-use-this
|
2018-04-27 17:29:26 +00:00
|
|
|
return 'gem';
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-09 18:08:36 +00:00
|
|
|
getItemType () { // eslint-disable-line class-methods-use-this
|
2018-08-20 19:13:22 +00:00
|
|
|
return 'gems';
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-27 17:29:26 +00:00
|
|
|
extractAndValidateParams (user, req) {
|
2019-10-09 14:51:17 +00:00
|
|
|
this.key = get(req, 'params.key');
|
2019-10-10 20:15:58 +00:00
|
|
|
const { key } = this;
|
2018-04-27 17:29:26 +00:00
|
|
|
if (!key) throw new BadRequest(this.i18n('missingKeyParam'));
|
|
|
|
|
|
2019-10-08 14:57:10 +00:00
|
|
|
let { convCap } = planGemLimits;
|
2018-04-27 17:29:26 +00:00
|
|
|
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) {
|
2020-05-02 19:49:38 +00:00
|
|
|
throw new NotAuthorized(this.i18n('maxBuyGems', { convCap: this.convCap }));
|
2018-04-27 17:29:26 +00:00
|
|
|
}
|
|
|
|
|
|
2018-05-07 13:20:28 +00:00
|
|
|
if (user.purchased.plan.gemsBought + this.quantity > this.convCap) {
|
2018-04-27 17:29:26 +00:00
|
|
|
throw new NotAuthorized(this.i18n('reachedGoldToGemCapQuantity', {
|
|
|
|
|
convCap: this.convCap,
|
|
|
|
|
quantity: this.quantity,
|
|
|
|
|
}));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
executeChanges (user, item) {
|
2022-01-31 21:36:15 +00:00
|
|
|
updateUserBalance(user, 0.25 * this.quantity, 'buy_gold');
|
2018-04-27 17:29:26 +00:00
|
|
|
user.purchased.plan.gemsBought += this.quantity;
|
|
|
|
|
|
|
|
|
|
this.subtractCurrency(user, item);
|
|
|
|
|
|
|
|
|
|
return [
|
|
|
|
|
pick(user, splitWhitespace('stats balance')),
|
2019-10-08 14:57:10 +00:00
|
|
|
this.i18n('plusGem', { count: this.quantity }),
|
2018-04-27 17:29:26 +00:00
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-09 18:08:36 +00:00
|
|
|
analyticsLabel () { // eslint-disable-line class-methods-use-this
|
2018-04-27 17:29:26 +00:00
|
|
|
return 'purchase gems';
|
|
|
|
|
}
|
|
|
|
|
}
|