Update slug after updating awtis

This commit is contained in:
Cédric FAMIBELLE-PRONZOLA
2021-05-23 21:35:51 +02:00
parent 883d0fa108
commit 9fec279169
+28 -1
View File
@@ -5,4 +5,31 @@
* to customize this model * to customize this model
*/ */
module.exports = {}; const slugify = require('slugify')
const jwennTeksEpiId = async data => {
const teks = await strapi.query('teks').find({id_in: data})
return teks
}
module.exports = {
lifecycles: {
afterUpdate: async (params, data) => {
const {values} = data
if (values.teks && values.teks.length >= 1) {
const teks = await jwennTeksEpiId(values.teks)
Promise.all(teks.map(async t => {
const {_id, tit, slug, awtis} = t
const alias = awtis.map(a => a.alias).join('-')
const slugUpdated = slugify(`${alias}-${tit}`, {lower: true, remove: /[*#+~.()'"!:@]/g})
if (slug !== slugUpdated) {
await strapi.query('teks').update(
{_id},
{slug: slugUpdated}
)
}
}))
}
}
}
}