Move and update models to content-types

This commit is contained in:
Cédric FAMIBELLE-PRONZOLA
2022-05-12 04:20:00 +04:00
parent 2737770ff7
commit f08459ffad
9 changed files with 248 additions and 228 deletions
+53
View File
@@ -0,0 +1,53 @@
'use strict';
/**
* Read the documentation (https://strapi.io/documentation/v3.x/concepts/models.html#lifecycle-hooks)
* to customize this model
*/
const slugify = require('slugify')
const jwennTeksEpiId = async data => {
const teks = await strapi.query('teks').find({id_in: data})
return teks
}
const jwennAwtisEpiId = async id => {
const awtis = await strapi.query('awtis').find({id})
return awtis
}
module.exports = {
beforeUpdate: async event => {
let {data} = event
if (!data.slug) {
data.slug = slugify(data.alias, {lower: true, remove: /[*#+~.()'"!:@]/g})
}
},
beforeCreate: async event => {
let {data} = event
data.slug = slugify(data.alias, {lower: true, remove: /[*#+~.()'"!:@]/g})
},
afterUpdate: async event => {
let {data} = event
const {id} = data
const awtis = await jwennAwtisEpiId(id)
if (awtis.teks && awtis.teks.length >= 1) {
const teks = await jwennTeksEpiId(awtis.teks)
Promise.all(teks.map(async t => {
const {id, tit, slug, awtis} = t
const alias = awtis.map(a => a.alias).join('-')
const slugUpdated = slugify(`${alias}-${tit}`, {lower: true, remove: /[*#+~.()'"!:@]/g})
if (slug !== slugUpdated) {
await strapi.query('teks').update(
{id},
{slug: slugUpdated}
)
}
}))
}
}
}
+58
View File
@@ -0,0 +1,58 @@
{
"kind": "collectionType",
"collectionName": "awtis",
"info": {
"singularName": "awti",
"pluralName": "awtis",
"displayName": "Awtis",
"name": "awtis",
"description": ""
},
"options": {
"increments": true,
"timestamps": true,
"draftAndPublish": true
},
"attributes": {
"alias": {
"type": "string",
"unique": true,
"required": true
},
"prenon": {
"type": "string"
},
"non": {
"type": "string"
},
"biyografi": {
"type": "richtext"
},
"nesans": {
"type": "date"
},
"foto": {
"collection": "file",
"via": "related",
"allowedTypes": [
"images"
],
"plugin": "upload",
"required": false,
"pluginOptions": {}
},
"teks": {
"type": "relation",
"relation": "manyToMany",
"target": "api::tek.tek",
"inversedBy": "teks"
},
"user": {
"plugin": "users-permissions",
"model": "user"
},
"slug": {
"type": "string"
}
}
}
+39
View File
@@ -0,0 +1,39 @@
'use strict';
const jwennTeksEpiId = async teksId => {
const teks = await strapi.query('teks').findOne({id: teksId})
return teks
}
const jwennUserEpiId = async userId => {
const user = await strapi.query('user', 'users-permissions').findOne({id: userId})
return user
}
module.exports = {
beforeCreate: async event => {
let {data} = event
if (data.kontni && data.teks && data.user) {
const teks = await jwennTeksEpiId(data.teks)
const user = await jwennUserEpiId(data.user)
if(!teks || !user) {
throw strapi.errors.badRequest('Not found')
}
} else {
throw strapi.errors.badRequest('Missing fields')
}
},
afterCreate: async data => {
if (data.user) {
strapi.services.email.send(
process.env.SMTP_FROM,
process.env.SMTP_SEND_TO,
`Nouveau commentaire de ${data.user.username}`,
data.kontni
)
}
}
};
+39
View File
@@ -0,0 +1,39 @@
{
"kind": "collectionType",
"collectionName": "komante",
"info": {
"singularName": "komant",
"pluralName": "komante",
"displayName": "Komante",
"name": "komante",
"description": ""
},
"options": {
"increments": true,
"timestamps": true,
"draftAndPublish": true,
"privateAttributes": [
"createdAt",
"updatedAt"
]
},
"attributes": {
"kontni": {
"type": "richtext",
"required": true
},
"user": {
"plugin": "users-permissions",
"model": "user"
},
"teks": {
"type": "relation",
"relation": "oneToOne",
"target": "api::tek.tek"
},
"sentAt": {
"type": "datetime",
"required": true
}
}
}
+133
View File
@@ -0,0 +1,133 @@
'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 = {
beforeCreate: async event => {
let {data} = event
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 event => {
let {data} = event
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 event => {
let {data} = event
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.`
)
}
}
}
+87
View File
@@ -0,0 +1,87 @@
{
"kind": "collectionType",
"collectionName": "teks",
"info": {
"singularName": "tek",
"pluralName": "teks",
"displayName": "Teks",
"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": {
"type": "relation",
"relation": "manyToMany",
"target": "api::awti.awti",
"mappedBy": "tek"
},
"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": {
"type": "relation",
"relation": "oneToOne",
"target": "api::komant.komant"
},
"forceSlug": {
"type": "boolean"
},
"tradiksyonOtomatik": {
"type": "boolean"
},
"userAdmin": {
"type": "string"
}
}
}