2022-05-12 03:22:15 +04:00
|
|
|
'use strict';
|
|
|
|
|
|
2022-05-12 03:03:29 +04:00
|
|
|
const { createCoreController } = require('@strapi/strapi').factories;
|
2022-05-20 00:06:01 +04:00
|
|
|
const slugify = require('slugify')
|
2022-05-12 03:03:29 +04:00
|
|
|
|
2022-05-20 00:06:01 +04:00
|
|
|
const getSlug = text => {
|
|
|
|
|
return slugify(text, {lower: true, remove: /[*#+~.()'"!:@]/g})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
module.exports = createCoreController('api::artiste.artiste', ({strapi}) => ({
|
|
|
|
|
async create(ctx) {
|
|
|
|
|
const {body} = ctx.request
|
|
|
|
|
let {data} = body
|
|
|
|
|
|
|
|
|
|
if (ctx.request && ctx.request.header && ctx.request.header.authorization) {
|
|
|
|
|
try {
|
|
|
|
|
const {id} = await strapi.plugins[
|
|
|
|
|
'users-permissions'
|
|
|
|
|
].services.jwt.getToken(ctx)
|
|
|
|
|
|
|
|
|
|
if (id !== data.user.id) {
|
|
|
|
|
throw new UnauthorizedError('Opération non autorisée')
|
|
|
|
|
}
|
|
|
|
|
} catch (err) {
|
|
|
|
|
throw new UnauthorizedError(ctx, err, 'Opération non autorisée')
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const user = await strapi.entityService.findOne('plugin::users-permissions.user', body.data.user.id)
|
|
|
|
|
|
|
|
|
|
if (!user) {
|
|
|
|
|
throw new NotFoundError('Utilisateur introuvable.')
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (user.id !== data.user.id || user.username !== data.user.username || user.email !== data.user.email) {
|
2023-04-02 12:27:05 +04:00
|
|
|
ctx.badRequest('Informations non valides.')
|
2022-05-20 00:06:01 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const artiste = await strapi.db.query('api::artiste.artiste').findOne({
|
|
|
|
|
where: {slug: getSlug(data.alias)}
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
if (artiste) {
|
|
|
|
|
return artiste
|
|
|
|
|
} else {
|
|
|
|
|
const newArtiste = await strapi.entityService.create('api::artiste.artiste', {
|
|
|
|
|
data: {
|
|
|
|
|
...data
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
return newArtiste
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}))
|