Lifecycles are functions triggered on data manipulation (from Admin UI and from API)
Controllers are functions that you attach to your routes (Only from API).
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.');
}
}
},