Update all models on general settings changes

This commit is contained in:
Alexandre Bodin 2019-07-22 17:01:25 +02:00
parent 6fbacc629d
commit e9ae95292e
4 changed files with 88 additions and 32 deletions

View File

@ -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'),

View File

@ -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 };
},

View File

@ -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 }
);
})
);
},
};

View File

@ -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,
};