Move and update models to content-types
This commit is contained in:
@@ -1,130 +0,0 @@
|
||||
'use strict';
|
||||
|
||||
const slugify = require('slugify')
|
||||
const axios = require('axios')
|
||||
|
||||
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 awtis = await strapi.query('awtis').find({id: data})
|
||||
return awtis.map(a => a.alias).join('-')
|
||||
}
|
||||
|
||||
const jwennUserEpiId = async userId => {
|
||||
if (!userId) {
|
||||
return null
|
||||
}
|
||||
|
||||
const user = await strapi.query('user', 'users-permissions').findOne({id: userId})
|
||||
return user
|
||||
}
|
||||
|
||||
const jwennUserAdminEpiId = async userAdminId => {
|
||||
if (!userAdminId) {
|
||||
return null
|
||||
}
|
||||
|
||||
const user = await strapi.query('user', 'admin').findOne({id: userAdminId, 'roles.code_nin': 'strapi-super-admin'})
|
||||
return user
|
||||
}
|
||||
|
||||
const translateTeks = async teksFR => {
|
||||
const english = await strapi.services.translator.translate('FR', 'EN', teksFR)
|
||||
const espagnol = await strapi.services.translator.translate('FR', 'ES', teksFR)
|
||||
const deutsch = await strapi.services.translator.translate('FR', 'DE', teksFR)
|
||||
const italiano = await strapi.services.translator.translate('FR', 'IT', teksFR)
|
||||
|
||||
return {
|
||||
francais: teksFR,
|
||||
english: english + '\n\n (Translated by DeepL)',
|
||||
espagnol: espagnol + '\n\n (Traducido por DeepL)',
|
||||
deutsch: deutsch + '\n\n (Übersetzt von DeepL)',
|
||||
italiano: italiano + '\n\n (Tradotto da DeepL)'
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
lifecycles: {
|
||||
beforeCreate: async data => {
|
||||
const user = await jwennUserEpiId(data?.user?.id)
|
||||
const userAdmin = await jwennUserAdminEpiId(data?.created_by)
|
||||
|
||||
if (userAdmin) {
|
||||
data.userAdmin = userAdmin.username
|
||||
}
|
||||
|
||||
if (data.tit && !data.forceSlug) {
|
||||
const awtis = await jwennAwtisEpiId(data.awtis)
|
||||
data.slug = slugify(`${awtis}-${data.tit}`, {lower: true, remove: /[*#+~.()'"!:@]/g})
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
},
|
||||
beforeUpdate: async (params, data) => {
|
||||
if (data.tit && !data.forceSlug) {
|
||||
const awtis = await jwennAwtisEpiId(data.awtis)
|
||||
data.slug = slugify(`${awtis}-${data.tit}`, {lower: true, remove: /[*#+~.()'"!:@]/g})
|
||||
}
|
||||
if (data.published_at != null) {
|
||||
const {id} = params
|
||||
const previousData = await strapi.query('teks').findOne({id})
|
||||
|
||||
const previousPublishedAt = previousData.published_at
|
||||
const currentPublished_at = data.published_at
|
||||
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}`
|
||||
if (previousData.user) {
|
||||
strapi.services.email.send(
|
||||
process.env.SMTP_FROM,
|
||||
previousData.user.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}`
|
||||
)
|
||||
}
|
||||
|
||||
const user = await jwennUserAdminEpiId(previousData?.created_by?.id)
|
||||
|
||||
if (user) {
|
||||
strapi.services.email.send(
|
||||
process.env.SMTP_FROM,
|
||||
user.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}`
|
||||
)
|
||||
}
|
||||
|
||||
await axios.post(`${MESSAGE_URL}&text=${message}`)
|
||||
}
|
||||
}
|
||||
},
|
||||
afterCreate: async data => {
|
||||
if (data.user) {
|
||||
strapi.services.email.send(
|
||||
process.env.SMTP_FROM,
|
||||
process.env.SMTP_SEND_TO,
|
||||
`Nouveau texte de ${data.user.username} : "${data.tit}" (site)`,
|
||||
`Le titre "${data.tit}" a été soumis depuis le site.`
|
||||
)
|
||||
}
|
||||
|
||||
const user = await jwennUserAdminEpiId(data?.created_by?.id)
|
||||
|
||||
if (user) {
|
||||
strapi.services.email.send(
|
||||
process.env.SMTP_FROM,
|
||||
process.env.SMTP_SEND_TO,
|
||||
`Nouveau texte de ${user.username} : "${data.tit}" (dashboard)`,
|
||||
`Le titre "${data.tit}" a été soumis depuis le dashboard.`
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,82 +0,0 @@
|
||||
{
|
||||
"kind": "collectionType",
|
||||
"collectionName": "teks",
|
||||
"info": {
|
||||
"name": "Teks",
|
||||
"description": ""
|
||||
},
|
||||
"options": {
|
||||
"increments": true,
|
||||
"timestamps": true,
|
||||
"draftAndPublish": true
|
||||
},
|
||||
"attributes": {
|
||||
"tit": {
|
||||
"type": "string",
|
||||
"required": true
|
||||
},
|
||||
"transkripsyon": {
|
||||
"type": "richtext",
|
||||
"required": true
|
||||
},
|
||||
"tradiksyon": {
|
||||
"type": "component",
|
||||
"repeatable": false,
|
||||
"component": "trad.traductions"
|
||||
},
|
||||
"lanne": {
|
||||
"type": "integer"
|
||||
},
|
||||
"lyen": {
|
||||
"type": "component",
|
||||
"repeatable": true,
|
||||
"component": "url.liens"
|
||||
},
|
||||
"awtis": {
|
||||
"via": "teks",
|
||||
"collection": "awtis",
|
||||
"dominant": true
|
||||
},
|
||||
"kouteyAchtey": {
|
||||
"type": "component",
|
||||
"repeatable": true,
|
||||
"component": "store.store"
|
||||
},
|
||||
"slug": {
|
||||
"type": "string"
|
||||
},
|
||||
"kouveti": {
|
||||
"model": "file",
|
||||
"via": "related",
|
||||
"allowedTypes": [
|
||||
"images"
|
||||
],
|
||||
"plugin": "upload",
|
||||
"required": false,
|
||||
"pluginOptions": {}
|
||||
},
|
||||
"okiMizikID": {
|
||||
"type": "integer"
|
||||
},
|
||||
"user": {
|
||||
"plugin": "users-permissions",
|
||||
"model": "user"
|
||||
},
|
||||
"eksplisit": {
|
||||
"type": "boolean"
|
||||
},
|
||||
"komante": {
|
||||
"via": "teks",
|
||||
"collection": "komante"
|
||||
},
|
||||
"forceSlug": {
|
||||
"type": "boolean"
|
||||
},
|
||||
"tradiksyonOtomatik": {
|
||||
"type": "boolean"
|
||||
},
|
||||
"userAdmin": {
|
||||
"type": "string"
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user