342 lines
12 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-08-02 13:17:40 +02:00
const _ = require('lodash');
2017-07-19 15:12:27 +02:00
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;
if (!env || _.isEmpty(_.find(Service.getEnvironments(), { name: env }))) return ctx.badRequest(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;
if (!env || _.isEmpty(_.find(Service.getEnvironments(), { name: env }))) return ctx.badRequest(null, [{ messages: [{ id: 'request.error.environment.unknow' }] }]);
2017-08-01 18:23:19 +02:00
if (!name || _.isEmpty(_.find(Service.getDatabases(env), { name }))) return ctx.badRequest(null, [{ messages: [{ id: 'request.error.database.unknow' }] }]);
2017-07-26 16:15:31 +02:00
const model = Service.databases(name, env);
ctx.send(model);
},
databaseModel: async ctx => {
const Service = strapi.plugins['settings-manager'].services.settingsmanager;
const env = strapi.config.environment;
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.badRequest(null, [{ messages: [{ id: 'request.error.environment.unknow' }] }]);
_.has(Service, slug) ? ctx.send(await Service[slug](env)) : ctx.badRequest(null, [{ messages: [{ id: 'request.error.config' }] }]);
},
2017-07-10 18:00:50 +02:00
update: async ctx => {
const Service = strapi.plugins['settings-manager'].services.settingsmanager;
const { slug, env } = ctx.params;
2017-08-02 17:17:54 +02:00
let params = ctx.request.body;
2017-07-10 18:00:50 +02:00
if (env && _.isEmpty(_.find(Service.getEnvironments(), { name: env }))) return ctx.badRequest(null, [{ messages: [{ id: 'request.error.environment.unknow' }] }]);
2017-07-11 14:56:52 +02:00
2017-08-02 12:12:28 +02:00
let model;
if (_.has(Service, slug)) {
model = await Service[slug](env);
2017-08-02 12:12:28 +02:00
} else {
return ctx.badRequest(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-08-01 18:23:19 +02:00
if (!_.isEmpty(validationErrors)) return ctx.badRequest(null, Service.formatErrors(validationErrors));
2017-07-11 11:41:00 +02:00
strapi.reload.isWatching = false;
const updateErrors = await Service.updateSettings(params, items, env);
2017-08-01 18:23:19 +02:00
!_.isEmpty(updateErrors) ? ctx.badRequest(null, Service.formatErrors(updateErrors)) : ctx.send({ ok: true });
strapi.reload();
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
2017-08-03 14:06:59 +02:00
if (_.find(languages, { name: _.lowerCase(name).replace(' ', '_') })) return ctx.badRequest(null, [{ messages: [{ id: 'request.error.languages.exist' }] }]);
2017-08-01 18:23:19 +02:00
if (!_.find(availableLanguages, { value: name })) return ctx.badRequest(null, [{ messages: [{ id: 'request.error.languages.incorrect' }] }]);
2017-07-19 15:12:27 +02:00
2017-08-01 18:23:19 +02:00
const filePath = path.join(strapi.config.appPath, 'config', 'locales', `${name}.json`);
2017-07-26 17:55:24 +02:00
try {
fs.writeFileSync(filePath, '{}');
ctx.send({ ok: true });
2017-08-03 17:13:25 +02:00
strapi.reload();
2017-07-26 17:55:24 +02:00
} catch (e) {
2017-08-01 18:23:19 +02:00
ctx.badRequest(null, Service.formatErrors([{
2017-07-26 17:55:24 +02:00
target: 'name',
message: 'request.error.config',
params: {
filePath: filePath
}
}]));
}
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-08-01 18:23:19 +02:00
if (!_.find(languages, { name })) return ctx.badRequest(null, [{ messages: [{ id: 'request.error.languages.unknow' }] }]);
2017-07-19 15:24:26 +02:00
2017-08-01 18:23:19 +02:00
const filePath = path.join(strapi.config.appPath, 'config', 'locales', `${name}.json`);
2017-07-26 17:55:24 +02:00
try {
fs.unlinkSync(filePath);
ctx.send({ ok: true });
2018-04-26 14:21:17 +02:00
strapi.reload();
2017-07-26 17:55:24 +02:00
} catch (e) {
2017-08-01 18:23:19 +02:00
ctx.badRequest(null, Service.formatErrors([{
2017-07-26 17:55:24 +02:00
target: 'name',
message: 'request.error.config',
params: {
filePath: filePath
}
}]));
}
2017-07-26 12:43:46 +02:00
},
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;
if (!env || _.isEmpty(_.find(Service.getEnvironments(), { name: env }))) return ctx.badRequest(null, [{ messages: [{ id: 'request.error.environment.unknow' }] }]);
2017-08-18 18:18:21 +02:00
2017-08-02 17:17:54 +02:00
const [name] = _.keys(params.database.connections);
2017-07-26 16:15:31 +02:00
2017-08-01 18:23:19 +02:00
if (!name || _.find(Service.getDatabases(env), { name })) return ctx.badRequest(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);
2017-08-02 17:17:54 +02:00
params.database.connections[name].connector = Service.getClientConnector(params.database.connections[name].settings.client);
2017-08-01 18:23:19 +02:00
if (!_.isEmpty(validationErrors)) return ctx.badRequest(null, Service.formatErrors(validationErrors));
2017-07-26 16:15:31 +02:00
2017-08-11 11:36:33 +02:00
if (_.isEmpty(_.keys(strapi.config.environments[env].database.connections)) || _.isEmpty(strapi.config.environments[env].database.defaultConnection)) {
params.database.defaultConnection = name;
items.push({
target: 'database.defaultConnection'
});
}
Service.installDependency(params, name);
strapi.reload.isWatching = false;
2017-07-26 16:15:31 +02:00
const updateErrors = Service.updateSettings(params, items, env);
2017-08-01 18:23:19 +02:00
if (!_.isEmpty(updateErrors)) return ctx.badRequest(null, Service.formatErrors(updateErrors));
2017-07-26 16:15:31 +02:00
ctx.send({ ok: true });
strapi.reload();
2017-07-26 16:15:31 +02:00
},
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;
if (!env || _.isEmpty(_.find(Service.getEnvironments(), { name: env }))) return ctx.badRequest(null, [{ messages: [{ id: 'request.error.environment.unknow' }] }]);
2017-08-01 18:23:19 +02:00
if (!name || _.isEmpty(_.find(Service.getDatabases(env), { name }))) return ctx.badRequest(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);
2017-08-01 18:23:19 +02:00
if (!_.isEmpty(validationErrors)) return ctx.badRequest(null, Service.formatErrors(validationErrors));
2017-07-26 12:43:46 +02:00
2017-08-02 17:17:54 +02:00
const newName = _.get(params, `database.connections.${name}.name`);
2017-08-21 14:11:53 +02:00
const defaultConnection = params.database.defaultConnection;
2017-07-26 12:43:46 +02:00
2017-08-21 14:11:53 +02:00
if (params.database.connections) {
const settings = _.assign(_.clone(strapi.config.environments[env].database.connections[name].settings), params.database.connections[name].settings);
const options = _.assign(_.clone(strapi.config.environments[env].database.connections[name].options), params.database.connections[name].options);
2017-08-21 14:11:53 +02:00
params = _.assign(_.clone(strapi.config.environments[env].database.connections[name]), params.database.connections[name]);
params.settings = settings;
params.options = options;
2017-08-21 14:11:53 +02:00
}
delete params.name;
2017-08-04 12:03:22 +02:00
const connections = _.clone(strapi.config.environments[env].database.connections);
2017-07-26 12:43:46 +02:00
2017-08-22 15:41:25 +02:00
if (newName && newName !== name) {
2017-07-26 12:43:46 +02:00
connections[newName] = params;
connections[name] = undefined;
2017-08-11 11:36:33 +02:00
_.forEach(strapi.models, (model, modelName) => {
if (name === model.connection) {
const [searchFilePath, getModelPathErrors] = Service.getModelPath(modelName);
if (!_.isEmpty(getModelPathErrors)) {
return ctx.badRequest(null, Service.formatErrors(getModelPathErrors));
}
try {
const modelJSON = require(searchFilePath);
modelJSON.connection = newName;
try {
fs.writeFileSync(searchFilePath, JSON.stringify(modelJSON, null, 2), 'utf8');
} catch (e) {
return ctx.badRequest(null, Service.formatErrors([{
id: 'request.error.mode.write',
params: {
filePath: searchFilePath
}
}]));
}
} catch (e) {
return ctx.badRequest(null, Service.formatErrors([{
id: 'request.error.mode.read',
params: {
filePath: searchFilePath
}
}]));
}
}
});
2017-08-22 15:41:25 +02:00
} else if (params.settings) {
connections[name] = params;
2017-07-26 12:43:46 +02:00
}
params = { database: { connections }};
items = [{ target: 'database.connections' }];
2017-08-11 11:36:33 +02:00
if (newName && newName !== name && strapi.config.environments[env].database.defaultConnection === name) {
params.database.defaultConnection = newName;
items.push({
target: 'database.defaultConnection'
});
2017-08-21 14:11:53 +02:00
} else if (defaultConnection) {
params.database.defaultConnection = defaultConnection;
items.push({
target: 'database.defaultConnection'
});
2017-08-11 11:36:33 +02:00
}
const newClient = _.get(params, `database.connections.${name}.settings.client`);
if (newClient) params.database.connections[name].connector = Service.getClientConnector(newClient);
strapi.reload.isWatching = false;
const cleanErrors = Service.cleanDependency(env, params);
if (!_.isEmpty(cleanErrors)) {
return ctx.badRequest(null, Service.formatErrors(cleanErrors));
}
Service.installDependency(params, name);
2017-07-26 12:43:46 +02:00
const updateErrors = Service.updateSettings(params, items, env);
2017-08-01 18:23:19 +02:00
!_.isEmpty(updateErrors) ? ctx.badRequest(null, Service.formatErrors(updateErrors)) : ctx.send({ ok: true });
strapi.reload();
2017-07-26 17:10:19 +02:00
},
deleteDatabase: async ctx => {
const Service = strapi.plugins['settings-manager'].services.settingsmanager;
const { name, env } = ctx.params;
if (!env || _.isEmpty(_.find(Service.getEnvironments(), { name: env }))) return ctx.badRequest(null, [{ messages: [{ id: 'request.error.environment.unknow' }] }]);
2017-08-01 18:23:19 +02:00
if (!name || _.isEmpty(_.find(Service.getDatabases(env), { name }))) return ctx.badRequest(null, [{ messages: [{ id: 'request.error.database.unknow' }] }]);
2017-07-26 17:10:19 +02:00
2017-08-03 17:31:13 +02:00
const connections = _.clone(strapi.config.environments[env].database.connections);
2017-07-26 17:10:19 +02:00
connections[name] = undefined;
2017-08-03 17:31:13 +02:00
const params = { database: { connections }};
const items = [{ target: 'database.connections' }];
2017-07-26 17:10:19 +02:00
2017-08-11 11:36:33 +02:00
if (strapi.config.environments[env].database.defaultConnection === name) {
params.database.defaultConnection = '';
items.push({
target: 'database.defaultConnection'
});
}
2017-08-01 18:23:19 +02:00
strapi.reload.isWatching = false;
2017-07-26 17:10:19 +02:00
const updateErrors = Service.updateSettings(params, items, env);
2017-08-01 18:23:19 +02:00
!_.isEmpty(updateErrors) ? ctx.badRequest(null, Service.formatErrors(updateErrors)) : ctx.send({ ok: true });
strapi.reload();
2017-08-23 13:21:54 +02:00
},
autoReload: async ctx => {
ctx.send({
autoReload: _.get(strapi.config.environments, 'development.server.autoReload', false),
environment: strapi.config.environment,
});
2017-07-19 14:31:17 +02:00
}
2017-07-06 10:02:00 +02:00
};