Rename content manager configuration key an model uid changes

This commit is contained in:
Alexandre Bodin 2019-07-26 17:53:03 +02:00
parent 1d09999586
commit 59523d47e7
6 changed files with 83 additions and 12 deletions

View File

@ -56,8 +56,8 @@
"collection": "tag"
},
"manyTags": {
"dominant": true,
"collection": "tag",
"dominant": true,
"via": "linkedArticles"
},
"fb_cta": {
@ -79,4 +79,4 @@
"max": 10
}
}
}
}

View File

@ -105,4 +105,15 @@ module.exports = {
);
return getModel(uid);
},
async updateUID({ oldUID, newUID, source }) {
const oldKey = uidToStoreKey({ uid: oldUID, source });
const newKey = uidToStoreKey({ uid: newUID, source });
await storeUtils.setModelConfiguration(oldKey, {
uid: oldUID,
});
return storeUtils.moveKey(oldKey, newKey);
},
};

View File

@ -29,4 +29,16 @@ module.exports = {
const storeKey = uidToStoreKey(uid);
return storeUtils.deleteKey(storeKey);
},
async updateUID(oldUID, newUID) {
const oldKey = uidToStoreKey(oldUID);
const newKey = uidToStoreKey(newUID);
await storeUtils.setModelConfiguration(oldKey, {
uid: oldUID,
isGroup: true,
});
return storeUtils.moveKey(oldKey, newKey);
},
};

View File

@ -89,6 +89,16 @@ function findByKeyQuery({ model }, key) {
}
const findByKey = key => strapi.query('core_store').custom(findByKeyQuery)(key);
const moveKey = (oldKey, newKey) => {
return strapi.query('core_store').update(
{
key: `plugin_content_manager_configuration_${oldKey}`,
},
{
key: `plugin_content_manager_configuration_${newKey}`,
}
);
};
const getAllConfigurations = () =>
findByKey('plugin_content_manager_configuration');
@ -103,5 +113,6 @@ module.exports = {
setModelConfiguration,
deleteKey,
moveKey,
keys,
};

View File

@ -220,8 +220,8 @@ module.exports = {
])
);
modelJSON.connection = connection;
modelJSON.collectionName = collectionName;
modelJSON.connection = connection || modelJSON.connection;
modelJSON.collectionName = collectionName || modelJSON.collectionName;
modelJSON.info = {
name,
description: _description,
@ -256,6 +256,20 @@ module.exports = {
if (!_.isEmpty(removeModelErrors)) {
return ctx.badRequest(null, [{ messages: removeModelErrors }]);
}
if (
_.has(strapi.plugins, ['content-manager', 'services', 'contenttypes'])
) {
await _.get(strapi.plugins, [
'content-manager',
'services',
'contenttypes',
]).updateUID({
oldUID: model,
newUID: name.toLowerCase(),
source: plugin,
});
}
}
try {

View File

@ -116,15 +116,34 @@ async function createGroup(uid, infos) {
* @param {Object} infos
*/
async function updateGroup(group, infos) {
const { uid } = group;
const { uid, schema: oldSchema } = group;
const newUid = createGroupUID(infos.name);
if (uid !== newUid) {
// don't update collectionName if not provided
const updatedSchema = {
info: {
name: infos.name || oldSchema.name,
description: infos.description || oldSchema.description,
},
connection: infos.connection || oldSchema.connection,
collectionName: infos.collectionName || oldSchema.collectionName,
attributes: convertAttributes(infos.attributes),
};
const newUID = createGroupUID(infos.name);
if (uid !== newUID) {
await deleteSchema(uid);
return createGroup(newUid, infos);
}
const updatedSchema = { ...group.schema, ...createSchema(uid, infos) };
if (_.has(strapi.plugins, ['content-manager', 'services', 'groups'])) {
await _.get(strapi.plugins, [
'content-manager',
'services',
'groups',
]).updateUID(uid, newUID);
}
await writeSchema(newUID, updatedSchema);
return { uid: newUID };
}
await writeSchema(uid, updatedSchema);
return { uid };
@ -137,7 +156,11 @@ async function updateGroup(group, infos) {
const createSchema = (uid, infos) => {
const {
name,
connection = 'default',
connection = _.get(
strapi,
['config', 'currentEnvironment', 'database', 'defaultConnection'],
'default'
),
description = '',
collectionName,
attributes,
@ -149,7 +172,7 @@ const createSchema = (uid, infos) => {
description,
},
connection,
collectionName: collectionName || `groups_${pluralize(uid)}`,
collectionName: collectionName || `groups_${pluralize(uid).toLowerCase()}`,
attributes: convertAttributes(attributes),
};
};