2020-05-22 12:58:14 +02:00
|
|
|
const _ = require('lodash');
|
|
|
|
|
|
|
|
const sanitizeRole = role => {
|
|
|
|
return _.omit(role, ['users']);
|
|
|
|
};
|
|
|
|
|
2020-05-19 14:51:08 +02:00
|
|
|
/**
|
|
|
|
* Create and save a role in database
|
|
|
|
* @param attributes A partial role object
|
|
|
|
* @returns {Promise<role>}
|
|
|
|
*/
|
2020-05-22 12:58:14 +02:00
|
|
|
const create = async attributes => {
|
2020-05-25 11:22:35 +02:00
|
|
|
const alreadyExists = await exists({ name: attributes.name });
|
|
|
|
if (alreadyExists) {
|
|
|
|
throw strapi.errors.badRequest('ValidationError', {
|
|
|
|
name: [`The name must be unique and a role with name \`${attributes.name}\` already exists.`],
|
|
|
|
});
|
2020-05-22 12:58:14 +02:00
|
|
|
}
|
|
|
|
|
2020-05-19 14:51:08 +02:00
|
|
|
return strapi.query('role', 'admin').create(attributes);
|
|
|
|
};
|
2020-05-19 11:53:34 +02:00
|
|
|
|
2020-05-19 14:51:08 +02:00
|
|
|
/**
|
|
|
|
* Find a role in database
|
|
|
|
* @param params query params to find the role
|
|
|
|
* @returns {Promise<role>}
|
|
|
|
*/
|
2020-05-18 16:29:32 +02:00
|
|
|
const findOne = (params = {}) => {
|
2020-05-19 14:51:08 +02:00
|
|
|
return strapi.query('role', 'admin').findOne(params);
|
|
|
|
};
|
|
|
|
|
2020-05-18 16:29:32 +02:00
|
|
|
/**
|
|
|
|
* Find roles in database
|
|
|
|
* @param params query params to find the roles
|
2020-05-19 15:34:33 +02:00
|
|
|
* @returns {Promise<array>}
|
2020-05-18 16:29:32 +02:00
|
|
|
*/
|
|
|
|
const find = (params = {}) => {
|
|
|
|
return strapi.query('role', 'admin').find(params);
|
|
|
|
};
|
|
|
|
|
2020-05-19 15:40:04 +02:00
|
|
|
/**
|
|
|
|
* Find all roles in database
|
|
|
|
* @returns {Promise<array>}
|
|
|
|
*/
|
|
|
|
const findAll = () => {
|
|
|
|
return strapi.query('role', 'admin').find({ _limit: -1 });
|
|
|
|
};
|
|
|
|
|
2020-05-19 16:11:19 +02:00
|
|
|
/**
|
|
|
|
* Update a role in database
|
|
|
|
* @param params query params to find the role to update
|
|
|
|
* @param attributes A partial role object
|
|
|
|
* @returns {Promise<role>}
|
|
|
|
*/
|
2020-05-22 12:58:14 +02:00
|
|
|
const update = async (params, attributes) => {
|
|
|
|
if (_.has(params, 'id')) {
|
2020-05-25 11:22:35 +02:00
|
|
|
const alreadyExists = await exists({ name: attributes.name, id_ne: params.id });
|
|
|
|
if (alreadyExists) {
|
|
|
|
throw strapi.errors.badRequest('ValidationError', {
|
|
|
|
name: [
|
|
|
|
`The name must be unique and a role with name \`${attributes.name}\` already exists.`,
|
|
|
|
],
|
|
|
|
});
|
2020-05-22 12:58:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-19 16:11:19 +02:00
|
|
|
return strapi.query('role', 'admin').update(params, attributes);
|
|
|
|
};
|
|
|
|
|
2020-05-25 11:22:35 +02:00
|
|
|
/**
|
|
|
|
* Check if a role exists in database
|
|
|
|
* @param params query params to find the role
|
|
|
|
* @returns {Promise<boolean>}
|
|
|
|
*/
|
|
|
|
const exists = async params => {
|
|
|
|
const foundCount = await strapi.query('role', 'admin').count(params);
|
|
|
|
|
|
|
|
return foundCount > 0;
|
|
|
|
};
|
|
|
|
|
2020-05-19 14:51:08 +02:00
|
|
|
module.exports = {
|
2020-05-22 12:58:14 +02:00
|
|
|
sanitizeRole,
|
2020-05-19 14:51:08 +02:00
|
|
|
create,
|
|
|
|
findOne,
|
2020-05-18 16:29:32 +02:00
|
|
|
find,
|
2020-05-19 15:40:04 +02:00
|
|
|
findAll,
|
2020-05-19 16:11:19 +02:00
|
|
|
update,
|
2020-05-25 11:22:35 +02:00
|
|
|
exists,
|
2020-05-18 16:21:02 +02:00
|
|
|
};
|