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 => {
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, {