fix: réparer la cascade de renommage des slugs d'artiste

This commit is contained in:
2026-07-04 11:37:20 +04:00
parent 85ecdf3072
commit c66c972a93
2 changed files with 82 additions and 10 deletions
@@ -0,0 +1,66 @@
import {describe, it, expect, vi, afterEach} from 'vitest'
async function loadLifecycles(strapiMock) {
vi.resetModules()
global.strapi = strapiMock
const mod = await import('../lifecycles.js')
return mod
}
describe('artiste afterUpdate — cascade de renommage des slugs', () => {
afterEach(() => {
delete global.strapi
})
it("renomme le slug des paroles de l'artiste quand son alias change", async () => {
const artisteFindOne = vi.fn(async () => ({
id: 5,
paroles: [{id: 10}, {id: 11}]
}))
const paroleFindMany = vi.fn(async () => [
{id: 10, titre: 'Titre 1', slug: 'ancien-alias-titre-1', artistes: [{alias: 'nouvel-alias'}]},
{id: 11, titre: 'Titre 2', slug: 'nouvel-alias-titre-2', artistes: [{alias: 'nouvel-alias'}]}
])
const paroleUpdate = vi.fn(async () => {})
const strapiMock = {
db: {
query: vi.fn(uid => {
if (uid === 'api::artiste.artiste') return {findOne: artisteFindOne}
if (uid === 'api::parole.parole') return {findMany: paroleFindMany, update: paroleUpdate}
throw new Error(`unexpected uid: ${uid}`)
})
}
}
const {afterUpdate} = await loadLifecycles(strapiMock)
await afterUpdate({result: {id: 5}})
expect(artisteFindOne).toHaveBeenCalledWith({where: {id: 5}, populate: ['paroles']})
expect(paroleFindMany).toHaveBeenCalledWith({where: {id: {$in: [10, 11]}}, populate: ['artistes']})
expect(paroleUpdate).toHaveBeenCalledTimes(1)
expect(paroleUpdate).toHaveBeenCalledWith({where: {id: 10}, data: {slug: 'nouvel-alias-titre-1'}})
})
it("ne fait rien quand l'artiste n'a aucune parole", async () => {
const artisteFindOne = vi.fn(async () => ({id: 5, paroles: []}))
const paroleFindMany = vi.fn()
const strapiMock = {
db: {
query: vi.fn(uid => {
if (uid === 'api::artiste.artiste') return {findOne: artisteFindOne}
if (uid === 'api::parole.parole') return {findMany: paroleFindMany}
throw new Error(`unexpected uid: ${uid}`)
})
}
}
const {afterUpdate} = await loadLifecycles(strapiMock)
await afterUpdate({result: {id: 5}})
expect(paroleFindMany).not.toHaveBeenCalled()
})
})
@@ -3,13 +3,19 @@
const { ApplicationError } = require("@strapi/utils").errors const { ApplicationError } = require("@strapi/utils").errors
const slugify = require('slugify') const slugify = require('slugify')
const jwennTeksEpiId = async data => { const jwennTeksEpiId = async ids => {
const paroles = await strapi.db.query('api::parole.parole').find({id_in: data}) const paroles = await strapi.db.query('api::parole.parole').findMany({
where: {id: {$in: ids}},
populate: ['artistes']
})
return paroles return paroles
} }
const jwennAwtisEpiId = async id => { const jwennAwtisEpiId = async id => {
const artiste = await strapi.db.query('api::artiste.artiste').findOne({id}) const artiste = await strapi.db.query('api::artiste.artiste').findOne({
where: {id},
populate: ['paroles']
})
return artiste return artiste
} }
@@ -39,15 +45,15 @@ module.exports = {
data.slug = slugify(data.alias, {lower: true, remove: /[*#+~.()'"!:@]/g}) data.slug = slugify(data.alias, {lower: true, remove: /[*#+~.()'"!:@]/g})
}, },
afterUpdate: async event => { afterUpdate: async event => {
let {data} = event.params const {result} = event
const {id} = data const artiste = await jwennAwtisEpiId(result.id)
const artiste = await jwennAwtisEpiId(id)
if (artiste.paroles && artiste.paroles.length >= 1) { if (artiste.paroles && artiste.paroles.length >= 1) {
const paroles = await jwennTeksEpiId(artiste.paroles) const paroleIds = artiste.paroles.map(({id}) => id)
Promise.all(paroles.map(async t => { const paroles = await jwennTeksEpiId(paroleIds)
const {id, titre, slug, artiste} = t await Promise.all(paroles.map(async t => {
const alias = artiste.map(a => a.alias).join('-') const {id, titre, slug, artistes} = t
const alias = artistes.map(a => a.alias).join('-')
const slugUpdated = slugify(`${alias}-${titre}`, {lower: true, remove: /[*#+~.()'"!:@]/g}) const slugUpdated = slugify(`${alias}-${titre}`, {lower: true, remove: /[*#+~.()'"!:@]/g})
if (slug !== slugUpdated) { if (slug !== slugUpdated) {