51 lines
1.4 KiB
JavaScript
51 lines
1.4 KiB
JavaScript
'use strict';
|
|
|
|
/**
|
|
* Read the documentation (https://strapi.io/documentation/v3.x/concepts/models.html#lifecycle-hooks)
|
|
* to customize this model
|
|
*/
|
|
|
|
const slugify = require('slugify')
|
|
|
|
const jwennTeksEpiId = async data => {
|
|
const teks = await strapi.query('teks').find({id_in: data})
|
|
return teks
|
|
}
|
|
|
|
const jwennAwtisEpiId = async id => {
|
|
const awtis = await strapi.query('awtis').find({id})
|
|
return awtis
|
|
}
|
|
|
|
module.exports = {
|
|
lifecycles: {
|
|
beforeUpdate: async (params, data) => {
|
|
if (!data.slug) {
|
|
data.slug = slugify(data.alias, {lower: true, remove: /[*#+~.()'"!:@]/g})
|
|
}
|
|
},
|
|
beforeCreate: async data => {
|
|
data.slug = slugify(data.alias, {lower: true, remove: /[*#+~.()'"!:@]/g})
|
|
},
|
|
afterUpdate: async (params, data) => {
|
|
const {id} = data
|
|
const awtis = await jwennAwtisEpiId(id)
|
|
|
|
if (awtis.teks && awtis.teks.length >= 1) {
|
|
const teks = await jwennTeksEpiId(awtis.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}
|
|
)
|
|
}
|
|
}))
|
|
}
|
|
}
|
|
}
|
|
}
|