Files
api.pawol.nu/api/komante/models/komante.js
T

40 lines
960 B
JavaScript
Raw Normal View History

2021-06-22 21:45:40 +02:00
'use strict';
const jwennTeksEpiId = async teksId => {
2022-05-05 20:20:00 +04:00
const teks = await strapi.query('teks').findOne({id: teksId})
2021-06-22 21:45:40 +02:00
return teks
}
const jwennUserEpiId = async userId => {
2022-05-05 20:20:00 +04:00
const user = await strapi.query('user', 'users-permissions').findOne({id: userId})
2021-06-22 21:45:40 +02:00
return user
}
module.exports = {
lifecycles: {
beforeCreate: async data => {
if (data.kontni && data.teks && data.user) {
const teks = await jwennTeksEpiId(data.teks)
const user = await jwennUserEpiId(data.user)
2022-03-27 02:07:22 +04:00
if(!teks || !user) {
2021-06-22 21:45:40 +02:00
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
)
}
}
}
};