From dbcf2a5dc2bb569282fc7e83d123fe0d65665866 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20FAMIBELLE-PRONZOLA?= Date: Sat, 4 Jun 2022 01:17:08 +0400 Subject: [PATCH] Permit superuser to translate paroles in dashboard --- .../parole/content-types/parole/lifecycles.js | 55 +++++++++++++++---- 1 file changed, 44 insertions(+), 11 deletions(-) diff --git a/src/api/parole/content-types/parole/lifecycles.js b/src/api/parole/content-types/parole/lifecycles.js index 0f94346..ef195b3 100644 --- a/src/api/parole/content-types/parole/lifecycles.js +++ b/src/api/parole/content-types/parole/lifecycles.js @@ -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({