2020-12-06 22:13:45 +01:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Read the documentation (https://strapi.io/documentation/v3.x/concepts/models.html#lifecycle-hooks)
|
|
|
|
|
* to customize this model
|
|
|
|
|
*/
|
|
|
|
|
|
2021-05-23 21:35:51 +02:00
|
|
|
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}
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
}))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|