mirror of
https://github.com/strapi/strapi.git
synced 2025-12-24 21:54:24 +00:00
Update content types when groups are updated
This commit is contained in:
parent
76daff3284
commit
89f07bf37a
@ -31,7 +31,10 @@
|
||||
},
|
||||
"enum": {
|
||||
"type": "enumeration",
|
||||
"enum": ["morning,", "noon"]
|
||||
"enum": [
|
||||
"morning,",
|
||||
"noon"
|
||||
]
|
||||
},
|
||||
"bool": {
|
||||
"type": "boolean"
|
||||
@ -76,4 +79,4 @@
|
||||
"max": 10
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -80,7 +80,7 @@ module.exports = {
|
||||
|
||||
const [formatedAttributes, attributesErrors] = Service.formatAttributes(
|
||||
attributes,
|
||||
name
|
||||
name.toLowerCase()
|
||||
);
|
||||
|
||||
if (!_.isEmpty(attributesErrors)) {
|
||||
@ -183,7 +183,7 @@ module.exports = {
|
||||
|
||||
const [formatedAttributes, attributesErrors] = Service.formatAttributes(
|
||||
attributes,
|
||||
name,
|
||||
name.toLowerCase(),
|
||||
plugin
|
||||
);
|
||||
|
||||
|
||||
@ -78,8 +78,12 @@ module.exports = {
|
||||
return ctx.send({ error: 'group.alreadyExists' }, 400);
|
||||
}
|
||||
|
||||
strapi.reload.isWatching = false;
|
||||
|
||||
const newGroup = await service.createGroup(uid, body);
|
||||
|
||||
strapi.reload();
|
||||
|
||||
ctx.send({ data: newGroup }, 201);
|
||||
},
|
||||
|
||||
@ -106,12 +110,13 @@ module.exports = {
|
||||
}
|
||||
|
||||
// disable file watcher
|
||||
strapi.reload.isWatching = false;
|
||||
|
||||
const updatedGroup = await service.updateGroup(group, body);
|
||||
// await service.updateContentTypes();
|
||||
await service.updateGroupInModels(group.uid, updatedGroup.uid);
|
||||
|
||||
// re-enable file watch
|
||||
// reload
|
||||
strapi.reload();
|
||||
|
||||
ctx.send({ data: updatedGroup }, 200);
|
||||
},
|
||||
@ -132,12 +137,13 @@ module.exports = {
|
||||
}
|
||||
|
||||
// disable file watcher
|
||||
strapi.reload.isWatching = false;
|
||||
|
||||
await service.deleteGroup(group);
|
||||
// await service.updateContentTypes();
|
||||
await service.deleteGroupInModels(group.uid);
|
||||
|
||||
// re-enable file watch
|
||||
// reload
|
||||
strapi.reload();
|
||||
|
||||
ctx.send({ data: { uid } }, 200);
|
||||
},
|
||||
|
||||
@ -209,22 +209,16 @@ const createGroupUID = str => slugify(str, { separator: '_' });
|
||||
*/
|
||||
async function deleteGroup(group) {
|
||||
await deleteSchema(group.uid);
|
||||
process.nextTick(() => strapi.reload());
|
||||
}
|
||||
|
||||
/**
|
||||
* Writes a group schema file
|
||||
*/
|
||||
async function writeSchema(uid, schema) {
|
||||
strapi.reload.isWatching = false;
|
||||
|
||||
await strapi.fs.writeAppFile(
|
||||
`groups/${uid}.json`,
|
||||
JSON.stringify(schema, null, 2)
|
||||
);
|
||||
|
||||
strapi.reload.isWatching = true;
|
||||
process.nextTick(() => strapi.reload());
|
||||
}
|
||||
|
||||
/**
|
||||
@ -232,11 +226,107 @@ async function writeSchema(uid, schema) {
|
||||
* @param {string} ui
|
||||
*/
|
||||
async function deleteSchema(uid) {
|
||||
strapi.reload.isWatching = false;
|
||||
await strapi.fs.removeAppFile(`groups/${uid}.json`);
|
||||
strapi.reload.isWatching = true;
|
||||
}
|
||||
|
||||
const updateGroupInModels = (oldUID, newUID) => {
|
||||
const contentTypeService =
|
||||
strapi.plugins['content-type-builder'].services.contenttypebuilder;
|
||||
|
||||
const updateModels = (models, { plugin } = {}) => {
|
||||
Object.keys(models).forEach(modelKey => {
|
||||
const model = models[modelKey];
|
||||
|
||||
const attributesToModify = Object.keys(model.attributes).reduce(
|
||||
(acc, key) => {
|
||||
if (
|
||||
model.attributes[key].type === 'group' &&
|
||||
model.attributes[key].group === oldUID
|
||||
) {
|
||||
acc.push(key);
|
||||
}
|
||||
|
||||
return acc;
|
||||
},
|
||||
[]
|
||||
);
|
||||
|
||||
if (attributesToModify.length > 0) {
|
||||
const modelJSON = contentTypeService.readModel(modelKey, {
|
||||
plugin,
|
||||
api: model.apiName,
|
||||
});
|
||||
|
||||
attributesToModify.forEach(key => {
|
||||
modelJSON.attributes[key].group = newUID;
|
||||
});
|
||||
|
||||
contentTypeService.writeModel(modelKey, modelJSON, {
|
||||
plugin,
|
||||
api: model.apiName,
|
||||
});
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
updateModels(strapi.models);
|
||||
|
||||
Object.keys(strapi.plugins).forEach(pluginKey => {
|
||||
updateModels(strapi.plugins[pluginKey].models, { plugin: pluginKey });
|
||||
});
|
||||
|
||||
// add strapi.groups or strapi.admin if necessary
|
||||
};
|
||||
|
||||
const deleteGroupInModels = groupUID => {
|
||||
const contentTypeService =
|
||||
strapi.plugins['content-type-builder'].services.contenttypebuilder;
|
||||
|
||||
const updateModels = (models, { plugin } = {}) => {
|
||||
Object.keys(models).forEach(modelKey => {
|
||||
const model = models[modelKey];
|
||||
|
||||
const attributesToDelete = Object.keys(model.attributes).reduce(
|
||||
(acc, key) => {
|
||||
if (
|
||||
model.attributes[key].type === 'group' &&
|
||||
model.attributes[key].group === groupUID
|
||||
) {
|
||||
acc.push(key);
|
||||
}
|
||||
|
||||
return acc;
|
||||
},
|
||||
[]
|
||||
);
|
||||
|
||||
if (attributesToDelete.length > 0) {
|
||||
const modelJSON = contentTypeService.readModel(modelKey, {
|
||||
plugin,
|
||||
api: model.apiName,
|
||||
});
|
||||
|
||||
attributesToDelete.forEach(key => {
|
||||
delete modelJSON.attributes[key];
|
||||
});
|
||||
|
||||
contentTypeService.writeModel(modelKey, modelJSON, {
|
||||
plugin,
|
||||
api: model.apiName,
|
||||
});
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
updateModels(strapi.models);
|
||||
|
||||
Object.keys(strapi.plugins).forEach(pluginKey => {
|
||||
updateModels(strapi.plugins[pluginKey].models, { plugin: pluginKey });
|
||||
});
|
||||
|
||||
// add strapi.groups or strapi.admin if necessary
|
||||
};
|
||||
|
||||
module.exports = {
|
||||
getGroups,
|
||||
getGroup,
|
||||
@ -247,4 +337,7 @@ module.exports = {
|
||||
|
||||
// export for testing only
|
||||
createSchema,
|
||||
|
||||
deleteGroupInModels,
|
||||
updateGroupInModels,
|
||||
};
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user