Fix code review, review error message format

This commit is contained in:
Jim Laurie 2017-07-13 17:09:44 +02:00
parent 9507c999eb
commit a09e25620e
3 changed files with 136 additions and 143 deletions

View File

@ -18,15 +18,7 @@
},
{
"method": "GET",
"path": "/configs/:slug",
"handler": "SettingsManager.get",
"config": {
"policies": []
}
},
{
"method": "GET",
"path": "/configs/:slug/:env",
"path": "/configurations/:slug/:env?",
"handler": "SettingsManager.get",
"config": {
"policies": []
@ -34,15 +26,7 @@
},
{
"method": "PUT",
"path": "/configs/:slug",
"handler": "SettingsManager.update",
"config": {
"policies": []
}
},
{
"method": "PUT",
"path": "/configs/:slug/:env",
"path": "/configurations/:slug/:env?",
"handler": "SettingsManager.update",
"config": {
"policies": []

View File

@ -17,12 +17,12 @@ module.exports = {
const Service = strapi.plugins['settings-manager'].services.settingsmanager;
const { slug, env } = ctx.params;
if (env && _.isEmpty(_.find(Service.getEnvironments(), { name: env }))) return ctx.badData('request.error.environment.unknow');
if (env && _.isEmpty(_.find(Service.getEnvironments(), { name: env }))) return ctx.badData(null, [{ messages: [{ id: 'request.error.environment.unknow' }] }]);
const model = env ? Service[slug](env) : Service[slug];
if (_.isUndefined(model)) return ctx.badData('request.error.config');
if (_.isFunction(model)) return ctx.badData('request.error.environment.required');
if (_.isUndefined(model)) return ctx.badData(null, [{ messages: [{ id: 'request.error.config' }] }]);
if (_.isFunction(model)) return ctx.badData(null, [{ messages: [{ id: 'request.error.environment.required' }] }]);
ctx.send(model);
},
@ -32,24 +32,28 @@ module.exports = {
const { slug, env } = ctx.params;
let params = ctx.request.body;
if (env && _.isEmpty(_.find(Service.getEnvironments(), { name: env }))) return ctx.badData('request.error.environment.unknow');
if (env && _.isEmpty(_.find(Service.getEnvironments(), { name: env }))) return ctx.badData(null, [{ messages: [{ id: 'request.error.environment.unknow' }] }]);
const model = env ? Service[slug](env) : Service[slug];
if (_.isUndefined(model)) return ctx.badData('request.error.config');
if (_.isFunction(model)) return ctx.badData('request.error.environment.required');
if (_.isUndefined(model)) return ctx.badData(null, [{ messages: [{ id: 'request.error.config' }] }]);
if (_.isFunction(model)) return ctx.badData(null, [{ messages: [{ id: 'request.error.environment.required' }] }]);
const items = Service.getItems(model);
params = Service.cleanParams(params, items);
let validationErrors = Service.paramsValidation(params, items);
const validationErrors = Service.paramsValidation(params, items);
if (!_.isEmpty(validationErrors)) {
return ctx.badData(null, validationErrors);
return ctx.badData(null, Service.formatErrors(validationErrors));
}
Service.updateSettings(params, items, env);
const updateErrors = Service.updateSettings(params, items, env);
if (!_.isEmpty(updateErrors)) {
return ctx.badData(null, Service.formatErrors(updateErrors));
}
ctx.send();
},

View File

@ -122,8 +122,7 @@ module.exports = {
]
},
security: env => {
return {
security: env => ({
name: 'form.security.name',
description: 'form.security.description',
sections: [
@ -190,11 +189,9 @@ module.exports = {
]
}
]
};
},
}),
server: env => {
return {
server: env => ({
name: 'form.server.name',
description: 'form.server.description',
sections: [
@ -216,8 +213,7 @@ module.exports = {
]
}
]
};
},
}),
getEnvironments: () => {
return _.map(_.keys(strapi.config.environments), environment => {
@ -228,12 +224,7 @@ module.exports = {
});
},
getItems: model => {
let items = [];
_.forEach(model.sections, section => items = _.concat(items, section.items));
return items;
},
getItems: model => _.flatten(_.map(model.sections, section => section.items)),
cleanParams: (params, items) => {
const cleanParams = {};
@ -243,6 +234,18 @@ module.exports = {
return cleanParams;
},
formatErrors: errors => _.map(_.groupBy(errors, 'target'), (errs, target) => {
return {
target,
messages: _.map(errs, err => {
return {
id: err.message,
params: _.get(err, 'params', undefined)
}
})
}
}),
paramsValidation: (params, items) => {
let errors = [];
@ -306,36 +309,38 @@ module.exports = {
}
});
if (!_.isEmpty(errors)) {
const grpTarget = _.groupBy(errors, 'target');
errors = _.map(grpTarget, (errs, target) => {
return {
target,
messages: _.map(errs, err => err.message)
}
});
}
return errors;
},
updateSettings: (params, items, env = '') => {
const appPath = process.cwd();
const appPath = strapi.config.appPath;
const errors = [];
_.forEach(items, ({ target }) => {
if (_.has(params, target)) {
const input = _.get(params, target, null);
const [file, ...objPath] = target.split('.');
let filePath = (file === 'package') ? path.join(appPath, 'package.json') : path.join(appPath, 'config', `${env ? `environments/${env}` : ''}`, `${_.replace(file, '.', '/')}.json`);
const filePath = (file === 'package') ? path.join(appPath, 'package.json') : path.join(appPath, 'config', `${env ? `environments/${env}` : ''}`, `${_.replace(file, '.', '/')}.json`);
const fileContent = require(filePath);
try {
const fileContent = require('coucou');
_.set(fileContent, objPath, input);
fs.writeFileSync(filePath, JSON.stringify(fileContent, null, 2), 'utf8');
} catch (e) {
errors.push({
target,
message: 'coucou',
params: {
filePath: filePath
}
});
}
}
});
return errors;
}
};