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 = {
|
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-25 15:11:11 +02:00
|
|
|
databases: async ctx => {
|
|
|
|
const Service = strapi.plugins['settings-manager'].services.settingsmanager;
|
|
|
|
const { env } = ctx.params;
|
|
|
|
|
2017-08-01 18:23:19 +02:00
|
|
|
if (!env || _.isEmpty(_.find(Service.getEnvironments(), { name: env }))) return ctx.badRequest(null, [{ messages: [{ id: 'request.error.environment.unknown' }] }]);
|
2017-07-25 15:11:11 +02:00
|
|
|
|
|
|
|
ctx.send({ databases: Service.getDatabases(env) });
|
|
|
|
},
|
|
|
|
|
2017-07-25 16:37:50 +02:00
|
|
|
database: async ctx => {
|
2017-07-10 11:40:41 +02:00
|
|
|
const Service = strapi.plugins['settings-manager'].services.settingsmanager;
|
2017-07-26 12:43:46 +02:00
|
|
|
const { name, env } = ctx.params;
|
2017-07-10 11:40:41 +02:00
|
|
|
|
2017-08-01 18:23:19 +02:00
|
|
|
if (!env || _.isEmpty(_.find(Service.getEnvironments(), { name: env }))) return ctx.badRequest(null, [{ messages: [{ id: 'request.error.environment.unknown' }] }]);
|
|
|
|
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 } = 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 11:40:41 +02:00
|
|
|
},
|
2017-07-10 18:00:50 +02:00
|
|
|
|
2017-07-25 16:37:50 +02:00
|
|
|
get: async ctx => {
|
|
|
|
const Service = strapi.plugins['settings-manager'].services.settingsmanager;
|
|
|
|
const { slug, env } = ctx.params;
|
|
|
|
|
2017-08-01 18:23:19 +02:00
|
|
|
if (env && _.isEmpty(_.find(Service.getEnvironments(), { name: env }))) return ctx.badRequest(null, [{ messages: [{ id: 'request.error.environment.unknown' }] }]);
|
2017-07-25 16:37:50 +02:00
|
|
|
|
2017-08-01 18:23:19 +02:00
|
|
|
_.has(Service, slug) ? ctx.send(Service[slug](env)) : ctx.badRequest(null, [{ messages: [{ id: 'request.error.config' }] }]);
|
2017-07-25 16:37:50 +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;
|
2017-08-02 17:17:54 +02:00
|
|
|
let params = ctx.request.body;
|
2017-07-10 18:00:50 +02:00
|
|
|
|
2017-08-01 18:23:19 +02:00
|
|
|
if (env && _.isEmpty(_.find(Service.getEnvironments(), { name: env }))) return ctx.badRequest(null, [{ messages: [{ id: 'request.error.environment.unknown' }] }]);
|
2017-07-11 14:56:52 +02:00
|
|
|
|
2017-08-02 12:12:28 +02:00
|
|
|
let model;
|
|
|
|
if (_.has(Service, slug)) {
|
|
|
|
model = Service[slug](env);
|
|
|
|
} 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
|
|
|
|
2017-07-28 16:14:50 +02:00
|
|
|
strapi.reload.isWatching = false;
|
|
|
|
|
2017-07-13 17:09:44 +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 });
|
2017-07-28 16:14:50 +02:00
|
|
|
|
|
|
|
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 });
|
|
|
|
} 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:12:27 +02:00
|
|
|
|
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 });
|
|
|
|
} 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
|
|
|
|
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;
|
|
|
|
|
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 (!env || _.isEmpty(_.find(Service.getEnvironments(), { name: env }))) return ctx.badRequest(null, [{ messages: [{ id: 'request.error.environment.unknown' }] }]);
|
|
|
|
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-07-28 16:14:50 +02:00
|
|
|
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 });
|
2017-07-28 16:14:50 +02:00
|
|
|
|
|
|
|
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;
|
|
|
|
|
2017-08-01 18:23:19 +02:00
|
|
|
if (!env || _.isEmpty(_.find(Service.getEnvironments(), { name: env }))) return ctx.badRequest(null, [{ messages: [{ id: 'request.error.environment.unknown' }] }]);
|
|
|
|
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-07-26 12:43:46 +02:00
|
|
|
|
|
|
|
if (newName && newName !== name) {
|
2017-08-02 17:17:54 +02:00
|
|
|
params = _.assign(_.clone(strapi.config.environments[env].database.connections[name]), params.database.connections[name]);
|
2017-07-26 12:43:46 +02:00
|
|
|
delete params.name;
|
|
|
|
|
2017-08-02 17:17:54 +02:00
|
|
|
const connections = _.clone(strapi.config.environments[env].database.connections);
|
2017-07-26 12:43:46 +02:00
|
|
|
connections[newName] = params;
|
|
|
|
connections[name] = undefined;
|
|
|
|
|
|
|
|
params = { databases: { connections }};
|
|
|
|
|
|
|
|
items = [{ target: 'databases.connections' }];
|
|
|
|
}
|
|
|
|
|
2017-08-03 10:59:37 +02:00
|
|
|
const newClient = _.get(params, `database.connections.${name}.settings.client`);
|
|
|
|
|
|
|
|
if (newClient) params.database.connections[name].connector = Service.getClientConnector(newClient);
|
|
|
|
|
2017-07-28 16:14:50 +02:00
|
|
|
strapi.reload.isWatching = false;
|
|
|
|
|
2017-08-03 10:59:37 +02:00
|
|
|
Service.installDependency(params, name);
|
2017-07-28 16:56:17 +02:00
|
|
|
|
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 });
|
2017-07-28 16:14:50 +02:00
|
|
|
|
|
|
|
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;
|
|
|
|
|
2017-08-01 18:23:19 +02:00
|
|
|
if (!env || _.isEmpty(_.find(Service.getEnvironments(), { name: env }))) return ctx.badRequest(null, [{ messages: [{ id: 'request.error.environment.unknown' }] }]);
|
|
|
|
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
|
|
|
|
|
|
|
const connections = _.clone(strapi.config.environments[env].databases.connections);
|
|
|
|
connections[name] = undefined;
|
|
|
|
|
|
|
|
const params = { databases: { connections }};
|
|
|
|
const items = [{ target: 'databases.connections' }];
|
|
|
|
|
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-07-19 14:31:17 +02:00
|
|
|
}
|
2017-07-06 10:02:00 +02:00
|
|
|
};
|