Fix and improve paroles lifecycles
This commit is contained in:
@@ -2,14 +2,43 @@
|
||||
|
||||
const slugify = require('slugify')
|
||||
const axios = require('axios')
|
||||
const { ValidationError } = require("@strapi/utils").errors
|
||||
|
||||
const TELEGRAM_API_URL = 'https://api.telegram.org'
|
||||
const TELEGRAM_CHAN_ID = process.env.TELEGRAM_CHAN_ID || null
|
||||
const TELEGRAM_API_TOKEN = process.env.TELEGRAM_API_TOKEN || null
|
||||
const MESSAGE_URL = `${TELEGRAM_API_URL}/bot${TELEGRAM_API_TOKEN}/sendMessage?chat_id=${TELEGRAM_CHAN_ID}&parse_mode=html`
|
||||
|
||||
const jwennAwtisEpiId = async data => {
|
||||
const artiste = await strapi.query('artiste').find({id: data})
|
||||
const getSlug = (artiste, parole) => {
|
||||
return slugify(`${artiste}-${parole}`, {lower: true, remove: /[*#+~.()'"!:@]/g})
|
||||
}
|
||||
|
||||
const isSlugExists = async existingSlug => {
|
||||
const slugs = await strapi.db.query('api::parole.parole').count({
|
||||
where: {
|
||||
slug: {
|
||||
$eq: existingSlug
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
return Boolean(slugs)
|
||||
}
|
||||
|
||||
const jwennAwtisEpiId = async id => {
|
||||
if (!id) {
|
||||
throw new ValidationError('Champ obligatoire. Veuillez choisir au moins un artiste.');
|
||||
}
|
||||
|
||||
const artiste = await strapi.db.query('api::artiste.artiste').findMany({
|
||||
select: ['alias'],
|
||||
where: {
|
||||
id: {
|
||||
$in: id
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
return artiste.map(a => a.alias).join('-')
|
||||
}
|
||||
|
||||
@@ -18,7 +47,7 @@ const jwennUserEpiId = async userId => {
|
||||
return null
|
||||
}
|
||||
|
||||
const user = await strapi.query('user', 'users-permissions').findOne({id: userId})
|
||||
const user = await strapi.query.db.query('plugin::users-permissions.user').findOne({id: userId})
|
||||
return user
|
||||
}
|
||||
|
||||
@@ -27,8 +56,24 @@ const jwennUserAdminEpiId = async userAdminId => {
|
||||
return null
|
||||
}
|
||||
|
||||
const user = await strapi.query('user', 'admin').findOne({id: userAdminId, 'roles.code_nin': 'strapi-super-admin'})
|
||||
return user
|
||||
const userAdmin = await strapi.db.query('admin::user').findOne({
|
||||
where: {
|
||||
$and: [
|
||||
{
|
||||
id: userAdminId
|
||||
},
|
||||
{
|
||||
$not: {
|
||||
roles: {
|
||||
code: 'strapi-super-admin'
|
||||
}
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
})
|
||||
|
||||
return userAdmin
|
||||
}
|
||||
|
||||
const translateTeks = async teksFR => {
|
||||
@@ -48,38 +93,51 @@ const translateTeks = async teksFR => {
|
||||
|
||||
module.exports = {
|
||||
beforeCreate: async event => {
|
||||
let {data} = event
|
||||
let {data} = event.params
|
||||
const user = await jwennUserEpiId(data?.user?.id)
|
||||
const userAdmin = await jwennUserAdminEpiId(data?.created_by)
|
||||
const userAdmin = await jwennUserAdminEpiId(data?.createdBy)
|
||||
|
||||
if (userAdmin) {
|
||||
data.userAdmin = userAdmin.username
|
||||
data.userAdmin = userAdmin
|
||||
}
|
||||
|
||||
if (data.tit && !data.forceSlug) {
|
||||
const artiste = await jwennAwtisEpiId(data.artiste)
|
||||
data.slug = slugify(`${artiste}-${data.tit}`, {lower: true, remove: /[*#+~.()'"!:@]/g})
|
||||
const artiste = await jwennAwtisEpiId(data.artistes)
|
||||
data.slug = getSlug(artiste, data.tit)
|
||||
}
|
||||
|
||||
const getSlugExistance = await isSlugExists(data.slug)
|
||||
|
||||
if (getSlugExistance) {
|
||||
throw new ValidationError('Un morceau du même artiste existe déjà.')
|
||||
}
|
||||
|
||||
if (user && user.canAutoTranslate && data.tradiksyonOtomatik && data.tradiksyon.francais && (!data.tradiksyon.english || !data.tradiksyon.espagnol || !data.tradiksyon.deutsch || !data.tradiksyon.italiano)) {
|
||||
const traslate = await translateTeks(data.tradiksyon.francais)
|
||||
data.tradiksyon = traslate
|
||||
const translate = await translateTeks(data.tradiksyon.francais)
|
||||
data.tradiksyon = translate
|
||||
}
|
||||
|
||||
},
|
||||
beforeUpdate: async event => {
|
||||
let {data} = event
|
||||
let {data} = event.params
|
||||
const {where} = event.params
|
||||
|
||||
if(!data.publishedAt) {
|
||||
if (data.tit && !data.forceSlug) {
|
||||
const artiste = await jwennAwtisEpiId(data.artiste)
|
||||
data.slug = slugify(`${artiste}-${data.tit}`, {lower: true, remove: /[*#+~.()'"!:@]/g})
|
||||
const artiste = await jwennAwtisEpiId(data.artistes)
|
||||
data.slug = getSlug(artiste, data.tit)
|
||||
}
|
||||
}
|
||||
if (data.published_at != null) {
|
||||
const {id} = params
|
||||
const previousData = await strapi.query('paroles').findOne({id})
|
||||
|
||||
const previousPublishedAt = previousData.published_at
|
||||
const currentPublished_at = data.published_at
|
||||
if (data.publishedAt != null) {
|
||||
const {id} = where
|
||||
const previousData = await strapi.db.query('api::parole.parole').findOne({
|
||||
where: {id},
|
||||
populate: {userAdmin: true, user: true}
|
||||
})
|
||||
|
||||
const previousPublishedAt = previousData.publishedAt
|
||||
const currentPublished_at = data.publishedAt
|
||||
if (currentPublished_at != previousPublishedAt) {
|
||||
const message = `<b>Nouvelle publication</b> \xF0\x9F\x8E\xB6 \xF0\x9F\x94\xA5
|
||||
\n${process.env.WEBSITE_URL}/paroles/${previousData.slug}`
|
||||
@@ -92,12 +150,10 @@ module.exports = {
|
||||
)
|
||||
}
|
||||
|
||||
const user = await jwennUserAdminEpiId(previousData?.created_by?.id)
|
||||
|
||||
if (user) {
|
||||
if (previousData.userAdmin) {
|
||||
strapi.services.email.send(
|
||||
process.env.SMTP_FROM,
|
||||
user.email,
|
||||
previousData.userAdmin.email,
|
||||
`Publication de "${previousData.tit}"`,
|
||||
`Le titre que vous avez soumis, "${previousData.tit}" a été publié sur le site. Vous pouvez le trouver à l'adresse ${process.env.WEBSITE_URL}/paroles/${previousData.slug}`
|
||||
)
|
||||
@@ -108,7 +164,7 @@ module.exports = {
|
||||
}
|
||||
},
|
||||
afterCreate: async event => {
|
||||
let {data} = event
|
||||
const {data} = event.params
|
||||
|
||||
if (data.user) {
|
||||
strapi.services.email.send(
|
||||
@@ -119,13 +175,11 @@ module.exports = {
|
||||
)
|
||||
}
|
||||
|
||||
const user = await jwennUserAdminEpiId(data?.created_by?.id)
|
||||
|
||||
if (user) {
|
||||
if (data.userAdmin) {
|
||||
strapi.services.email.send(
|
||||
process.env.SMTP_FROM,
|
||||
process.env.SMTP_SEND_TO,
|
||||
`Nouveau texte de ${user.username} : "${data.tit}" (dashboard)`,
|
||||
`Nouveau texte de ${data.userAdmin.username} : "${data.tit}" (dashboard)`,
|
||||
`Le titre "${data.tit}" a été soumis depuis le dashboard.`
|
||||
)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user