Files
habitica/website/common/script/ops/sell.js
negue f72f71fd32 [WIP] New Client - Shops/Market (#8884)
* initial market - routing - store - load market data

* move drawer/drawerSlider / count/star badge to components/ui

* filter market categories

* shopItem with gem / gold

* show count of purchable items

* show count of purchable itemsshow drawer with currently owned items + DrawerHeaderTabs-Component

* show featured gear

* show Gear - filter by class - sort by (type, price, stats) - sort market items

* Component: ItemRows - shows only the max items in one row (depending on the available width)

* Sell Dialog + Balance Component

* generic buy-dialog / attributes grid with highlight

* buyItem - hide already owned gear

* filter: hide locked/pinned - lock items if not enough gold

* API: Sell multiple items

* show avatar in buy-equipment-dialog with changed gear

* market banner

* misc fixes

* filter by text

* pin/unpin gear store actions

* Sell API: amount as query-parameter

* Update user.js

* fixes

* fix sell api amount test

* add back stroke/fill currentColor

* use scss variables
2017-07-27 19:41:23 +02:00

48 lines
1.2 KiB
JavaScript

import content from '../content/index';
import i18n from '../i18n';
import get from 'lodash/get';
import pick from 'lodash/pick';
import splitWhitespace from '../libs/splitWhitespace';
import {
NotFound,
NotAuthorized,
BadRequest,
} from '../libs/errors';
const ACCEPTEDTYPES = ['eggs', 'hatchingPotions', 'food'];
module.exports = function sell (user, req = {}) {
let key = get(req.params, 'key');
let type = get(req.params, 'type');
let amount = get(req.query, 'amount', 1);
if (!type) {
throw new BadRequest(i18n.t('typeRequired', req.language));
}
if (!key) {
throw new BadRequest(i18n.t('keyRequired', req.language));
}
if (ACCEPTEDTYPES.indexOf(type) === -1) {
throw new NotAuthorized(i18n.t('typeNotSellable', {acceptedTypes: ACCEPTEDTYPES.join(', ')}, req.language));
}
if (!user.items[type][key]) {
throw new NotFound(i18n.t('userItemsKeyNotFound', {type}, req.language));
}
let currentAmount = user.items[type][key];
if (amount > currentAmount) {
throw new NotFound(i18n.t('userItemsNotEnough', {type}, req.language));
}
user.items[type][key] -= amount;
user.stats.gp += content[type][key].value * amount;
return [
pick(user, splitWhitespace('stats items')),
];
};