Nathan Pichon e4e2a310e2
fix(review-workflows): display review workflows informations in content-manager when needed (#16919)
* chore: update example schemas

* fix(content-manager): merge data instead of overwrite nested objects

* feat(content-manager): add options object in the configuration object

* feat(review-workflow): add the review workflows option in the configuration of content-manager

* fix(content-manager): use review workflows option value in edit-view

* test(workflows): update mocks

* test(info-box): update mocks

* chore: add comments for utils in content-manager

* test(content-manager): add tests on content-types service

* fix(review-workflows): merge configuration before updating it

* docs(review-workflows): add comments about why we merge configuration of CTs

* refactor(review-workflows): extract updateContentTypeConfig function

* test(workflows): update mock

* fix(review-workflows): remove infinite loop

* fix(review-workflows): use findConfiguration instead of getConfiguration

* test(review-workflows): change plural name of long content type

* test(review-workflows): skip long ct name test
2023-06-12 10:52:52 +02:00

81 lines
2.3 KiB
JavaScript

'use strict';
const { intersection, difference } = require('lodash');
const { createDefaultConfiguration, syncConfiguration } = require('./utils/configuration');
module.exports = ({ isComponent, prefix, storeUtils, getModels }) => {
const uidToStoreKey = (uid) => {
return `${prefix}::${uid}`;
};
const getConfiguration = (uid) => {
const storeKey = uidToStoreKey(uid);
return storeUtils.getModelConfiguration(storeKey);
};
const setConfiguration = (uid, input) => {
const { settings, metadatas, layouts, options } = input;
const configuration = {
uid,
settings,
metadatas,
layouts,
...(options ? { options } : {}),
};
if (isComponent) {
configuration.isComponent = isComponent;
}
const storeKey = uidToStoreKey(uid);
return storeUtils.setModelConfiguration(storeKey, configuration);
};
const deleteConfiguration = (uid) => {
const storeKey = uidToStoreKey(uid);
return storeUtils.deleteKey(storeKey);
};
const syncConfigurations = async () => {
const models = getModels();
const configurations = await storeUtils.findByKey(
`plugin_content_manager_configuration_${prefix}`
);
const updateConfiguration = async (uid) => {
const conf = configurations.find((conf) => conf.uid === uid);
return setConfiguration(uid, await syncConfiguration(conf, models[uid]));
};
const generateNewConfiguration = async (uid) => {
return setConfiguration(uid, await createDefaultConfiguration(models[uid]));
};
const currentUIDS = Object.keys(models);
const DBUIDs = configurations.map(({ uid }) => uid);
const contentTypesToUpdate = intersection(currentUIDS, DBUIDs);
const contentTypesToAdd = difference(currentUIDS, DBUIDs);
const contentTypesToDelete = difference(DBUIDs, currentUIDS);
// delete old schemas
await Promise.all(contentTypesToDelete.map((uid) => deleteConfiguration(uid)));
// create new schemas
await Promise.all(contentTypesToAdd.map((uid) => generateNewConfiguration(uid)));
// update current schemas
await Promise.all(contentTypesToUpdate.map((uid) => updateConfiguration(uid)));
};
return {
getConfiguration,
setConfiguration,
deleteConfiguration,
syncConfigurations,
};
};