115 lines
3.1 KiB
JavaScript
Raw Normal View History

'use strict';
const _ = require('lodash');
const storeUtils = require('../../services/utils/store');
const {
createDefaultConfiguration,
syncConfiguration,
} = require('../../services/utils/configuration');
2019-11-14 12:21:21 +01:00
const contentTypeService = require('../../services/ContentTypes');
const componentService = require('../../services/Components');
/**
* Synchronize content manager schemas
*/
2019-08-12 15:35:40 +02:00
module.exports = () => {
return syncSchemas();
};
async function syncSchemas() {
await syncContentTypesSchemas();
2019-10-22 18:01:03 +02:00
await syncComponentsSchemas();
}
/**
* Sync content types schemas
*/
async function syncContentTypesSchemas() {
const configurations = await storeUtils.findByKey(
'plugin_content_manager_configuration_content_types'
);
2019-11-14 12:21:21 +01:00
await updateContentTypes(configurations);
}
2019-11-14 12:21:21 +01:00
async function updateContentTypes(configurations) {
const updateConfiguration = async uid => {
const conf = configurations.find(conf => conf.uid === uid);
2019-11-14 12:21:21 +01:00
return contentTypeService.setConfiguration(
uid,
await syncConfiguration(conf, strapi.contentTypes[uid])
);
};
const generateNewConfiguration = async uid => {
2019-11-14 12:21:21 +01:00
return contentTypeService.setConfiguration(
uid,
await createDefaultConfiguration(strapi.contentTypes[uid])
);
};
2019-11-14 12:21:21 +01:00
const currentUIDS = Object.keys(strapi.contentTypes);
const DBUIDs = configurations.map(({ uid }) => uid);
2019-11-14 12:21:21 +01:00
const contentTypesToUpdate = _.intersection(currentUIDS, DBUIDs);
const contentTypesToAdd = _.difference(currentUIDS, DBUIDs);
const contentTypesToDelete = _.difference(DBUIDs, currentUIDS);
// delette old schemas
await Promise.all(
2019-11-14 12:21:21 +01:00
contentTypesToDelete.map(uid => contentTypeService.deleteConfiguration(uid))
);
// create new schemas
await Promise.all(
contentTypesToAdd.map(uid => generateNewConfiguration(uid))
);
// update current schemas
await Promise.all(contentTypesToUpdate.map(uid => updateConfiguration(uid)));
}
/**
2019-10-22 18:01:03 +02:00
* sync components schemas
*/
2019-10-22 18:01:03 +02:00
async function syncComponentsSchemas() {
const updateConfiguration = async uid => {
const conf = configurations.find(conf => conf.uid === uid);
2019-11-14 12:21:21 +01:00
return componentService.setConfiguration(
uid,
await syncConfiguration(conf, strapi.components[uid])
);
};
const generateNewConfiguration = async uid => {
2019-11-14 12:21:21 +01:00
return componentService.setConfiguration(
uid,
2019-10-22 18:01:03 +02:00
await createDefaultConfiguration(strapi.components[uid])
);
};
const configurations = await storeUtils.findByKey(
2019-10-22 18:01:03 +02:00
'plugin_content_manager_configuration_components'
);
2019-10-22 18:01:03 +02:00
const realUIDs = Object.keys(strapi.components);
const DBUIDs = configurations.map(({ uid }) => uid);
2019-10-22 18:01:03 +02:00
const componentsToUpdate = _.intersection(realUIDs, DBUIDs);
const componentsToAdd = _.difference(realUIDs, DBUIDs);
const componentsToDelete = _.difference(DBUIDs, realUIDs);
// delette old schemas
await Promise.all(
2019-11-14 12:21:21 +01:00
componentsToDelete.map(uid => componentService.deleteConfiguration(uid))
);
// create new schemas
2019-10-22 18:01:03 +02:00
await Promise.all(componentsToAdd.map(uid => generateNewConfiguration(uid)));
// update current schemas
2019-10-22 18:01:03 +02:00
await Promise.all(componentsToUpdate.map(uid => updateConfiguration(uid)));
}