2017-07-06 10:02:00 +02:00
|
|
|
'use strict';
|
|
|
|
|
2017-07-19 15:12:27 +02:00
|
|
|
const path = require('path');
|
|
|
|
const fs = require('fs');
|
|
|
|
|
2017-07-06 10:02:00 +02:00
|
|
|
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-19 15:12:27 +02:00
|
|
|
languages: async ctx => {
|
|
|
|
const Service = strapi.plugins['settings-manager'].services.settingsmanager;
|
|
|
|
|
2017-07-20 15:27:05 +02:00
|
|
|
ctx.send({ languages: Service.getLanguages() });
|
2017-07-19 15:12:27 +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
|
|
|
|
2017-07-19 18:35:59 +02:00
|
|
|
const model = Service[slug](env);
|
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
|
|
|
|
|
|
|
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-19 18:35:59 +02:00
|
|
|
const model = Service[slug](env);
|
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-20 15:08:03 +02:00
|
|
|
let validationErrors
|
|
|
|
[params, validationErrors] = Service.paramsValidation(params, items);
|
2017-07-10 18:00:50 +02:00
|
|
|
|
2017-07-19 09:52:29 +02:00
|
|
|
if (!_.isEmpty(validationErrors)) return ctx.badData(null, Service.formatErrors(validationErrors));
|
2017-07-11 11:41:00 +02:00
|
|
|
|
2017-07-13 17:09:44 +02:00
|
|
|
const updateErrors = Service.updateSettings(params, items, env);
|
|
|
|
|
2017-07-19 09:52:29 +02:00
|
|
|
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-19 14:31:17 +02:00
|
|
|
|
2017-07-19 15:12:27 +02:00
|
|
|
createLanguage: async ctx => {
|
2017-07-19 14:31:17 +02:00
|
|
|
const Service = strapi.plugins['settings-manager'].services.settingsmanager;
|
2017-07-19 15:12:27 +02:00
|
|
|
const { name } = ctx.request.body;
|
2017-07-19 14:31:17 +02:00
|
|
|
|
2017-07-19 15:12:27 +02:00
|
|
|
const languages = Service.getLanguages();
|
2017-07-20 11:27:33 +02:00
|
|
|
const availableLanguages = strapi.plugins['settings-manager'].services.languages;
|
2017-07-19 15:12:27 +02:00
|
|
|
|
|
|
|
if (_.find(languages, { name })) return ctx.badData(null, [{ messages: [{ id: 'request.error.languages.exist' }] }]);
|
2017-07-20 11:27:33 +02:00
|
|
|
if (!_.find(availableLanguages, { value: name })) return ctx.badData(null, [{ messages: [{ id: 'request.error.languages.incorrect' }] }]);
|
2017-07-19 15:12:27 +02:00
|
|
|
|
|
|
|
fs.writeFileSync(path.join(process.cwd(), 'config', 'locales', `${name}.json`), '{}');
|
|
|
|
|
2017-07-19 15:24:26 +02:00
|
|
|
ctx.send();
|
|
|
|
},
|
|
|
|
|
|
|
|
deleteLanguage: async ctx => {
|
|
|
|
const Service = strapi.plugins['settings-manager'].services.settingsmanager;
|
|
|
|
const { name } = ctx.params;
|
|
|
|
|
|
|
|
const languages = Service.getLanguages();
|
|
|
|
|
|
|
|
if (!_.find(languages, { name })) return ctx.badData(null, [{ messages: [{ id: 'request.error.languages.notExist' }] }]);
|
|
|
|
|
|
|
|
fs.unlinkSync(path.join(process.cwd(), 'config', 'locales', `${name}.json`));
|
|
|
|
|
2017-07-19 15:12:27 +02:00
|
|
|
ctx.send();
|
2017-07-19 14:31:17 +02:00
|
|
|
}
|
2017-07-06 10:02:00 +02:00
|
|
|
};
|