2017-07-06 10:02:00 +02:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
module.exports = {
|
2017-07-10 11:40:41 +02:00
|
|
|
menu: async ctx => {
|
|
|
|
const Service = strapi.plugins['settings-manager'].services.settingsmanager;
|
2017-07-06 10:02:00 +02:00
|
|
|
|
2017-07-10 11:40:41 +02:00
|
|
|
ctx.send(Service.menu);
|
|
|
|
},
|
2017-07-06 10:02:00 +02:00
|
|
|
|
2017-07-10 11:40:41 +02:00
|
|
|
environments: async ctx => {
|
2017-07-11 14:56:52 +02:00
|
|
|
const Service = strapi.plugins['settings-manager'].services.settingsmanager;
|
|
|
|
|
|
|
|
ctx.send({ environments: Service.getEnvironments() });
|
2017-07-10 11:40:41 +02:00
|
|
|
},
|
|
|
|
|
2017-07-10 18:00:50 +02:00
|
|
|
get: async ctx => {
|
2017-07-10 11:40:41 +02:00
|
|
|
const Service = strapi.plugins['settings-manager'].services.settingsmanager;
|
|
|
|
const { slug, env } = ctx.params;
|
|
|
|
|
2017-07-13 17:09:44 +02:00
|
|
|
if (env && _.isEmpty(_.find(Service.getEnvironments(), { name: env }))) return ctx.badData(null, [{ messages: [{ id: 'request.error.environment.unknow' }] }]);
|
2017-07-11 14:56:52 +02:00
|
|
|
|
|
|
|
const model = env ? Service[slug](env) : Service[slug];
|
|
|
|
|
2017-07-13 17:09:44 +02:00
|
|
|
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' }] }]);
|
2017-07-11 14:56:52 +02:00
|
|
|
|
|
|
|
ctx.send(model);
|
2017-07-10 11:40:41 +02:00
|
|
|
},
|
2017-07-10 18:00:50 +02:00
|
|
|
|
|
|
|
update: async ctx => {
|
|
|
|
const Service = strapi.plugins['settings-manager'].services.settingsmanager;
|
|
|
|
const { slug, env } = ctx.params;
|
|
|
|
let params = ctx.request.body;
|
|
|
|
|
2017-07-13 17:09:44 +02:00
|
|
|
if (env && _.isEmpty(_.find(Service.getEnvironments(), { name: env }))) return ctx.badData(null, [{ messages: [{ id: 'request.error.environment.unknow' }] }]);
|
2017-07-11 14:56:52 +02:00
|
|
|
|
2017-07-10 18:00:50 +02:00
|
|
|
const model = env ? Service[slug](env) : Service[slug];
|
2017-07-11 14:56:52 +02:00
|
|
|
|
2017-07-13 17:09:44 +02:00
|
|
|
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' }] }]);
|
2017-07-11 14:56:52 +02:00
|
|
|
|
2017-07-10 18:00:50 +02:00
|
|
|
const items = Service.getItems(model);
|
|
|
|
|
|
|
|
params = Service.cleanParams(params, items);
|
|
|
|
|
2017-07-13 17:09:44 +02:00
|
|
|
const validationErrors = Service.paramsValidation(params, items);
|
2017-07-10 18:00:50 +02:00
|
|
|
|
2017-07-11 11:41:00 +02:00
|
|
|
if (!_.isEmpty(validationErrors)) {
|
2017-07-13 17:09:44 +02:00
|
|
|
return ctx.badData(null, Service.formatErrors(validationErrors));
|
2017-07-10 18:00:50 +02:00
|
|
|
}
|
2017-07-11 11:41:00 +02:00
|
|
|
|
2017-07-13 17:09:44 +02:00
|
|
|
const updateErrors = Service.updateSettings(params, items, env);
|
|
|
|
|
|
|
|
if (!_.isEmpty(updateErrors)) {
|
|
|
|
return ctx.badData(null, Service.formatErrors(updateErrors));
|
|
|
|
}
|
2017-07-11 11:41:00 +02:00
|
|
|
|
|
|
|
ctx.send();
|
2017-07-10 18:00:50 +02:00
|
|
|
},
|
2017-07-06 10:02:00 +02:00
|
|
|
};
|