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

View File

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

View File

@ -122,8 +122,7 @@ module.exports = {
] ]
}, },
security: env => { security: env => ({
return {
name: 'form.security.name', name: 'form.security.name',
description: 'form.security.description', description: 'form.security.description',
sections: [ sections: [
@ -190,11 +189,9 @@ module.exports = {
] ]
} }
] ]
}; }),
},
server: env => { server: env => ({
return {
name: 'form.server.name', name: 'form.server.name',
description: 'form.server.description', description: 'form.server.description',
sections: [ sections: [
@ -216,8 +213,7 @@ module.exports = {
] ]
} }
] ]
}; }),
},
getEnvironments: () => { getEnvironments: () => {
return _.map(_.keys(strapi.config.environments), environment => { return _.map(_.keys(strapi.config.environments), environment => {
@ -228,12 +224,7 @@ module.exports = {
}); });
}, },
getItems: model => { getItems: model => _.flatten(_.map(model.sections, section => section.items)),
let items = [];
_.forEach(model.sections, section => items = _.concat(items, section.items));
return items;
},
cleanParams: (params, items) => { cleanParams: (params, items) => {
const cleanParams = {}; const cleanParams = {};
@ -243,6 +234,18 @@ module.exports = {
return cleanParams; 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) => { paramsValidation: (params, items) => {
let errors = []; 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; return errors;
}, },
updateSettings: (params, items, env = '') => { updateSettings: (params, items, env = '') => {
const appPath = process.cwd(); const appPath = strapi.config.appPath;
const errors = [];
_.forEach(items, ({ target }) => { _.forEach(items, ({ target }) => {
if (_.has(params, target)) { if (_.has(params, target)) {
const input = _.get(params, target, null); const input = _.get(params, target, null);
const [file, ...objPath] = target.split('.'); 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); _.set(fileContent, objPath, input);
fs.writeFileSync(filePath, JSON.stringify(fileContent, null, 2), 'utf8'); fs.writeFileSync(filePath, JSON.stringify(fileContent, null, 2), 'utf8');
} catch (e) {
errors.push({
target,
message: 'coucou',
params: {
filePath: filePath
} }
}); });
} }
}
});
return errors;
}
}; };