mirror of
https://github.com/HabitRPG/habitica.git
synced 2025-12-17 22:57:21 +01:00
Add Transaction log for gem and hourglass changes (#13589)
* 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>
This commit is contained in:
@@ -34,7 +34,7 @@ api.addTenGems = {
|
||||
async handler (req, res) {
|
||||
const { user } = res.locals;
|
||||
|
||||
user.balance += 2.5;
|
||||
await user.updateBalance(2.5, 'debug');
|
||||
|
||||
await user.save();
|
||||
|
||||
@@ -57,7 +57,7 @@ api.addHourglass = {
|
||||
async handler (req, res) {
|
||||
const { user } = res.locals;
|
||||
|
||||
user.purchased.plan.consecutive.trinkets += 1;
|
||||
await user.purchased.plan.updateHourglasses(user._id, 1, 'debug');
|
||||
|
||||
await user.save();
|
||||
|
||||
|
||||
@@ -124,7 +124,7 @@ api.createGroup = {
|
||||
|
||||
group.balance = 1;
|
||||
|
||||
user.balance -= 1;
|
||||
await user.updateBalance(-1, 'create_guild', group._id, group.name);
|
||||
user.guilds.push(group._id);
|
||||
if (!user.achievements.joinedGuild) {
|
||||
user.achievements.joinedGuild = true;
|
||||
|
||||
@@ -266,7 +266,7 @@ api.updateHero = {
|
||||
hero.flags.contributor = true;
|
||||
let tierDiff = newTier - oldTier; // can be 2+ tier increases at once
|
||||
while (tierDiff) {
|
||||
hero.balance += gemsPerTier[newTier] / 4; // balance is in $
|
||||
await hero.updateBalance(gemsPerTier[newTier] / 4, 'contribution', newTier); // eslint-disable-line no-await-in-loop
|
||||
tierDiff -= 1;
|
||||
newTier -= 1; // give them gems for the next tier down if they weren't already that tier
|
||||
}
|
||||
|
||||
@@ -714,8 +714,8 @@ api.transferGems = {
|
||||
throw new NotAuthorized(res.t('badAmountOfGemsToSend'));
|
||||
}
|
||||
|
||||
receiver.balance += amount;
|
||||
sender.balance -= amount;
|
||||
await receiver.updateBalance(amount, 'gift_receive', sender._id, sender.profile.name);
|
||||
await sender.updateBalance(-amount, 'gift_send', sender._id, receiver.profile.name);
|
||||
// @TODO necessary? Also saved when sending the inbox message
|
||||
const promises = [receiver.save(), sender.save()];
|
||||
await Promise.all(promises);
|
||||
|
||||
@@ -22,6 +22,7 @@ import {
|
||||
} from '../../libs/email';
|
||||
import * as inboxLib from '../../libs/inbox';
|
||||
import * as userLib from '../../libs/user';
|
||||
import logger from '../../libs/logger';
|
||||
|
||||
const TECH_ASSISTANCE_EMAIL = nconf.get('EMAILS_TECH_ASSISTANCE_EMAIL');
|
||||
const DELETE_CONFIRMATION = 'DELETE';
|
||||
@@ -493,7 +494,7 @@ api.buy = {
|
||||
let quantity = 1;
|
||||
if (req.body.quantity) quantity = req.body.quantity;
|
||||
req.quantity = quantity;
|
||||
const buyRes = common.ops.buy(user, req, res.analytics);
|
||||
const buyRes = await common.ops.buy(user, req, res.analytics);
|
||||
|
||||
await user.save();
|
||||
res.respond(200, ...buyRes);
|
||||
@@ -541,7 +542,7 @@ api.buyGear = {
|
||||
url: '/user/buy-gear/:key',
|
||||
async handler (req, res) {
|
||||
const { user } = res.locals;
|
||||
const buyGearRes = common.ops.buy(user, req, res.analytics);
|
||||
const buyGearRes = await common.ops.buy(user, req, res.analytics);
|
||||
await user.save();
|
||||
res.respond(200, ...buyGearRes);
|
||||
},
|
||||
@@ -583,7 +584,7 @@ api.buyArmoire = {
|
||||
const { user } = res.locals;
|
||||
req.type = 'armoire';
|
||||
req.params.key = 'armoire';
|
||||
const buyArmoireResponse = common.ops.buy(user, req, res.analytics);
|
||||
const buyArmoireResponse = await common.ops.buy(user, req, res.analytics);
|
||||
await user.save();
|
||||
res.respond(200, ...buyArmoireResponse);
|
||||
},
|
||||
@@ -623,7 +624,7 @@ api.buyHealthPotion = {
|
||||
const { user } = res.locals;
|
||||
req.type = 'potion';
|
||||
req.params.key = 'potion';
|
||||
const buyHealthPotionResponse = common.ops.buy(user, req, res.analytics);
|
||||
const buyHealthPotionResponse = await common.ops.buy(user, req, res.analytics);
|
||||
await user.save();
|
||||
res.respond(200, ...buyHealthPotionResponse);
|
||||
},
|
||||
@@ -665,7 +666,7 @@ api.buyMysterySet = {
|
||||
async handler (req, res) {
|
||||
const { user } = res.locals;
|
||||
req.type = 'mystery';
|
||||
const buyMysterySetRes = common.ops.buy(user, req, res.analytics);
|
||||
const buyMysterySetRes = await common.ops.buy(user, req, res.analytics);
|
||||
await user.save();
|
||||
res.respond(200, ...buyMysterySetRes);
|
||||
},
|
||||
@@ -708,7 +709,7 @@ api.buyQuest = {
|
||||
async handler (req, res) {
|
||||
const { user } = res.locals;
|
||||
req.type = 'quest';
|
||||
const buyQuestRes = common.ops.buy(user, req, res.analytics);
|
||||
const buyQuestRes = await common.ops.buy(user, req, res.analytics);
|
||||
await user.save();
|
||||
res.respond(200, ...buyQuestRes);
|
||||
},
|
||||
@@ -750,7 +751,7 @@ api.buySpecialSpell = {
|
||||
async handler (req, res) {
|
||||
const { user } = res.locals;
|
||||
req.type = 'special';
|
||||
const buySpecialSpellRes = common.ops.buy(user, req);
|
||||
const buySpecialSpellRes = await common.ops.buy(user, req);
|
||||
await user.save();
|
||||
res.respond(200, ...buySpecialSpellRes);
|
||||
},
|
||||
@@ -941,7 +942,7 @@ api.changeClass = {
|
||||
url: '/user/change-class',
|
||||
async handler (req, res) {
|
||||
const { user } = res.locals;
|
||||
const changeClassRes = common.ops.changeClass(user, req, res.analytics);
|
||||
const changeClassRes = await common.ops.changeClass(user, req, res.analytics);
|
||||
await user.save();
|
||||
res.respond(200, ...changeClassRes);
|
||||
},
|
||||
@@ -1013,7 +1014,8 @@ api.purchase = {
|
||||
if (req.body.quantity) quantity = req.body.quantity;
|
||||
req.quantity = quantity;
|
||||
|
||||
const purchaseRes = common.ops.buy(user, req, res.analytics);
|
||||
logger.info('AAAAHHHHHH');
|
||||
const purchaseRes = await common.ops.buy(user, req, res.analytics);
|
||||
await user.save();
|
||||
res.respond(200, ...purchaseRes);
|
||||
},
|
||||
@@ -1053,7 +1055,7 @@ api.userPurchaseHourglass = {
|
||||
const { user } = res.locals;
|
||||
const quantity = req.body.quantity || 1;
|
||||
if (quantity < 1 || !Number.isInteger(quantity)) throw new BadRequest(res.t('invalidQuantity'), req.language);
|
||||
const purchaseHourglassRes = common.ops.buy(
|
||||
const purchaseHourglassRes = await common.ops.buy(
|
||||
user,
|
||||
req,
|
||||
res.analytics,
|
||||
@@ -1185,7 +1187,7 @@ api.userReleasePets = {
|
||||
url: '/user/release-pets',
|
||||
async handler (req, res) {
|
||||
const { user } = res.locals;
|
||||
const releasePetsRes = common.ops.releasePets(user, req, res.analytics);
|
||||
const releasePetsRes = await common.ops.releasePets(user, req, res.analytics);
|
||||
await user.save();
|
||||
res.respond(200, ...releasePetsRes);
|
||||
},
|
||||
@@ -1270,7 +1272,7 @@ api.userReleaseMounts = {
|
||||
url: '/user/release-mounts',
|
||||
async handler (req, res) {
|
||||
const { user } = res.locals;
|
||||
const releaseMountsRes = common.ops.releaseMounts(user, req, res.analytics);
|
||||
const releaseMountsRes = await common.ops.releaseMounts(user, req, res.analytics);
|
||||
await user.save();
|
||||
res.respond(200, ...releaseMountsRes);
|
||||
},
|
||||
@@ -1346,7 +1348,7 @@ api.userUnlock = {
|
||||
url: '/user/unlock',
|
||||
async handler (req, res) {
|
||||
const { user } = res.locals;
|
||||
const unlockRes = common.ops.unlock(user, req, res.analytics);
|
||||
const unlockRes = await common.ops.unlock(user, req, res.analytics);
|
||||
await user.save();
|
||||
res.respond(200, ...unlockRes);
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user