get list of components to delete them afterwards

This commit is contained in:
Marc-Roig 2022-08-18 17:19:18 +02:00
parent 840391f541
commit 3577fae4e4
3 changed files with 51 additions and 3 deletions

View File

@ -100,6 +100,34 @@ const createComponents = async (uid, data) => {
return componentBody;
};
/**
* @param {str} uid
* @param {*} entity
* @return {Array<{uid: string, data: *}>}
*/
const getComponents = async (uid, entity) => {
const { attributes } = strapi.getModel(uid);
const components = [];
for (const attributeName in attributes) {
const attribute = attributes[attributeName];
if (attribute.type === 'component' || attribute.type === 'dynamiczone') {
const value = await strapi.query(uid).load(entity, attributeName);
if (!value) continue;
_.castArray(value).forEach((component) => {
components.push({
uid: attribute.type === 'component' ? attribute.component : component.__component,
data: component,
});
});
}
}
return components;
};
/*
delete old components
create or update
@ -352,7 +380,9 @@ const deleteComponent = async (uid, componentToDelete) => {
module.exports = {
omitComponentData,
getComponents,
createComponents,
updateComponents,
deleteComponents,
deleteComponent,
};

View File

@ -14,9 +14,10 @@ const uploadFiles = require('../utils/upload-files');
const {
omitComponentData,
getComponents,
createComponents,
updateComponents,
deleteComponents,
deleteComponent,
} = require('./components');
const { transformParamsToQuery, pickSelectionParams } = require('./params');
const { applyTransforms } = require('./attributes');
@ -213,8 +214,10 @@ const createDefaultImplementation = ({ strapi, db, eventHub, entityValidator })
return null;
}
const componentsToDelete = await getComponents(uid, entityToDelete);
await db.query(uid).delete({ where: { id: entityToDelete.id } });
await deleteComponents(uid, entityToDelete);
await Promise.all(componentsToDelete.map((compo) => deleteComponent(compo.uid, compo.data)));
await this.emitEvent(uid, ENTRY_DELETE, entityToDelete);
@ -234,8 +237,14 @@ const createDefaultImplementation = ({ strapi, db, eventHub, entityValidator })
return null;
}
const componentsToDelete = await Promise.all(
entitiesToDelete.map((entityToDelete) => getComponents(uid, entityToDelete))
);
const deletedEntities = await db.query(uid).deleteMany(query);
await Promise.all(entitiesToDelete.map((entity) => deleteComponents(uid, entity)));
await Promise.all(
componentsToDelete.flat().map((compo) => deleteComponent(compo.uid, compo.data))
);
// Trigger webhooks. One for each entity
await Promise.all(entitiesToDelete.map((entity) => this.emitEvent(uid, ENTRY_DELETE, entity)));

View File

@ -158,6 +158,15 @@ describe('Core API - Basic + compo + draftAndPublish', () => {
data.productsWithCompoAndDP.shift();
});
describe('database state', () => {
test('components have been removed from the database', async () => {
const dbComponents = await strapi.db
.query('default.compo')
.findMany({ name: 'compo name updated' });
expect(dbComponents).toHaveLength(0);
});
});
describe('validation', () => {
test('Cannot create product with compo - compo required', async () => {
const product = {