Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/trigger webhook on bulk delete #14041

Merged
merged 13 commits into from Aug 11, 2022
Merged

Conversation

Marc-Roig
Copy link
Contributor

@Marc-Roig Marc-Roig commented Aug 9, 2022

πŸ‘‹πŸ» What does it do?

  • Webhook was not triggered when doing a bulk delete.

❓ Why is it needed?

It was triggered when deleting a single entity but not for a bulk delete.

πŸ§ͺ How to test it?

  1. Create your webhook. ( example )
    You can use this site to test the webhook https://webhook.site/
  2. Delete a single entity from any content type.
  3. Webhook (entry.delete) should have been triggered
  4. Bulk delete entities from any content type
  5. One webhook (entry.delete) should have been triggered for each entity deleted.

πŸ”— Related issue(s)/PR(s)

#12290

@Marc-Roig Marc-Roig added source: core:strapi Source is core/strapi package pr: fix This PR is fixing a bug labels Aug 9, 2022
@Marc-Roig Marc-Roig added this to the 4.3.3 milestone Aug 9, 2022
@Marc-Roig
Copy link
Contributor Author

About the components not being removed from the database, @derrickmehaffy mentions in the issue that we could modify the foreign key constrain to enable cascade delete.
@petersg83 we could have a discussion tomorrow about this.

@codecov
Copy link

codecov bot commented Aug 9, 2022

Codecov Report

Merging #14041 (5fe6702) into master (ccc854d) will increase coverage by 0.11%.
The diff coverage is 46.66%.

❗ Current head 5fe6702 differs from pull request most recent head dad7237. Consider uploading reports for the commit dad7237 to get more accurate results

@@            Coverage Diff             @@
##           master   #14041      +/-   ##
==========================================
+ Coverage   55.35%   55.46%   +0.11%     
==========================================
  Files        1258     1258              
  Lines       31699    31708       +9     
  Branches     5734     5736       +2     
==========================================
+ Hits        17546    17588      +42     
+ Misses      12337    12307      -30     
+ Partials     1816     1813       -3     
Flag Coverage Ξ”
front 58.10% <ΓΈ> (ΓΈ)
unit 49.01% <46.66%> (+0.40%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Ξ”
.../content-manager/server/services/entity-manager.js 31.19% <0.00%> (ΓΈ)
packages/core/database/lib/query/query-builder.js 1.89% <0.00%> (-0.03%) ⬇️
...e/upload/admin/src/components/AssetDialog/index.js 74.69% <ΓΈ> (ΓΈ)
...s/core/strapi/lib/services/entity-service/index.js 63.12% <85.71%> (+22.37%) ⬆️
...ges/core/admin/server/services/project-settings.js 93.22% <100.00%> (ΓΈ)
...core/strapi/lib/services/entity-validator/index.js 51.40% <0.00%> (+1.86%) ⬆️
...e/strapi/lib/services/entity-service/components.js 20.00% <0.00%> (+4.13%) ⬆️

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

@alexandrebodin alexandrebodin removed this from the 4.3.3 milestone Aug 10, 2022
await deleteComponents(uid, entityToDelete);
await db.query(uid).delete({ where: { id: entityToDelete.id } });
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did you change the order? It was not working before?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

deleteComponents was querying for the uid entity:

// In deleteComponents:
      const value = await strapi.query(uid).load(entityToDelete, attributeName);

the entity was already deleted at the time deleteComponents was executed.

return db.query(uid).deleteMany(query);
const entitiesToDelete = await db.query(uid).findMany({
...query,
});
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about:

    const entitiesToDelete = await db.query(uid).findMany(query);

?

@@ -161,6 +161,13 @@ 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({});
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's see what the fun test run says but it may not work as the test database may have data left from the other tests

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't the database pruned in each test file?
Maybe I can add a query in the findMany referring to the component added in this test file then.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure, it would be nice to check and know. Js wrote the code

Copy link
Contributor Author

@Marc-Roig Marc-Roig Aug 11, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will add the query just in case then. And also check if it works first.

@petersg83
Copy link
Contributor

Very nice!
Let's talk about the cascade delete, that may be an option. What do you think to separate this in 2 PR? One for the webhook and one for the component deletion in DB?

@Marc-Roig
Copy link
Contributor Author

et's talk about the cascade delete, that may be an option. What do you think to separate this in 2 PR? One for the webhook and one for the component deletion in DB?

Sure! What do you think if we leave the deleteComponents reorder & test as a temporary fix, and we work on the cascade delete in another PR?

@Marc-Roig
Copy link
Contributor Author

Marc-Roig commented Aug 11, 2022

@petersg83
I see an e2e failing with my current change: packages/core/strapi/tests/lifecycles/before-delete.test.e2e.js Exactly at line 94.

From there, I saw a PR made by @stafyniaksacha some months ago: #12653

He changed the order of deletion (between component and entity) to allow developers to query the entity being deleted including the components on the beforeDelete hook.

So I think I will leave things as they were, and maybe we can have this in mind when planning the cascade delete.

Note: props to @stafyniaksacha for the e2e tests that made me realize this :)

@Marc-Roig Marc-Roig linked an issue Aug 11, 2022 that may be closed by this pull request
Copy link
Contributor

@petersg83 petersg83 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM :) well done

@Marc-Roig Marc-Roig added this to the 4.3.5 milestone Aug 11, 2022
@Marc-Roig Marc-Roig merged commit 24d3417 into master Aug 11, 2022
@Marc-Roig Marc-Roig deleted the fix/trigger-webhook-on-bulk-delete branch August 11, 2022 14:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pr: fix This PR is fixing a bug source: core:strapi Source is core/strapi package
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Webhook not triggered on bulkDelete
3 participants