Call controller from an external api's request AND / OR strapi's admin dashboard

System Information
  • Strapi Version: 3.1.3
  • Operating System: Windows 10
  • Database: MongoDB
  • Node Version: 12.18.2
  • NPM Version: 6.14.5
  • Yarn Version: 1.22.4

Hello,

My client would like to be able to administrate her orders, whether creating or updating them from Strapi’s dashboard. It seems like I can’t call controller in ‘api/orders/controller/order.js’ from admin’s dashboard, I can only from API.
I saw that model is called everytime in ‘api/orders/models/order.js’.

Where am I supposed to write my code for Stripe’s call (for example) etc ?
Should I do everything in the model’s ‘beforeCreate’ then ?
And why is model working everywhere and not controller ??

Hello, can you please provide more details? As I couldn’t understand what exactly are you trying to achieve

Hello @sunnyson

I’m building an e-commerce website for a client.
She would like to be able to create or update orders she receives, and I would like to create orders from the website.
But there are conditions about these orders, like the connection to Stripe for example, the calculous of the amount (which I put in the model already)…

So that’s it, I want to call a controller from my API (it’s already the case, it gets called) AND from Strapi’s admin dashboard (my client, and it’s easier to test for me also, but controller isn’t called)

// api/order/controllers/order.js
// This gets called from my API, but not from admin's dashboard, while model gets called for both
module.exports = {
  create: async ctx => {
      console.log('Creating...');
      const order = await strapi.services.order.create(ctx.request.body);
      return sanitizeEntity(order, { model: strapi.models.order });
  },
};

Don’t hesitate to tell me if I’m not clear, maybe I can explain another way :thinking:

@sunnyson Do you have any idea ? Or anyone

Hello, sorry for late reply,

You can use lifecycles for this to trigger your create service from Strapi’s admin.

module.exports = {
  lifecycles: {
    // Called before an entry is created
    beforeCreate(data) {
   //trigger your custom code here
    },
    // Called after an entry is created
    afterCreate(result) {
   },
  },
};

Hello no problem @sunnyson thank you answering !!

Okay so I have to do it in the model file, good to know. Thanks !

1 Like

Yes, write your custom code in a custom service and trigger it inside the lifecycle.

Bad news @sunnyson

I have the same problem as in controller, this code isn’t called in service in ‘api/order/services/order.js’, when I create an order from Strapi’s admin dashboard:

module.exports = {
  create: async (data, files = {}) => {
    console.log('creating in service')
    const entry = await strapi.query('order').create(data);
    return entry;
  }
};

What should I do ?