diff --git a/packages/strapi-plugin-content-manager/config/functions/bootstrap.js b/packages/strapi-plugin-content-manager/config/functions/bootstrap.js index c76fc32720..f1389e7104 100644 --- a/packages/strapi-plugin-content-manager/config/functions/bootstrap.js +++ b/packages/strapi-plugin-content-manager/config/functions/bootstrap.js @@ -1,31 +1,12 @@ 'use strict'; const _ = require('lodash'); - -function getContentManagerKeys({ model }, key) { - if (model.orm === 'mongoose') { - return model - .find({ - $regex: `${key}.*`, - }) - .then(results => results.map(({ value }) => JSON.parse(value))); - } - - return model - .query(qb => { - qb.where('key', 'like', `${key}%`); - }) - .fetchAll() - .then(config => config && config.toJSON()) - .then(results => results.map(({ value }) => JSON.parse(value))); -} +const storeUtils = require('../../services/utils/store'); async function updateGroups() { const service = strapi.plugins['content-manager'].services.groups; - const configurations = await strapi - .query('core_store') - .custom(getContentManagerKeys)( + const configurations = await storeUtils.findByKey( 'plugin_content_manager_configuration_groups' ); @@ -250,9 +231,7 @@ async function updateContentTypesScope(models, configurations, source) { } async function updateContentTypes() { - const configurations = await strapi - .query('core_store') - .custom(getContentManagerKeys)( + const configurations = await storeUtils.findByKey( 'plugin_content_manager_configuration_content_types' ); @@ -260,6 +239,7 @@ async function updateContentTypes() { _.omit(strapi.models, ['core_store']), configurations.filter(({ source }) => !source) ); + await updateContentTypesScope( strapi.admin.models, configurations.filter(({ source }) => source === 'admin'), diff --git a/packages/strapi-plugin-content-manager/controllers/GeneralSettings.js b/packages/strapi-plugin-content-manager/controllers/GeneralSettings.js index dc920cb584..55caa57caa 100644 --- a/packages/strapi-plugin-content-manager/controllers/GeneralSettings.js +++ b/packages/strapi-plugin-content-manager/controllers/GeneralSettings.js @@ -7,10 +7,9 @@ module.exports = { * Returns the general content manager settings */ async getGeneralSettings(ctx) { - const contentTypeService = - strapi.plugins['content-manager'].services.contenttypes; + const service = strapi.plugins['content-manager'].services.generalsettings; - const generalSettings = await contentTypeService.getGeneralSettings(); + const generalSettings = await service.getGeneralSettings(); ctx.body = { data: generalSettings }; }, @@ -21,8 +20,7 @@ module.exports = { */ async updateGeneralSettings(ctx) { const { body = {} } = ctx.request; - const contentTypeService = - strapi.plugins['content-manager'].services.contenttypes; + const service = strapi.plugins['content-manager'].services.generalsettings; let data; try { @@ -38,7 +36,7 @@ module.exports = { }); } - await contentTypeService.setGeneralSettings(data); + await service.setGeneralSettings(data); ctx.body = { data }; }, diff --git a/packages/strapi-plugin-content-manager/services/GeneralSettings.js b/packages/strapi-plugin-content-manager/services/GeneralSettings.js index 006b0e553f..b01ad90d7e 100644 --- a/packages/strapi-plugin-content-manager/services/GeneralSettings.js +++ b/packages/strapi-plugin-content-manager/services/GeneralSettings.js @@ -1,4 +1,5 @@ 'use strict'; + const storeUtils = require('./utils/store'); const defaultGeneralSettings = { @@ -15,7 +16,34 @@ module.exports = { return generalSettings || defaultGeneralSettings; }, - setGeneralSettings(data) { - return storeUtils.setGeneralSettings(data); + async setGeneralSettings(data) { + await storeUtils.setGeneralSettings(data); + + // overwriite all the other configuration settings + const groupsService = strapi.plugins['content-manager'].services.groups; + const contentTypesService = + strapi.plugins['content-manager'].services.contenttypes; + + const configurations = await storeUtils.getAllConfigurations(); + await Promise.all( + configurations.map(({ value }) => { + const { uid, source } = value; + const settings = { + ...value.settings, + ...data, + }; + + if (value.isGroup) { + return groupsService.setConfiguration(value.uid, { + settings, + }); + } + + return contentTypesService.setContentTypeConfiguration( + { uid, source }, + { settings } + ); + }) + ); }, }; diff --git a/packages/strapi-plugin-content-manager/services/utils/store.js b/packages/strapi-plugin-content-manager/services/utils/store.js index 72c01346ce..6b372813c4 100644 --- a/packages/strapi-plugin-content-manager/services/utils/store.js +++ b/packages/strapi-plugin-content-manager/services/utils/store.js @@ -1,4 +1,5 @@ 'use strict'; + const _ = require('lodash'); const keys = { @@ -14,6 +15,31 @@ const getStore = () => { }); }; +function getAllConfigurationsQuery({ model }) { + if (model.orm === 'mongoose') { + return model + .find({ + $regex: `plugin_content_manager_configuration.*`, + }) + .then(results => + results.map(({ key, value }) => ({ key, value: JSON.parse(value) })) + ); + } + + return model + .query(qb => { + qb.where('key', 'like', `plugin_content_manager_configuration%`); + }) + .fetchAll() + .then(config => config && config.toJSON()) + .then(results => + results.map(({ key, value }) => ({ key, value: JSON.parse(value) })) + ); +} + +const getAllConfigurations = () => + strapi.query('core_store').custom(getAllConfigurationsQuery)(); + /** General settings */ const getGeneralSettings = () => @@ -69,11 +95,35 @@ const deleteKey = key => { .delete({ key: `plugin_content_manager_configuration_${key}` }); }; +function findByKeyQuery({ model }, key) { + if (model.orm === 'mongoose') { + return model + .find({ + $regex: `${key}.*`, + }) + .then(results => results.map(({ value }) => JSON.parse(value))); + } + + return model + .query(qb => { + qb.where('key', 'like', `${key}%`); + }) + .fetchAll() + .then(config => config && config.toJSON()) + .then(results => results.map(({ value }) => JSON.parse(value))); +} + +const findByKey = key => strapi.query('core_store').custom(findByKeyQuery)(key); + module.exports = { getGeneralSettings, setGeneralSettings, + + getAllConfigurations, + findByKey, getModelConfiguration, setModelConfiguration, deleteKey, + keys, };