202 lines
7.8 KiB
JavaScript
Raw Normal View History

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 = {
menu: async ctx => {
const Service = strapi.plugins['settings-manager'].services.settingsmanager;
2017-07-06 10:02:00 +02:00
ctx.send(Service.menu);
},
2017-07-06 10:02:00 +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-19 15:12:27 +02:00
languages: async ctx => {
const Service = strapi.plugins['settings-manager'].services.settingsmanager;
ctx.send({ languages: Service.getLanguages() });
2017-07-19 15:12:27 +02:00
},
2017-07-25 15:11:11 +02:00
databases: async ctx => {
const Service = strapi.plugins['settings-manager'].services.settingsmanager;
const { env } = ctx.params;
2017-07-26 17:39:41 +02:00
if (!env || _.isEmpty(_.find(Service.getEnvironments(), { name: env }))) return ctx.badData(null, [{ messages: [{ id: 'request.error.environment.unknow' }] }]);
2017-07-25 15:11:11 +02:00
ctx.send({ databases: Service.getDatabases(env) });
},
database: async ctx => {
const Service = strapi.plugins['settings-manager'].services.settingsmanager;
2017-07-26 12:43:46 +02:00
const { name, env } = ctx.params;
2017-07-26 16:15:31 +02:00
if (!env || _.isEmpty(_.find(Service.getEnvironments(), { name: env }))) return ctx.badData(null, [{ messages: [{ id: 'request.error.environment.unknow' }] }]);
if (!name || _.isEmpty(_.find(Service.getDatabases(env), { name }))) return ctx.badData(null, [{ messages: [{ id: 'request.error.database.unknow' }] }]);
const model = Service.databases(name, env);
ctx.send(model);
},
databaseModel: async ctx => {
const Service = strapi.plugins['settings-manager'].services.settingsmanager;
const { env } = ctx.params;
2017-07-11 14:56:52 +02:00
2017-07-26 16:15:31 +02:00
const model = Service.databases('${name}', env);
2017-07-11 14:56:52 +02:00
ctx.send(model);
},
2017-07-10 18:00:50 +02:00
get: async ctx => {
const Service = strapi.plugins['settings-manager'].services.settingsmanager;
const { slug, env } = ctx.params;
if (env && _.isEmpty(_.find(Service.getEnvironments(), { name: env }))) return ctx.badData(null, [{ messages: [{ id: 'request.error.environment.unknow' }] }]);
const model = _.has(Service, slug) ? Service[slug](env) : undefined;
2017-07-26 17:39:41 +02:00
_.isUndefined(model) ? ctx.badData(null, [{ messages: [{ id: 'request.error.config' }] }]) : ctx.send(model);
},
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;
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-26 12:43:46 +02:00
const model = _.has(Service, slug) ? Service[slug](env) : undefined;
2017-07-11 14:56:52 +02:00
if (_.isUndefined(model)) return ctx.badData(null, [{ messages: [{ id: 'request.error.config' }] }]);
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-26 12:43:46 +02:00
let validationErrors;
2017-07-20 15:08:03 +02:00
[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
const updateErrors = Service.updateSettings(params, items, env);
2017-07-26 17:39:41 +02:00
!_.isEmpty(updateErrors) ? ctx.badData(null, Service.formatErrors(updateErrors)) : ctx.send({ ok: true });
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-24 11:11:28 +02:00
ctx.send({ ok: true });
2017-07-19 15:24:26 +02:00
},
deleteLanguage: async ctx => {
const Service = strapi.plugins['settings-manager'].services.settingsmanager;
const { name } = ctx.params;
const languages = Service.getLanguages();
2017-07-26 17:39:41 +02:00
if (!_.find(languages, { name })) return ctx.badData(null, [{ messages: [{ id: 'request.error.languages.unknow' }] }]);
2017-07-19 15:24:26 +02:00
fs.unlinkSync(path.join(process.cwd(), 'config', 'locales', `${name}.json`));
2017-07-26 12:43:46 +02:00
ctx.send({ ok: true });
},
2017-07-26 16:15:31 +02:00
createDatabase: async ctx => {
const Service = strapi.plugins['settings-manager'].services.settingsmanager;
const { env } = ctx.params;
let params = ctx.request.body;
const [name] = _.keys(params.databases.connections);
2017-07-26 17:39:41 +02:00
if (!env || _.isEmpty(_.find(Service.getEnvironments(), { name: env }))) return ctx.badData(null, [{ messages: [{ id: 'request.error.environment.unknow' }] }]);
if (!name || _.find(Service.getDatabases(env), { name })) return ctx.badData(null, [{ messages: [{ id: 'request.error.database.exist' }] }]);
2017-07-26 16:15:31 +02:00
const model = Service.databases(name, env);
const items = Service.getItems(model);
params = Service.cleanParams(params, items);
let validationErrors;
[params, validationErrors] = Service.paramsValidation(params, items);
if (!_.isEmpty(validationErrors)) return ctx.badData(null, Service.formatErrors(validationErrors));
const updateErrors = Service.updateSettings(params, items, env);
if (!_.isEmpty(updateErrors)) return ctx.badData(null, Service.formatErrors(updateErrors));
ctx.send({ ok: true });
},
2017-07-26 12:43:46 +02:00
updateDatabase: async ctx => {
const Service = strapi.plugins['settings-manager'].services.settingsmanager;
const { name, env } = ctx.params;
let params = ctx.request.body;
2017-07-26 17:39:41 +02:00
if (!env || _.isEmpty(_.find(Service.getEnvironments(), { name: env }))) return ctx.badData(null, [{ messages: [{ id: 'request.error.environment.unknow' }] }]);
if (!name || _.isEmpty(_.find(Service.getDatabases(env), { name }))) return ctx.badData(null, [{ messages: [{ id: 'request.error.database.unknow' }] }]);
2017-07-26 12:43:46 +02:00
2017-07-26 16:15:31 +02:00
const model = Service.databases(name, env);
2017-07-26 12:43:46 +02:00
let items = Service.getItems(model);
params = Service.cleanParams(params, items);
let validationErrors;
[params, validationErrors] = Service.paramsValidation(params, items);
if (!_.isEmpty(validationErrors)) return ctx.badData(null, Service.formatErrors(validationErrors));
const newName = _.get(params, `databases.connections.${name}.name`);
if (newName && newName !== name) {
params = _.assign(_.clone(strapi.config.environments[env].databases.connections[name]), params.databases.connections[name]);
delete params.name;
const connections = _.clone(strapi.config.environments[env].databases.connections);
connections[newName] = params;
connections[name] = undefined;
params = { databases: { connections }};
items = [{ target: 'databases.connections' }];
}
const updateErrors = Service.updateSettings(params, items, env);
2017-07-26 17:39:41 +02:00
!_.isEmpty(updateErrors) ? ctx.badData(null, Service.formatErrors(updateErrors)) : ctx.send({ ok: true });
2017-07-26 17:10:19 +02:00
},
deleteDatabase: async ctx => {
const Service = strapi.plugins['settings-manager'].services.settingsmanager;
const { name, env } = ctx.params;
2017-07-26 17:39:41 +02:00
if (!env || _.isEmpty(_.find(Service.getEnvironments(), { name: env }))) return ctx.badData(null, [{ messages: [{ id: 'request.error.environment.unknow' }] }]);
if (!name || _.isEmpty(_.find(Service.getDatabases(env), { name }))) return ctx.badData(null, [{ messages: [{ id: 'request.error.database.unknow' }] }]);
2017-07-26 17:10:19 +02:00
const connections = _.clone(strapi.config.environments[env].databases.connections);
connections[name] = undefined;
const params = { databases: { connections }};
const items = [{ target: 'databases.connections' }];
const updateErrors = Service.updateSettings(params, items, env);
2017-07-26 17:39:41 +02:00
!_.isEmpty(updateErrors) ? ctx.badData(null, Service.formatErrors(updateErrors)) : ctx.send({ ok: true });
2017-07-19 14:31:17 +02:00
}
2017-07-06 10:02:00 +02:00
};