Files
habitica/website/common/script/ops/openMysteryItem.js
Matteo Pagliazzi 0b8ce63c76 WIP: Improve User model performances (#10832)
* wip: define items as mixed objects

* add default owned gear

* mark modified

* more mark modified

* more mark modified

* more mark modified

* more mark modified

* fix common tests

* fix common tests

* update mongoose

* add itemsUtils

* use new util function in hall controller

* add tests for items utils

* update website/server to mark all items as modified

* start updating common code

* update login incentives

* update unlock

* remove changes to package-lock.json

* remove changes to package.json
2019-04-01 19:24:18 +02:00

50 lines
1.2 KiB
JavaScript

import content from '../content/index';
import i18n from '../i18n';
import {
BadRequest,
} from '../libs/errors';
import cloneDeep from 'lodash/cloneDeep';
function markNotificationAsRead (user) {
const index = user.notifications.findIndex(notification => {
return notification && notification.type === 'NEW_MYSTERY_ITEMS';
});
if (index !== -1) user.notifications.splice(index, 1);
}
module.exports = function openMysteryItem (user, req = {}, analytics) {
const mysteryItems = user.purchased.plan.mysteryItems;
let item = mysteryItems.shift();
if (!item) {
throw new BadRequest(i18n.t('mysteryItemIsEmpty', req.language));
}
if (mysteryItems.length === 0) markNotificationAsRead(user);
item = cloneDeep(content.gear.flat[item]);
user.items.gear.owned[item.key] = true;
if (user.markModified) {
user.markModified('purchased.plan.mysteryItems');
user.markModified('items.gear.owned');
}
if (analytics) {
analytics.track('open mystery item', {
uuid: user._id,
itemKey: item,
itemType: 'Subscriber Gear',
acquireMethod: 'Subscriber',
category: 'behavior',
headers: req.headers,
});
}
return [
item,
i18n.t('mysteryItemOpened', req.language),
];
};