fix: adapt parole beforeCreate method

This commit is contained in:
2026-04-28 12:31:19 +04:00
parent be4c7ed421
commit f781d52d1f
@@ -31,7 +31,7 @@ const isSlugExists = async existingSlug => {
} }
const jwennAwtisEpiId = async artistesIds => { const jwennAwtisEpiId = async artistesIds => {
if (artistesIds.length === 0) { if (!artistesIds || artistesIds.length === 0) {
throw new ApplicationError('Champ obligatoire. Veuillez choisir au moins un artiste.'); throw new ApplicationError('Champ obligatoire. Veuillez choisir au moins un artiste.');
} }
@@ -53,7 +53,7 @@ const jwennUserEpiId = async userId => {
} }
const user = await strapi.db.query('plugin::users-permissions.user').findOne({ const user = await strapi.db.query('plugin::users-permissions.user').findOne({
where: {id: userId} where: {user: userId}
}) })
if (!user) { if (!user) {
@@ -116,23 +116,16 @@ const jwennSuperAdminEpiId = async userAdminId => {
module.exports = { module.exports = {
beforeCreate: async event => { beforeCreate: async event => {
let {data} = event.params let {data} = event.params
delete data.createdBy
delete data.updatedBy
strapi.service('api::parole.parole').validateParoles(data.titre, data.transcription) strapi.service('api::parole.parole').validateParoles(data.titre, data.transcription)
const userAdmin = await jwennUserAdminEpiId(data?.createdBy)
if (userAdmin) {
data.userAdmin = userAdmin
}
let artistesIds = [] let artistesIds = []
if (data?.artistes?.connect) { if (data?.artistes?.connect?.length) {
for (const artiste of data.artistes.connect) { artistesIds = data.artistes.connect.map(a => a.id)
artistesIds.push(artiste.id)
}
} else {
artistesIds = data.artistes
}
if (data.titre && !data.forceSlug) { if (data.titre && !data.forceSlug) {
const artiste = await jwennAwtisEpiId(artistesIds) const artiste = await jwennAwtisEpiId(artistesIds)
@@ -144,15 +137,15 @@ module.exports = {
if (getSlugExistance) { if (getSlugExistance) {
throw new ApplicationError('Un morceau du même artiste existe déjà.') throw new ApplicationError('Un morceau du même artiste existe déjà.')
} }
}
}, },
beforeUpdate: async event => { beforeUpdate: async event => {
const {state} = event const {state} = event
let {data} = event.params let {data} = event.params
const {where} = event.params const {documentId} = data
const {id} = where
const previousParoles = await strapi.db.query('api::parole.parole').findOne({ const previousParoles = await strapi.db.query('api::parole.parole').findOne({
where: {id}, where: {documentId},
populate: {difference: true, artistes: true} populate: {difference: true, artistes: true}
}) })
@@ -169,7 +162,11 @@ module.exports = {
if (data.artistes.connect.length === 0) { if (data.artistes.connect.length === 0) {
artistes = previousParoles.artistes.map(a => a.alias).join('-') artistes = previousParoles.artistes.map(a => a.alias).join('-')
} else { } 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) data.slug = getSlug(artistes, data.titre)
@@ -177,10 +174,8 @@ module.exports = {
} }
if (data.publishedAt != null) { if (data.publishedAt != null) {
const {id} = where
const previousData = await strapi.db.query('api::parole.parole').findOne({ const previousData = await strapi.db.query('api::parole.parole').findOne({
where: {id}, where: {documentId}
populate: {userAdmin: true, user: true}
}) })
const previousPublishedAt = previousData.publishedAt const previousPublishedAt = previousData.publishedAt