Override controller / services not working in Admin Panel V4

System Information
  • Strapi Version: 4.0.2
  • Operating System: Mac OS 12.0.1
  • Database: PostgreSQL 12.2
  • Node Version: 16.13.1
  • NPM Version: 8.1.2
  • Yarn Version: 1.22.17

Hi, when I override a v4 service/controller it works as expected when called with REST/GraphQL api but doesn’t call when I modify the data from Admin Panel.

src/api/test/controllers/test.js

'use strict';
const { createCoreController } = require('@strapi/strapi').factories;

module.exports = createCoreController('api::test.test', ({ strapi, ...rest }) => ({
    async update(ctx) {
        // some logic here
        const response = await super.update(ctx);

        console.log({ response })
        // some more logic

        return response;
    }
}
));

From my understanding of what I read of V3, it has something to do with Admin Panel calls custom content-manager services.

Is there a way a controller/service override can apply to both? REST/GraphQL Client & Admin Panel Content Manager?

5 Likes

Did you ever find a solution?

Any update here?

Still nothing?

@xantiagoma So finally I found the way to do it for me. I am not sure the best aproach,but you can do something inside of test/content-types/test/, create a lifecycles.js doc and create a this funcions

module.exports = {

  async afterUpdate(event) {
    // some logic here
    if (
      //compare you new data with old  to avoid infinite loop
      ) {

      await strapi.db.query('api::test.test').update({
        where: { id: event.params.where.id },
        data: {
          ...data,
           // some logic here
        },
      });
    }
  },
}

hope it helps

Hmm this is not ideal. Controller update should work. Create gets called from admin panel queries but not update which is strange. Update gets called when using the rest api. So it doesnt make sense.

I dont like using the lifecycle for this because in my case the logic should be in the controller.