mirror of
https://github.com/strapi/strapi.git
synced 2025-07-07 17:12:33 +00:00
95 lines
3.3 KiB
JavaScript
95 lines
3.3 KiB
JavaScript
'use strict';
|
|
|
|
const path = require('path');
|
|
const fs = require('fs');
|
|
|
|
module.exports = {
|
|
menu: async ctx => {
|
|
const Service = strapi.plugins['settings-manager'].services.settingsmanager;
|
|
|
|
ctx.send(Service.menu);
|
|
},
|
|
|
|
environments: async ctx => {
|
|
const Service = strapi.plugins['settings-manager'].services.settingsmanager;
|
|
|
|
ctx.send({ environments: Service.getEnvironments() });
|
|
},
|
|
|
|
languages: async ctx => {
|
|
const Service = strapi.plugins['settings-manager'].services.settingsmanager;
|
|
|
|
ctx.send({ languages: Service.getLanguages() });
|
|
},
|
|
|
|
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 = Service[slug](env);
|
|
|
|
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' }] }]);
|
|
|
|
ctx.send(model);
|
|
},
|
|
|
|
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' }] }]);
|
|
|
|
const model = Service[slug](env);
|
|
|
|
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' }] }]);
|
|
|
|
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 });
|
|
},
|
|
|
|
createLanguage: async ctx => {
|
|
const Service = strapi.plugins['settings-manager'].services.settingsmanager;
|
|
const { name } = ctx.request.body;
|
|
|
|
const languages = Service.getLanguages();
|
|
const availableLanguages = strapi.plugins['settings-manager'].services.languages;
|
|
|
|
if (_.find(languages, { name })) return ctx.badData(null, [{ messages: [{ id: 'request.error.languages.exist' }] }]);
|
|
if (!_.find(availableLanguages, { value: name })) return ctx.badData(null, [{ messages: [{ id: 'request.error.languages.incorrect' }] }]);
|
|
|
|
fs.writeFileSync(path.join(process.cwd(), 'config', 'locales', `${name}.json`), '{}');
|
|
|
|
ctx.send({ ok: true });
|
|
},
|
|
|
|
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`));
|
|
|
|
ctx.send({ ok: true });
|
|
}
|
|
};
|