mirror of
https://github.com/HabitRPG/habitica.git
synced 2025-12-15 05:37:22 +01:00
49 lines
1.3 KiB
JavaScript
49 lines
1.3 KiB
JavaScript
import cloneDeep from 'lodash/cloneDeep';
|
|
import content from '../content/index';
|
|
import i18n from '../i18n';
|
|
import {
|
|
BadRequest,
|
|
} from '../libs/errors';
|
|
|
|
function markNotificationAsRead (user) {
|
|
const index = user.notifications.findIndex(notification => notification && notification.type === 'NEW_MYSTERY_ITEMS');
|
|
|
|
if (index !== -1) user.notifications.splice(index, 1);
|
|
}
|
|
|
|
export default function openMysteryItem (user, req = {}, analytics) {
|
|
const { mysteryItems } = user.purchased.plan;
|
|
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]);
|
|
item.text = content.gear.flat[item.key].text(user.preferences.language);
|
|
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),
|
|
];
|
|
}
|