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
This commit is contained in:
negue
2018-05-04 23:00:19 +02:00
committed by Sabe Jones
parent f226b5da07
commit c26696a9eb
65 changed files with 254 additions and 185 deletions

View File

@@ -1,4 +1,3 @@
import i18n from '../../i18n';
import get from 'lodash/get';
import {
BadRequest,
@@ -11,6 +10,7 @@ import {BuyQuestWithGoldOperation} from './buyQuest';
import buySpecialSpell from './buySpecialSpell';
import purchaseOp from './purchase';
import hourglassPurchase from './hourglassPurchase';
import errorMessage from '../../libs/errorMessage';
import {BuyGemOperation} from './buyGem';
// @TODO: remove the req option style. Dependency on express structure is an anti-pattern
@@ -20,7 +20,7 @@ import {BuyGemOperation} from './buyGem';
module.exports = function buy (user, req = {}, analytics) {
let key = get(req, 'params.key');
if (!key) throw new BadRequest(i18n.t('missingKeyParam', req.language));
if (!key) throw new BadRequest(errorMessage('missingKeyParam'));
// @TODO: Slowly remove the need for key and use type instead
// This should evenutally be the 'factory' function with vendor classes

View File

@@ -13,6 +13,7 @@ import ultimateGear from '../../fns/ultimateGear';
import {removePinnedGearAddPossibleNewOnes} from '../pinnedGearUtils';
import { AbstractGoldItemOperation } from './abstractBuyOperation';
import errorMessage from '../../libs/errorMessage';
export class BuyMarketGearOperation extends AbstractGoldItemOperation {
constructor (user, req, analytics) {
@@ -25,9 +26,10 @@ export class BuyMarketGearOperation extends AbstractGoldItemOperation {
extractAndValidateParams (user, req) {
let key = this.key = get(req, 'params.key');
if (!key) throw new BadRequest(this.i18n('missingKeyParam'));
if (!key) throw new BadRequest(errorMessage('missingKeyParam'));
let item = content.gear.flat[key];
if (!item) throw new NotFound(this.i18n('itemNotFound', {key}));
if (!item) throw new NotFound(errorMessage('itemNotFound', {key}));
this.canUserPurchase(user, item);

View File

@@ -7,10 +7,11 @@ import {
NotAuthorized,
NotFound,
} from '../../libs/errors';
import errorMessage from '../../libs/errorMessage';
module.exports = function buyMysterySet (user, req = {}, analytics) {
let key = get(req, 'params.key');
if (!key) throw new BadRequest(i18n.t('missingKeyParam', req.language));
if (!key) throw new BadRequest(errorMessage('missingKeyParam'));
if (!(user.purchased.plan.consecutive.trinkets > 0)) {
throw new NotAuthorized(i18n.t('notEnoughHourglasses', req.language));

View File

@@ -7,6 +7,7 @@ import content from '../../content/index';
import get from 'lodash/get';
import {AbstractGoldItemOperation} from './abstractBuyOperation';
import errorMessage from '../../libs/errorMessage';
export class BuyQuestWithGoldOperation extends AbstractGoldItemOperation {
constructor (user, req, analytics) {
@@ -30,7 +31,7 @@ export class BuyQuestWithGoldOperation extends AbstractGoldItemOperation {
extractAndValidateParams (user, req) {
let key = this.key = get(req, 'params.key');
if (!key) throw new BadRequest(this.i18n('missingKeyParam'));
if (!key) throw new BadRequest(errorMessage('missingKeyParam'));
if (key === 'lostMasterclasser1' && !this.userAbleToStartMasterClasser(user)) {
throw new NotAuthorized(this.i18n('questUnlockLostMasterclasser'));
@@ -38,7 +39,7 @@ export class BuyQuestWithGoldOperation extends AbstractGoldItemOperation {
let item = content.quests[key];
if (!item) throw new NotFound(this.i18n('questNotFound', {key}));
if (!item) throw new NotFound(errorMessage('questNotFound', {key}));
if (!(item.category === 'gold' && item.goldValue)) {
throw new NotAuthorized(this.i18n('questNotGoldPurchasable', {key}));

View File

@@ -8,15 +8,16 @@ import {
NotAuthorized,
NotFound,
} from '../../libs/errors';
import errorMessage from '../../libs/errorMessage';
module.exports = function buySpecialSpell (user, req = {}, analytics) {
let key = get(req, 'params.key');
let quantity = req.quantity || 1;
if (!key) throw new BadRequest(i18n.t('missingKeyParam', req.language));
if (!key) throw new BadRequest(errorMessage('missingKeyParam'));
let item = content.special[key];
if (!item) throw new NotFound(i18n.t('spellNotFound', {spellId: key}, req.language));
if (!item) throw new NotFound(errorMessage('spellNotFound', {spellId: key}));
if (user.stats.gp < item.value * quantity) {
throw new NotAuthorized(i18n.t('messageNotEnoughGold', req.language));

View File

@@ -7,13 +7,14 @@ import {
BadRequest,
NotAuthorized,
} from '../../libs/errors';
import errorMessage from '../../libs/errorMessage';
module.exports = function purchaseHourglass (user, req = {}, analytics) {
let key = get(req, 'params.key');
if (!key) throw new BadRequest(i18n.t('missingKeyParam', req.language));
if (!key) throw new BadRequest(errorMessage('missingKeyParam'));
let type = get(req, 'params.type');
if (!type) throw new BadRequest(i18n.t('missingTypeParam', req.language));
if (!type) throw new BadRequest(errorMessage('missingTypeParam'));
if (!content.timeTravelStable[type]) {
throw new NotAuthorized(i18n.t('typeNotAllowedHourglass', {allowedTypes: keys(content.timeTravelStable).toString()}, req.language));

View File

@@ -6,6 +6,7 @@ import {
BadRequest,
} from '../libs/errors';
import get from 'lodash/get';
import errorMessage from '../libs/errorMessage';
module.exports = function equip (user, req = {}) {
// Being type a parameter followed by another parameter
@@ -13,9 +14,9 @@ module.exports = function equip (user, req = {}) {
let type = get(req, 'params.type', 'equipped');
let key = get(req, 'params.key');
if (!key || !type) throw new BadRequest(i18n.t('missingTypeKeyEquip', req.language));
if (!key || !type) throw new BadRequest(errorMessage('missingTypeKeyEquip'));
if (['mount', 'pet', 'costume', 'equipped'].indexOf(type) === -1) {
throw new BadRequest(i18n.t('invalidTypeEquip', req.language));
throw new BadRequest(errorMessage('invalidTypeEquip'));
}
let message;

View File

@@ -6,6 +6,7 @@ import {
NotAuthorized,
NotFound,
} from '../libs/errors';
import errorMessage from '../libs/errorMessage';
function evolve (user, pet, req) {
user.items.pets[pet.key] = -1;
@@ -24,17 +25,17 @@ module.exports = function feed (user, req = {}) {
let pet = get(req, 'params.pet');
let foodK = get(req, 'params.food');
if (!pet || !foodK) throw new BadRequest(i18n.t('missingPetFoodFeed', req.language));
if (!pet || !foodK) throw new BadRequest(errorMessage('missingPetFoodFeed'));
pet = content.petInfo[pet];
if (!pet) {
throw new BadRequest(i18n.t('invalidPetName', req.language));
throw new BadRequest(errorMessage('invalidPetName'));
}
let food = content.food[foodK];
if (!food) {
throw new NotFound(i18n.t('messageFoodNotFound', req.language));
throw new NotFound(errorMessage('invalidFoodName', req.language));
}
let userPets = user.items.pets;

View File

@@ -6,13 +6,14 @@ import {
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(i18n.t('missingEggHatchingPotionHatch', req.language));
throw new BadRequest(errorMessage('missingEggHatchingPotion'));
}
if (!(user.items.eggs[egg] > 0 && user.items.hatchingPotions[hatchingPotion] > 0)) {

View File

@@ -7,12 +7,13 @@ import {
NotAuthorized,
} from '../../libs/errors';
import i18n from '../../i18n';
import errorMessage from '../../libs/errorMessage';
module.exports = function allocate (user, req = {}) {
let stat = get(req, 'query.stat', 'str');
if (ATTRIBUTES.indexOf(stat) === -1) {
throw new BadRequest(i18n.t('invalidAttribute', {attr: stat}, req.language));
throw new BadRequest(errorMessage('invalidAttribute', {attr: stat}));
}
if (user.stats.points > 0) {

View File

@@ -7,17 +7,18 @@ import {
NotAuthorized,
} from '../../libs/errors';
import i18n from '../../i18n';
import errorMessage from '../../libs/errorMessage';
module.exports = function allocateBulk (user, req = {}) {
const stats = get(req, 'body.stats');
if (!stats) throw new BadRequest(i18n.t('statsObjectRequired', req.language));
if (!stats) throw new BadRequest(errorMessage('statsObjectRequired'));
const statKeys = Object.keys(stats);
const invalidStats = statKeys.filter(statKey => {
return ATTRIBUTES.indexOf(statKey) === -1;
});
if (invalidStats.length > 0) {
throw new BadRequest(i18n.t('invalidAttribute', {attr: invalidStats.join(',')}, req.language));
throw new BadRequest(errorMessage('invalidAttribute', {attr: invalidStats.join(',')}));
}
if (user.stats.points <= 0) {