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';
|
|
|
|
|
|
|
|
|
|
export class BuyGemOperation extends AbstractGoldItemOperation {
|
|
|
|
|
constructor (user, req, analytics) {
|
|
|
|
|
super(user, req, analytics);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
multiplePurchaseAllowed () {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
getItemValue () {
|
|
|
|
|
return planGemLimits.convRate;
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-20 19:13:22 +00:00
|
|
|
getItemKey () {
|
2018-04-27 17:29:26 +00:00
|
|
|
return 'gem';
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-20 19:13:22 +00:00
|
|
|
getItemType () {
|
|
|
|
|
return 'gems';
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-27 17:29:26 +00:00
|
|
|
extractAndValidateParams (user, req) {
|
2019-10-08 14:57:10 +00:00
|
|
|
const key = this.key = get(req, 'params.key');
|
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) {
|
2019-10-08 14:57:10 +00:00
|
|
|
throw new NotAuthorized(this.i18n('reachedGoldToGemCap', { 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) {
|
|
|
|
|
user.balance += 0.25 * this.quantity;
|
|
|
|
|
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
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
analyticsLabel () {
|
|
|
|
|
return 'purchase gems';
|
|
|
|
|
}
|
|
|
|
|
}
|