I am trying to achieve something that seems pretty standard to me but I can’t find a way to achieve it.
I want to send an email everytime a content is added from the admin panel. I tried to override the content controller, but I noticed that content added from the admin do not trigger controllers.
afterCreate will be triggered at the first draft creation.
If you want to trigger a function only when it’s published then you need to use beforeUpdate and compare the previous state of published_at and the current state. If the previous state is null and the current one contains the date, then call your function.
async beforeUpdate(params, data) {
if (data.published_at != null) {
const { id } = data;
const previousData = await strapi.services.articles.findOne({ id });
const previousPublishedAt = previousData.published_at;
const currentPublished_at = data.published_at;
if (currentPublished_at != previousPublishedAt) {
console.log('Triggered only when published.');
}
}
},
So everytime a controller is run, at least one lifecycle is also run right? That’s why I don’t really understand if I should write my function in a controller or lifecycle function.