fix: adapt parole beforeCreate method
This commit is contained in:
@@ -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,8 +53,8 @@ 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) {
|
||||||
throw new ApplicationError('Utilisateur introuvable.')
|
throw new ApplicationError('Utilisateur introuvable.')
|
||||||
@@ -116,43 +116,36 @@ 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)
|
|
||||||
|
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 getSlugExistance = await isSlugExists(data.slug)
|
||||||
const artiste = await jwennAwtisEpiId(artistesIds)
|
|
||||||
data.slug = getSlug(artiste, data.titre)
|
|
||||||
}
|
|
||||||
|
|
||||||
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 => {
|
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}
|
||||||
})
|
})
|
||||||
|
|
||||||
@@ -162,25 +155,27 @@ module.exports = {
|
|||||||
state.diff = difference
|
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)
|
strapi.service('api::parole.parole').validateParoles(data.titre, data.transcription)
|
||||||
if (data.titre && !data.forceSlug) {
|
if (data.titre && !data.forceSlug) {
|
||||||
let artistes
|
let artistes
|
||||||
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)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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
|
||||||
@@ -239,7 +234,7 @@ module.exports = {
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
afterUpdate: async event => {
|
afterUpdate: async event => {
|
||||||
const {result, state} = event
|
const {result, state} = event
|
||||||
|
|
||||||
if (state.diff) {
|
if (state.diff) {
|
||||||
await strapi.entityService.update('api::parole.parole', result.id, {
|
await strapi.entityService.update('api::parole.parole', result.id, {
|
||||||
|
|||||||
Reference in New Issue
Block a user