2019-07-22 12:15:54 +02:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const _ = require('lodash');
|
2019-07-22 17:01:25 +02:00
|
|
|
const storeUtils = require('../../services/utils/store');
|
2019-07-23 10:54:44 +02:00
|
|
|
const {
|
|
|
|
createDefaultConfiguration,
|
|
|
|
syncConfiguration,
|
|
|
|
} = require('../../services/utils/configuration');
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Synchronize content manager schemas
|
|
|
|
*/
|
2019-08-12 15:35:40 +02:00
|
|
|
module.exports = () => {
|
|
|
|
return syncSchemas();
|
2019-07-22 16:24:44 +02:00
|
|
|
};
|
2019-07-22 12:15:54 +02:00
|
|
|
|
2019-07-23 10:54:44 +02:00
|
|
|
async function syncSchemas() {
|
|
|
|
await syncContentTypesSchemas();
|
|
|
|
await syncGroupsSchemas();
|
2019-07-22 12:15:54 +02:00
|
|
|
}
|
|
|
|
|
2019-07-23 10:54:44 +02:00
|
|
|
/**
|
|
|
|
* Sync content types schemas
|
|
|
|
*/
|
|
|
|
async function syncContentTypesSchemas() {
|
2019-07-22 17:01:25 +02:00
|
|
|
const configurations = await storeUtils.findByKey(
|
2019-07-22 12:15:54 +02:00
|
|
|
'plugin_content_manager_configuration_content_types'
|
|
|
|
);
|
|
|
|
|
2019-07-23 10:54:44 +02:00
|
|
|
// update strapi.models
|
2019-07-22 12:15:54 +02:00
|
|
|
await updateContentTypesScope(
|
|
|
|
_.omit(strapi.models, ['core_store']),
|
|
|
|
configurations.filter(({ source }) => !source)
|
|
|
|
);
|
2019-07-22 17:01:25 +02:00
|
|
|
|
2019-07-23 10:54:44 +02:00
|
|
|
// update strapi.admin.models
|
2019-07-22 12:15:54 +02:00
|
|
|
await updateContentTypesScope(
|
|
|
|
strapi.admin.models,
|
|
|
|
configurations.filter(({ source }) => source === 'admin'),
|
|
|
|
'admin'
|
|
|
|
);
|
|
|
|
|
2019-07-23 10:54:44 +02:00
|
|
|
// update strapi.plugins.x.models
|
2019-07-22 12:15:54 +02:00
|
|
|
await Promise.all(
|
|
|
|
Object.keys(strapi.plugins).map(pluginKey => {
|
|
|
|
const plugin = strapi.plugins[pluginKey];
|
2019-07-23 10:54:44 +02:00
|
|
|
|
2019-07-22 12:15:54 +02:00
|
|
|
return updateContentTypesScope(
|
|
|
|
plugin.models || {},
|
|
|
|
configurations.filter(({ source }) => source === pluginKey),
|
|
|
|
pluginKey
|
|
|
|
);
|
|
|
|
})
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-07-23 10:54:44 +02:00
|
|
|
async function updateContentTypesScope(models, configurations, source) {
|
|
|
|
const service = strapi.plugins['content-manager'].services.contenttypes;
|
2019-07-22 12:15:54 +02:00
|
|
|
|
2019-07-23 10:54:44 +02:00
|
|
|
const updateConfiguration = async uid => {
|
|
|
|
const conf = configurations.find(conf => conf.uid === uid);
|
|
|
|
const model = models[uid];
|
|
|
|
return service.setConfiguration(
|
|
|
|
{ uid, source },
|
|
|
|
await syncConfiguration(conf, model)
|
|
|
|
);
|
2019-07-22 12:15:54 +02:00
|
|
|
};
|
2019-07-22 14:36:46 +02:00
|
|
|
|
2019-07-23 10:54:44 +02:00
|
|
|
const generateNewConfiguration = async uid => {
|
|
|
|
return service.setConfiguration(
|
|
|
|
{ uid, source },
|
|
|
|
await createDefaultConfiguration(models[uid])
|
|
|
|
);
|
|
|
|
};
|
2019-07-22 14:36:46 +02:00
|
|
|
|
2019-07-23 10:54:44 +02:00
|
|
|
const realUIDs = Object.keys(models);
|
|
|
|
const DBUIDs = configurations.map(({ uid }) => uid);
|
|
|
|
const contentTypesToUpdate = _.intersection(realUIDs, DBUIDs);
|
|
|
|
const contentTypesToAdd = _.difference(realUIDs, DBUIDs);
|
|
|
|
const contentTypesToDelete = _.difference(DBUIDs, realUIDs);
|
2019-07-22 14:36:46 +02:00
|
|
|
|
2019-07-23 10:54:44 +02:00
|
|
|
// delette old schemas
|
|
|
|
await Promise.all(
|
|
|
|
contentTypesToDelete.map(uid =>
|
|
|
|
service.deleteConfiguration({ uid, source })
|
|
|
|
)
|
2019-07-22 14:36:46 +02:00
|
|
|
);
|
|
|
|
|
2019-07-23 10:54:44 +02:00
|
|
|
// create new schemas
|
|
|
|
await Promise.all(
|
|
|
|
contentTypesToAdd.map(uid => generateNewConfiguration(uid))
|
2019-07-22 16:24:44 +02:00
|
|
|
);
|
|
|
|
|
2019-07-23 10:54:44 +02:00
|
|
|
// update current schemas
|
|
|
|
await Promise.all(contentTypesToUpdate.map(uid => updateConfiguration(uid)));
|
|
|
|
}
|
2019-07-22 16:24:44 +02:00
|
|
|
|
2019-07-23 10:54:44 +02:00
|
|
|
/**
|
|
|
|
* sync groups schemas
|
|
|
|
*/
|
|
|
|
async function syncGroupsSchemas() {
|
|
|
|
const updateConfiguration = async uid => {
|
|
|
|
const conf = configurations.find(conf => conf.uid === uid);
|
|
|
|
const model = strapi.groups[uid];
|
|
|
|
return service.setConfiguration(uid, await syncConfiguration(conf, model));
|
|
|
|
};
|
2019-07-22 16:24:44 +02:00
|
|
|
|
2019-07-23 10:54:44 +02:00
|
|
|
const generateNewConfiguration = async uid => {
|
|
|
|
return service.setConfiguration(
|
|
|
|
uid,
|
|
|
|
await createDefaultConfiguration(strapi.groups[uid])
|
2019-07-22 16:24:44 +02:00
|
|
|
);
|
2019-07-23 10:54:44 +02:00
|
|
|
};
|
2019-07-22 16:24:44 +02:00
|
|
|
|
2019-07-23 10:54:44 +02:00
|
|
|
const service = strapi.plugins['content-manager'].services.groups;
|
2019-07-22 16:24:44 +02:00
|
|
|
|
2019-07-23 10:54:44 +02:00
|
|
|
const configurations = await storeUtils.findByKey(
|
|
|
|
'plugin_content_manager_configuration_groups'
|
2019-07-22 16:24:44 +02:00
|
|
|
);
|
|
|
|
|
2019-07-23 10:54:44 +02:00
|
|
|
const realUIDs = Object.keys(strapi.groups);
|
|
|
|
const DBUIDs = configurations.map(({ uid }) => uid);
|
|
|
|
const groupsToUpdate = _.intersection(realUIDs, DBUIDs);
|
|
|
|
const groupsToAdd = _.difference(realUIDs, DBUIDs);
|
|
|
|
const groupsToDelete = _.difference(DBUIDs, realUIDs);
|
2019-07-22 12:15:54 +02:00
|
|
|
|
2019-07-23 10:54:44 +02:00
|
|
|
// delette old schemas
|
|
|
|
await Promise.all(
|
|
|
|
groupsToDelete.map(uid => service.deleteConfiguration(uid))
|
2019-07-22 14:36:46 +02:00
|
|
|
);
|
|
|
|
|
2019-07-23 10:54:44 +02:00
|
|
|
// create new schemas
|
|
|
|
await Promise.all(groupsToAdd.map(uid => generateNewConfiguration(uid)));
|
2019-07-22 12:15:54 +02:00
|
|
|
|
2019-07-23 10:54:44 +02:00
|
|
|
// update current schemas
|
|
|
|
await Promise.all(groupsToUpdate.map(uid => updateConfiguration(uid)));
|
2019-07-22 09:56:25 +02:00
|
|
|
}
|