Files
habitica/website/common/script/ops/hatch.js
negue c26696a9eb moving developer-only strings to api/common messages (#10258)
* move translatable string to apiMessages

* use apiMessages instead of res.t for groupIdRequired / keepOrRemove

* move pageMustBeNumber to apiMessages

* change apimessages

* move missingKeyParam to apiMessages

* move more strings to apiMessages

* fix lint

* revert lodash imports to fix tests

* fix webhook test

* fix test

* rollback key change of `keepOrRemove`

* remove unneeded `req.language` param

*  extract more messages from i18n

* add missing `missingTypeParam` message

* Split api- and commonMessages

* fix test

* fix sanity

* merge messages to an object, rename commonMessage to errorMessage

* apiMessages -> apiError, commonMessages -> errorMessage, extract messages to separate objects

* fix test

* module.exports
2018-05-04 16:00:19 -05:00

42 lines
1.1 KiB
JavaScript

import content from '../content/index';
import i18n from '../i18n';
import get from 'lodash/get';
import {
BadRequest,
NotAuthorized,
NotFound,
} from '../libs/errors';
import errorMessage from '../libs/errorMessage';
module.exports = function hatch (user, req = {}) {
let egg = get(req, 'params.egg');
let hatchingPotion = get(req, 'params.hatchingPotion');
if (!(egg && hatchingPotion)) {
throw new BadRequest(errorMessage('missingEggHatchingPotion'));
}
if (!(user.items.eggs[egg] > 0 && user.items.hatchingPotions[hatchingPotion] > 0)) {
throw new NotFound(i18n.t('messageMissingEggPotion', req.language));
}
if (content.hatchingPotions[hatchingPotion].premium && !content.dropEggs[egg]) {
throw new BadRequest(i18n.t('messageInvalidEggPotionCombo', req.language));
}
let pet = `${egg}-${hatchingPotion}`;
if (user.items.pets[pet] && user.items.pets[pet] > 0) {
throw new NotAuthorized(i18n.t('messageAlreadyPet', req.language));
}
user.items.pets[pet] = 5;
user.items.eggs[egg]--;
user.items.hatchingPotions[hatchingPotion]--;
return [
user.items,
i18n.t('messageHatched', req.language),
];
};