From f781d52d1fb6f931312656acc0dfd4c2d658d082 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20FAMIBELLE-PRONZOLA?= Date: Tue, 28 Apr 2026 12:31:19 +0400 Subject: [PATCH] fix: adapt parole beforeCreate method --- .../parole/content-types/parole/lifecycles.js | 61 +++++++++---------- 1 file changed, 28 insertions(+), 33 deletions(-) diff --git a/src/api/parole/content-types/parole/lifecycles.js b/src/api/parole/content-types/parole/lifecycles.js index 181224e..77d69b4 100644 --- a/src/api/parole/content-types/parole/lifecycles.js +++ b/src/api/parole/content-types/parole/lifecycles.js @@ -31,7 +31,7 @@ const isSlugExists = async existingSlug => { } const jwennAwtisEpiId = async artistesIds => { - if (artistesIds.length === 0) { + if (!artistesIds || artistesIds.length === 0) { throw new ApplicationError('Champ obligatoire. Veuillez choisir au moins un artiste.'); } @@ -53,8 +53,8 @@ const jwennUserEpiId = async userId => { } const user = await strapi.db.query('plugin::users-permissions.user').findOne({ - where: {id: userId} - }) + where: {user: userId} + }) if (!user) { throw new ApplicationError('Utilisateur introuvable.') @@ -116,43 +116,36 @@ const jwennSuperAdminEpiId = async userAdminId => { module.exports = { beforeCreate: async event => { let {data} = event.params + + delete data.createdBy + delete data.updatedBy + strapi.service('api::parole.parole').validateParoles(data.titre, data.transcription) - const userAdmin = await jwennUserAdminEpiId(data?.createdBy) - - if (userAdmin) { - data.userAdmin = userAdmin - } - let artistesIds = [] - if (data?.artistes?.connect) { - for (const artiste of data.artistes.connect) { - artistesIds.push(artiste.id) + if (data?.artistes?.connect?.length) { + artistesIds = data.artistes.connect.map(a => a.id) + + if (data.titre && !data.forceSlug) { + const artiste = await jwennAwtisEpiId(artistesIds) + data.slug = getSlug(artiste, data.titre) } - } else { - artistesIds = data.artistes - } - if (data.titre && !data.forceSlug) { - const artiste = await jwennAwtisEpiId(artistesIds) - data.slug = getSlug(artiste, data.titre) - } + const getSlugExistance = await isSlugExists(data.slug) - const getSlugExistance = await isSlugExists(data.slug) - - if (getSlugExistance) { - throw new ApplicationError('Un morceau du même artiste existe déjà.') + if (getSlugExistance) { + throw new ApplicationError('Un morceau du même artiste existe déjà.') + } } }, beforeUpdate: async event => { const {state} = event let {data} = event.params - const {where} = event.params - const {id} = where + const {documentId} = data const previousParoles = await strapi.db.query('api::parole.parole').findOne({ - where: {id}, + where: {documentId}, populate: {difference: true, artistes: true} }) @@ -162,25 +155,27 @@ module.exports = { state.diff = difference } - if(!data.publishedAt && data.titre && data.transcription) { + if(!data.publishedAt && data.titre && data.transcription) { strapi.service('api::parole.parole').validateParoles(data.titre, data.transcription) if (data.titre && !data.forceSlug) { let artistes if (data.artistes.connect.length === 0) { artistes = previousParoles.artistes.map(a => a.alias).join('-') } else { - artistes = await jwennAwtisEpiId(data.artistes.connect) + let artistesIds = [] + + artistesIds = data.artistes.connect.map(a => a.id) + artistes = await jwennAwtisEpiId(artistesIds) + console.log('artistes', artistes); } data.slug = getSlug(artistes, data.titre) - } + } } if (data.publishedAt != null) { - const {id} = where const previousData = await strapi.db.query('api::parole.parole').findOne({ - where: {id}, - populate: {userAdmin: true, user: true} + where: {documentId} }) const previousPublishedAt = previousData.publishedAt @@ -239,7 +234,7 @@ module.exports = { } }, afterUpdate: async event => { - const {result, state} = event + const {result, state} = event if (state.diff) { await strapi.entityService.update('api::parole.parole', result.id, {