Permit superuser to translate paroles in dashboard

This commit is contained in:
Cédric FAMIBELLE-PRONZOLA
2022-06-04 01:17:08 +04:00
parent 95ad4e7b0a
commit dbcf2a5dc2
@@ -76,24 +76,35 @@ const jwennUserAdminEpiId = async userAdminId => {
return userAdmin
}
const validateParoles = (titre, transcription) => {
if (!titre || titre.trim().length === 0) {
throw new ValidationError('Champ obligatoire. Veuillez choisir un titre.');
const jwennSuperAdminEpiId = async userAdminId => {
if (!userAdminId) {
return null
}
if (!transcription || transcription.trim().length === 0) {
throw new ValidationError('Champ obligatoire. Veuillez renseigner la transcription.')
}
const userAdmin = await strapi.db.query('admin::user').findOne({
where: {
$and: [
{
id: userAdminId
},
{
roles: {
code: {
$eq: 'strapi-super-admin'
}
}
}
]
}
})
if (transcription.trim().length < 10) {
throw new ValidationError('La transcription doit contenir au moins 10 caractères.')
}
return userAdmin
}
module.exports = {
beforeCreate: async event => {
let {data} = event.params
validateParoles(data.titre, data.transcription)
strapi.service('api::parole.parole').validateParoles(data.titre, data.transcription)
const userAdmin = await jwennUserAdminEpiId(data?.createdBy)
@@ -117,7 +128,7 @@ module.exports = {
const {where} = event.params
if(!data.publishedAt && data.titre && data.transcription) {
validateParoles(data.titre, data.transcription)
strapi.service('api::parole.parole').validateParoles(data.titre, data.transcription)
if (data.titre && !data.forceSlug) {
const artiste = await jwennAwtisEpiId(data.artistes)
data.slug = getSlug(artiste, data.titre)
@@ -170,6 +181,28 @@ module.exports = {
const {data} = event.params
const user = await jwennUserEpiId(data.user)
const userAdmin = await jwennUserAdminEpiId(data?.createdBy)
const superAdmin = await jwennSuperAdminEpiId(data?.createdBy)
const traductionsId = data.traductions.id
const result = await strapi.db.query('api::parole.parole').findOne({
where: {
traductions: {
id: {
$eq: traductionsId
}
}
},
populate: {traductions: true, artistes: true}
})
if (superAdmin && data.traductionAuto && result.traductions.francais && (!result.traductions.anglais || !result.traductions.espagnol || !result.traductions.allemand || !result.traductions.italien)) {
const traductions = await strapi.service('api::parole.parole').translateLyrics(result.traductions.francais)
await strapi.entityService.update('api::parole.parole', result.id, {
data: {
traductions
}
})
}
if (user) {
strapi.plugins['email'].services.email.send({