How to trigger a notification when adding a document to a collection?

System Information
  • Strapi Version: 3.3.3
  • Database: MongoDB

Hello!

I made a notification component to my Users collection, and I want to create an automatic notification system during some events.

One of them is adding document to the User collection from the admin in the back-office.

To do this, i try to get this information in the beforeUpdate lifecycle of the user model, but Strapi sends all the user’s datas, not just the modified one…

Someone has an idea how to do this in a simple way.

Today, i just see two solutions: get the actual user data in the hooks and compare them, or create a plugin to upload document to the user and create a notification in the same way. But both are heavy solutions…

Thanks for your ideas :wink:

1 Like

Did you get any progress on this?

I am trying with this:

module.exports = {
  lifecycles: {
    async afterCreate(result, data) {
      try {
        await strapi.services.profile.create({
          user: result.id,
        });
        await strapi
          .query("user", "users-permissions")
          .update({ id: result.id }, { confirmed: false });
        await strapi.notifications.toggle({
          type: "info",
          title: { id: "app.new.user.registered.notification.title" },
          message: {
            id: "app.new.user.registered.notification.message",
            values: { email: result.email },
          },
          link: {
            url: `${strapi.backendURL}/admin/plugins/content-manager/collectionType/plugins::users-permissions.user/${result.id}`,
            label: {
              id: "notification.see.more.link",
            },
          },
        });
      } catch (error) {
        console.log("Error after registering new user: ", error);
      }
    },
};

But it produces the following error:

Error after registering new user:  TypeError: Cannot read property 'toggle' of undefined
    at Object.afterCreate (/srv/app/extensions/users-permissions/models/User.js:13:36)
    at runMicrotasks (<anonymous>)
    at processTicksAndRejections (internal/process/task_queues.js:97:5)
    at async executeLifecycle (/srv/app/node_modules/strapi-database/lib/utils/lifecycles.js:11:5)
    at async Object.create (/srv/app/node_modules/strapi-database/lib/queries/helpers.js:18:3)
    at async Object.register (/srv/app/node_modules/strapi-plugin-users-permissions/controllers/Auth.js:492:20)
    at async resolver (/srv/app/node_modules/strapi-plugin-users-permissions/config/schema.graphql.js:195:11)
1 Like

I’d also like to know this, but I found it impossible to get any info on how to show a simple alert in the admin panel…

1 Like

Did you try to get information on the discord channel?

The last time I asked, they told me there is a internal core API, but there is not available documentation about it.

It’s already some time, since this question was asked, but maybe it will help someone in the future: Error handling | Strapi Documentation