mirror of
https://github.com/HabitRPG/habitica.git
synced 2025-12-17 14:47:53 +01:00
Ported purchase, add unit tests, created new user purchase route, and added tests
This commit is contained in:
@@ -128,5 +128,13 @@
|
||||
"privateMessageGiftSubscriptionMessage": "<%= numberOfMonths %> months of subscription! ",
|
||||
"cannotSendGemsToYourself": "Cannot send gems to yourself. Try a subscription instead.",
|
||||
"notEnoughGemsToSend": "Amount must be within 0 and your current number of gems.",
|
||||
"mustPurchaseToSet": "Must purchase <%= val %> to set it on <%= key %>."
|
||||
"mustPurchaseToSet": "Must purchase <%= val %> to set it on <%= key %>.",
|
||||
"typeRequired": "Type is required",
|
||||
"keyRequired": "Key is required",
|
||||
"mustSubscribeToPurchaseGems": "Must subscribe to purchase gems with GP",
|
||||
"reachedGoldToGemCap": "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.",
|
||||
"notAccteptedType": "Type must be in [eggs, hatchingPotions, food, quests, gear]",
|
||||
"contentKeyNotFound": "Key not found for Content <%= type %>",
|
||||
"plusOneGem": "+1 Gem",
|
||||
"purchased": "You purchsed a <%= key %> <%= type %>"
|
||||
}
|
||||
|
||||
@@ -114,6 +114,7 @@ import feed from './ops/feed';
|
||||
import equip from './ops/equip';
|
||||
import changeClass from './ops/changeClass';
|
||||
import disableClasses from './ops/disableClasses';
|
||||
import purchase from './ops/purchase';
|
||||
|
||||
api.ops = {
|
||||
scoreTask,
|
||||
@@ -129,6 +130,7 @@ api.ops = {
|
||||
equip,
|
||||
changeClass,
|
||||
disableClasses,
|
||||
purchase,
|
||||
};
|
||||
|
||||
import handleTwoHanded from './fns/handleTwoHanded';
|
||||
|
||||
@@ -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 = {
|
||||
|
||||
if (analytics) {
|
||||
analytics.track('purchase gems', {
|
||||
uuid: user._id,
|
||||
itemKey: key,
|
||||
acquireMethod: 'Gold',
|
||||
goldCost: convRate,
|
||||
category: 'behavior'
|
||||
category: 'behavior',
|
||||
});
|
||||
}
|
||||
|
||||
let response = {
|
||||
data: _.pick(user, splitWhitespace('stats balance')),
|
||||
message: i18n.t('plusOneGem'),
|
||||
};
|
||||
if (analytics != null) {
|
||||
analytics.track('purchase gems', analyticsData);
|
||||
|
||||
return response;
|
||||
}
|
||||
return typeof cb === "function" ? cb({
|
||||
code: 200,
|
||||
message: "+1 Gem"
|
||||
}, _.pick(user, splitWhitespace('stats balance'))) : void 0;
|
||||
}
|
||||
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 = {
|
||||
|
||||
if (analytics) {
|
||||
analytics.track('acquire item', {
|
||||
uuid: user._id,
|
||||
itemKey: key,
|
||||
itemType: 'Market',
|
||||
acquireMethod: 'Gems',
|
||||
gemCost: item.value,
|
||||
category: 'behavior'
|
||||
};
|
||||
if (analytics != null) {
|
||||
analytics.track('acquire item', analyticsData);
|
||||
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;
|
||||
};
|
||||
|
||||
@@ -30,7 +30,6 @@ const COMMON_FILES = [
|
||||
'!./common/script/ops/getTags.js',
|
||||
'!./common/script/ops/hourglassPurchase.js',
|
||||
'!./common/script/ops/openMysteryItem.js',
|
||||
'!./common/script/ops/purchase.js',
|
||||
'!./common/script/ops/readCard.js',
|
||||
'!./common/script/ops/rebirth.js',
|
||||
'!./common/script/ops/releaseBoth.js',
|
||||
|
||||
35
test/api/v3/integration/user/POST-user_purchase.test.js
Normal file
35
test/api/v3/integration/user/POST-user_purchase.test.js
Normal file
@@ -0,0 +1,35 @@
|
||||
import {
|
||||
generateUser,
|
||||
translate as t,
|
||||
} from '../../../../helpers/api-integration/v3';
|
||||
|
||||
describe('POST /user/purchase/:type/:key', () => {
|
||||
let user;
|
||||
let type = 'hatchingPotions';
|
||||
let key = 'Base';
|
||||
|
||||
beforeEach(async () => {
|
||||
user = await generateUser({
|
||||
balance: 40,
|
||||
});
|
||||
});
|
||||
|
||||
// More tests in common code unit tests
|
||||
|
||||
it('returns an error when key is not provided', async () => {
|
||||
await expect(user.post(`/user/purchase/gems/gem`))
|
||||
.to.eventually.be.rejected.and.eql({
|
||||
code: 401,
|
||||
error: 'NotAuthorized',
|
||||
message: t('mustSubscribeToPurchaseGems'),
|
||||
});
|
||||
});
|
||||
|
||||
it('purchases a gem item', async () => {
|
||||
let res = await user.post(`/user/purchase/${type}/${key}`);
|
||||
await user.sync();
|
||||
|
||||
expect(res.message).to.equal(t('purchased', {type, key}));
|
||||
expect(user.items[type][key]).to.equal(1);
|
||||
});
|
||||
});
|
||||
199
test/common/ops/purchase.js
Normal file
199
test/common/ops/purchase.js
Normal file
@@ -0,0 +1,199 @@
|
||||
import purchase from '../../../common/script/ops/purchase';
|
||||
import planGemLimits from '../../../common/script/libs/planGemLimits';
|
||||
import {
|
||||
BadRequest,
|
||||
NotAuthorized,
|
||||
NotFound,
|
||||
} from '../../../common/script/libs/errors';
|
||||
import i18n from '../../../common/script/i18n';
|
||||
import {
|
||||
generateUser,
|
||||
} from '../../helpers/common.helper';
|
||||
|
||||
describe('shared.ops.feed', () => {
|
||||
let user;
|
||||
let goldPoints = 40;
|
||||
let gemsBought = 40;
|
||||
|
||||
before(() => {
|
||||
user = generateUser({'stats.class': 'rogue'});
|
||||
});
|
||||
|
||||
context('failure conditions', () => {
|
||||
it('returns an error when type is not provided', (done) => {
|
||||
try {
|
||||
purchase(user, {params: {}});
|
||||
} catch (err) {
|
||||
expect(err).to.be.an.instanceof(BadRequest);
|
||||
expect(err.message).to.equal(i18n.t('typeRequired'));
|
||||
done();
|
||||
}
|
||||
});
|
||||
|
||||
it('returns an error when key is not provided', (done) => {
|
||||
try {
|
||||
purchase(user, {params: {type: 'gems'}});
|
||||
} catch (err) {
|
||||
expect(err).to.be.an.instanceof(BadRequest);
|
||||
expect(err.message).to.equal(i18n.t('keyRequired'));
|
||||
done();
|
||||
}
|
||||
});
|
||||
|
||||
it('prevents unsubscribed user from buying gems', (done) => {
|
||||
try {
|
||||
purchase(user, {params: {type: 'gems', key: 'gem'}});
|
||||
} catch (err) {
|
||||
expect(err).to.be.an.instanceof(NotAuthorized);
|
||||
expect(err.message).to.equal(i18n.t('mustSubscribeToPurchaseGems'));
|
||||
done();
|
||||
}
|
||||
});
|
||||
|
||||
it('prevents user with not enough gold from buying gems', (done) => {
|
||||
user.purchased.plan.customerId = 'customer-id';
|
||||
|
||||
try {
|
||||
purchase(user, {params: {type: 'gems', key: 'gem'}});
|
||||
} catch (err) {
|
||||
expect(err).to.be.an.instanceof(NotAuthorized);
|
||||
expect(err.message).to.equal(i18n.t('messageNotEnoughGold'));
|
||||
done();
|
||||
}
|
||||
});
|
||||
|
||||
it('prevents user that have reached the conversion cap from buying gems', (done) => {
|
||||
user.stats.gp = goldPoints;
|
||||
user.purchased.plan.gemsBought = gemsBought;
|
||||
|
||||
try {
|
||||
purchase(user, {params: {type: 'gems', key: 'gem'}});
|
||||
} catch (err) {
|
||||
expect(err).to.be.an.instanceof(NotAuthorized);
|
||||
expect(err.message).to.equal(i18n.t('reachedGoldToGemCap', {convCap: planGemLimits.convCap}));
|
||||
done();
|
||||
}
|
||||
});
|
||||
|
||||
it('returns error when unknown type is provided', (done) => {
|
||||
try {
|
||||
purchase(user, {params: {type: 'randomType', key: 'gem'}});
|
||||
} catch (err) {
|
||||
expect(err).to.be.an.instanceof(NotFound);
|
||||
expect(err.message).to.equal(i18n.t('notAccteptedType'));
|
||||
done();
|
||||
}
|
||||
});
|
||||
|
||||
it('returns error when user attempts to purchase a piece of gear they own', (done) => {
|
||||
user.items.gear.owned['shield_rogue_1'] = true; // eslint-disable-line dot-notation
|
||||
|
||||
try {
|
||||
purchase(user, {params: {type: 'gear', key: 'shield_rogue_1'}});
|
||||
} catch (err) {
|
||||
expect(err).to.be.an.instanceof(NotAuthorized);
|
||||
expect(err.message).to.equal(i18n.t('alreadyHave'));
|
||||
done();
|
||||
}
|
||||
});
|
||||
|
||||
it('returns error when unknown item is requested', (done) => {
|
||||
try {
|
||||
purchase(user, {params: {type: 'gear', key: 'randomKey'}});
|
||||
} catch (err) {
|
||||
expect(err).to.be.an.instanceof(NotFound);
|
||||
expect(err.message).to.equal(i18n.t('contentKeyNotFound', {type: 'gear'}));
|
||||
done();
|
||||
}
|
||||
});
|
||||
|
||||
it('returns error when user does not have permission to buy an item', (done) => {
|
||||
try {
|
||||
purchase(user, {params: {type: 'gear', key: 'eyewear_mystery_301405'}});
|
||||
} catch (err) {
|
||||
expect(err).to.be.an.instanceof(NotAuthorized);
|
||||
expect(err.message).to.equal(i18n.t('messageNotAvailable'));
|
||||
done();
|
||||
}
|
||||
});
|
||||
|
||||
it('returns error when user does not have enough gems to buy an item', (done) => {
|
||||
try {
|
||||
purchase(user, {params: {type: 'gear', key: 'headAccessory_special_wolfEars'}});
|
||||
} catch (err) {
|
||||
expect(err).to.be.an.instanceof(NotAuthorized);
|
||||
expect(err.message).to.equal(i18n.t('notEnoughGems'));
|
||||
done();
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
context('successful feeding', () => {
|
||||
let userGemAmount = 10;
|
||||
|
||||
before(() => {
|
||||
user.balance = userGemAmount;
|
||||
user.stats.gp = goldPoints;
|
||||
user.purchased.plan.gemsBought = 0;
|
||||
});
|
||||
|
||||
it('purchases gems', () => {
|
||||
let purchaseResponse = purchase(user, {params: {type: 'gems', key: 'gem'}});
|
||||
|
||||
expect(purchaseResponse.message).to.equal(i18n.t('plusOneGem'));
|
||||
expect(user.balance).to.equal(userGemAmount + 0.25);
|
||||
expect(user.purchased.plan.gemsBought).to.equal(1);
|
||||
expect(user.stats.gp).to.equal(goldPoints - planGemLimits.convRate);
|
||||
});
|
||||
|
||||
it('purchases eggs', () => {
|
||||
let type = 'eggs';
|
||||
let key = 'Wolf';
|
||||
|
||||
let purchaseResponse = purchase(user, {params: {type, key}});
|
||||
|
||||
expect(purchaseResponse.message).to.equal(i18n.t('purchased', {type, key}));
|
||||
expect(user.items[type][key]).to.equal(1);
|
||||
});
|
||||
|
||||
it('purchases hatchingPotions', () => {
|
||||
let type = 'hatchingPotions';
|
||||
let key = 'Base';
|
||||
|
||||
let purchaseResponse = purchase(user, {params: {type, key}});
|
||||
|
||||
expect(purchaseResponse.message).to.equal(i18n.t('purchased', {type, key}));
|
||||
expect(user.items[type][key]).to.equal(1);
|
||||
});
|
||||
|
||||
it('purchases food', () => {
|
||||
let type = 'food';
|
||||
let key = 'Meat';
|
||||
|
||||
let purchaseResponse = purchase(user, {params: {type, key}});
|
||||
|
||||
expect(purchaseResponse.message).to.equal(i18n.t('purchased', {type, key}));
|
||||
expect(user.items[type][key]).to.equal(1);
|
||||
});
|
||||
|
||||
it('purchases quests', () => {
|
||||
let type = 'quests';
|
||||
let key = 'gryphon';
|
||||
|
||||
let purchaseResponse = purchase(user, {params: {type, key}});
|
||||
|
||||
expect(purchaseResponse.message).to.equal(i18n.t('purchased', {type, key}));
|
||||
expect(user.items[type][key]).to.equal(1);
|
||||
});
|
||||
|
||||
it('purchases gear', () => {
|
||||
let type = 'gear';
|
||||
let key = 'headAccessory_special_tigerEars';
|
||||
|
||||
let purchaseResponse = purchase(user, {params: {type, key}});
|
||||
|
||||
expect(purchaseResponse.message).to.equal(i18n.t('purchased', {type, key}));
|
||||
expect(user.items.gear.owned[key]).to.be.true;
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -676,4 +676,27 @@ api.disableClasses = {
|
||||
},
|
||||
};
|
||||
|
||||
/**
|
||||
* @api {post} /user/purchase/:type/:key Purchase Gem Items.
|
||||
* @apiVersion 3.0.0
|
||||
* @apiName UserPurchase
|
||||
* @apiGroup User
|
||||
*
|
||||
* @apiParam {string} type Type of item to purchase
|
||||
* @apiParam {string} key Item's key
|
||||
*
|
||||
* @apiSuccess {Object} data `items balance`
|
||||
*/
|
||||
api.purchase = {
|
||||
method: 'POST',
|
||||
middlewares: [authWithHeaders(), cron],
|
||||
url: '/user/purchase/:type/:key',
|
||||
async handler (req, res) {
|
||||
let user = res.locals.user;
|
||||
let purchaseResponse = common.ops.purchase(user, req, res.analytics);
|
||||
await user.save();
|
||||
res.respond(200, purchaseResponse);
|
||||
},
|
||||
};
|
||||
|
||||
module.exports = api;
|
||||
|
||||
Reference in New Issue
Block a user