mirror of
https://github.com/HabitRPG/habitica.git
synced 2025-12-18 15:17:25 +01:00
Ported purchase, add unit tests, created new user purchase route, and added tests
This commit is contained in:
@@ -3,105 +3,126 @@ import i18n from '../i18n';
|
||||
import _ from 'lodash';
|
||||
import splitWhitespace from '../libs/splitWhitespace';
|
||||
import planGemLimits from '../libs/planGemLimits';
|
||||
import {
|
||||
NotFound,
|
||||
NotAuthorized,
|
||||
BadRequest,
|
||||
} from '../libs/errors';
|
||||
|
||||
module.exports = function purchase (user, req = {}, analytics) {
|
||||
let type = _.get(req.params, 'type');
|
||||
let key = _.get(req.params, 'key');
|
||||
let item;
|
||||
let price;
|
||||
|
||||
if (!type) {
|
||||
throw new BadRequest(i18n.t('typeRequired', req.language));
|
||||
}
|
||||
|
||||
if (!key) {
|
||||
throw new BadRequest(i18n.t('keyRequired', req.language));
|
||||
}
|
||||
|
||||
module.exports = function(user, req, cb, analytics) {
|
||||
var analyticsData, convCap, convRate, item, key, price, ref, ref1, ref2, ref3, type;
|
||||
ref = req.params, type = ref.type, key = ref.key;
|
||||
if (type === 'gems' && key === 'gem') {
|
||||
ref1 = planGemLimits, convRate = ref1.convRate, convCap = ref1.convCap;
|
||||
let convRate = planGemLimits.convRate;
|
||||
let convCap = planGemLimits.convCap;
|
||||
convCap += user.purchased.plan.consecutive.gemCapExtra;
|
||||
if (!((ref2 = user.purchased) != null ? (ref3 = ref2.plan) != null ? ref3.customerId : void 0 : void 0)) {
|
||||
return typeof cb === "function" ? cb({
|
||||
code: 401,
|
||||
message: "Must subscribe to purchase gems with GP"
|
||||
}, req) : void 0;
|
||||
|
||||
if (!user.purchased || !user.purchased.plan || !user.purchased.plan.customerId) {
|
||||
throw new NotAuthorized(i18n.t('mustSubscribeToPurchaseGems', req.language));
|
||||
}
|
||||
if (!(user.stats.gp >= convRate)) {
|
||||
return typeof cb === "function" ? cb({
|
||||
code: 401,
|
||||
message: "Not enough Gold"
|
||||
}) : void 0;
|
||||
|
||||
if (user.stats.gp < convRate) {
|
||||
throw new NotAuthorized(i18n.t('messageNotEnoughGold', req.language));
|
||||
}
|
||||
|
||||
if (user.purchased.plan.gemsBought >= convCap) {
|
||||
return typeof cb === "function" ? cb({
|
||||
code: 401,
|
||||
message: "You've reached the Gold=>Gem conversion cap (" + convCap + ") for this month. We have this to prevent abuse / farming. The cap will reset within the first three days of next month."
|
||||
}) : void 0;
|
||||
throw new NotAuthorized(i18n.t('reachedGoldToGemCap', {convCap}, req.language));
|
||||
}
|
||||
user.balance += .25;
|
||||
|
||||
user.balance += 0.25;
|
||||
user.purchased.plan.gemsBought++;
|
||||
user.stats.gp -= convRate;
|
||||
analyticsData = {
|
||||
uuid: user._id,
|
||||
itemKey: key,
|
||||
acquireMethod: 'Gold',
|
||||
goldCost: convRate,
|
||||
category: 'behavior'
|
||||
};
|
||||
if (analytics != null) {
|
||||
analytics.track('purchase gems', analyticsData);
|
||||
|
||||
if (analytics) {
|
||||
analytics.track('purchase gems', {
|
||||
uuid: user._id,
|
||||
itemKey: key,
|
||||
acquireMethod: 'Gold',
|
||||
goldCost: convRate,
|
||||
category: 'behavior',
|
||||
});
|
||||
}
|
||||
return typeof cb === "function" ? cb({
|
||||
code: 200,
|
||||
message: "+1 Gem"
|
||||
}, _.pick(user, splitWhitespace('stats balance'))) : void 0;
|
||||
|
||||
let response = {
|
||||
data: _.pick(user, splitWhitespace('stats balance')),
|
||||
message: i18n.t('plusOneGem'),
|
||||
};
|
||||
|
||||
return response;
|
||||
}
|
||||
if (type !== 'eggs' && type !== 'hatchingPotions' && type !== 'food' && type !== 'quests' && type !== 'gear') {
|
||||
return typeof cb === "function" ? cb({
|
||||
code: 404,
|
||||
message: ":type must be in [eggs,hatchingPotions,food,quests,gear]"
|
||||
}, req) : void 0;
|
||||
|
||||
let acceptedTypes = ['eggs', 'hatchingPotions', 'food', 'quests', 'gear'];
|
||||
if (acceptedTypes.indexOf(type) === -1) {
|
||||
throw new NotFound(i18n.t('notAccteptedType', req.language));
|
||||
}
|
||||
|
||||
if (type === 'gear') {
|
||||
item = content.gear.flat[key];
|
||||
if (user.items.gear.owned[key]) {
|
||||
return typeof cb === "function" ? cb({
|
||||
code: 401,
|
||||
message: i18n.t('alreadyHave', req.language)
|
||||
}) : void 0;
|
||||
|
||||
if (!item) {
|
||||
throw new NotFound(i18n.t('contentKeyNotFound', {type}, req.language));
|
||||
}
|
||||
|
||||
if (user.items.gear.owned[key]) {
|
||||
throw new NotAuthorized(i18n.t('alreadyHave', req.language));
|
||||
}
|
||||
|
||||
price = (item.twoHanded || item.gearSet === 'animal' ? 2 : 1) / 4;
|
||||
} else {
|
||||
item = content[type][key];
|
||||
|
||||
if (!item) {
|
||||
throw new NotFound(i18n.t('contentKeyNotFound', {type}, req.language));
|
||||
}
|
||||
|
||||
price = item.value / 4;
|
||||
}
|
||||
if (!item) {
|
||||
return typeof cb === "function" ? cb({
|
||||
code: 404,
|
||||
message: ":key not found for Content." + type
|
||||
}, req) : void 0;
|
||||
}
|
||||
|
||||
if (!item.canBuy(user)) {
|
||||
return typeof cb === "function" ? cb({
|
||||
code: 403,
|
||||
message: i18n.t('messageNotAvailable', req.language)
|
||||
}) : void 0;
|
||||
throw new NotAuthorized(i18n.t('messageNotAvailable', req.language));
|
||||
}
|
||||
if ((user.balance < price) || !user.balance) {
|
||||
return typeof cb === "function" ? cb({
|
||||
code: 403,
|
||||
message: i18n.t('notEnoughGems', req.language)
|
||||
}) : void 0;
|
||||
|
||||
if (!user.balance || user.balance < price) {
|
||||
throw new NotAuthorized(i18n.t('notEnoughGems', req.language));
|
||||
}
|
||||
|
||||
user.balance -= price;
|
||||
|
||||
if (type === 'gear') {
|
||||
user.items.gear.owned[key] = true;
|
||||
} else {
|
||||
if (!(user.items[type][key] > 0)) {
|
||||
if (!user.items[type][key] || user.items[type][key] < 0) {
|
||||
user.items[type][key] = 0;
|
||||
}
|
||||
user.items[type][key]++;
|
||||
}
|
||||
analyticsData = {
|
||||
uuid: user._id,
|
||||
itemKey: key,
|
||||
itemType: 'Market',
|
||||
acquireMethod: 'Gems',
|
||||
gemCost: item.value,
|
||||
category: 'behavior'
|
||||
};
|
||||
if (analytics != null) {
|
||||
analytics.track('acquire item', analyticsData);
|
||||
|
||||
if (analytics) {
|
||||
analytics.track('acquire item', {
|
||||
uuid: user._id,
|
||||
itemKey: key,
|
||||
itemType: 'Market',
|
||||
acquireMethod: 'Gems',
|
||||
gemCost: item.value,
|
||||
category: 'behavior',
|
||||
});
|
||||
}
|
||||
return typeof cb === "function" ? cb(null, _.pick(user, splitWhitespace('items balance'))) : void 0;
|
||||
|
||||
let response = {
|
||||
data: _.pick(user, splitWhitespace('items balance')),
|
||||
message: i18n.t('purchased', {type, key}),
|
||||
};
|
||||
|
||||
return response;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user