Update user generated API

This commit is contained in:
Jim Laurie 2017-11-17 12:05:03 +01:00
parent 304a5bbb72
commit 45da41ce11
3 changed files with 18 additions and 13 deletions

View File

@ -120,12 +120,12 @@ module.exports = {
};
}
// First, check if the user is the first one to register.
// First, check if the user is the first one to register as admin.
try {
const usersCount = await strapi.query('user', 'users-permissions').count();
const adminUsers = await strapi.query('user', 'users-permissions').findAll({ admin: true });
// Check if the user is the first to register
if (usersCount === 0) {
if (adminUsers.length === 0) {
params.admin = true;
}

View File

@ -21,13 +21,13 @@ module.exports = {
fetchAll: (params) => {
const convertedParams = strapi.utils.models.convertParams('user', params);
return User
return strapi.plugins['users-permissions'].models.user
.find()
.where(convertedParams.where)
.sort(convertedParams.sort)
.skip(convertedParams.start)
.limit(convertedParams.limit)
.populate(_.keys(_.groupBy(_.reject(strapi.models.user.associations, {autoPopulate: false}), 'alias')).join(' '));
.populate(_.keys(_.groupBy(_.reject(strapi.plugins['users-permissions'].models.user.associations, {autoPopulate: false}), 'alias')).join(' '));
},
/**
@ -37,9 +37,9 @@ module.exports = {
*/
fetch: (params) => {
return User
return strapi.plugins['users-permissions'].models.user
.findOne(params)
.populate(_.keys(_.groupBy(_.reject(strapi.models.user.associations, {autoPopulate: false}), 'alias')).join(' '));
.populate(_.keys(_.groupBy(_.reject(strapi.plugins['users-permissions'].models.user.associations, {autoPopulate: false}), 'alias')).join(' '));
},
/**
@ -49,7 +49,7 @@ module.exports = {
*/
add: async (values) => {
const data = await User.create(_.omit(values, _.keys(_.groupBy(strapi.models.user.associations, 'alias'))));
const data = await strapi.plugins['users-permissions'].models.user.create(_.omit(values, _.keys(_.groupBy(strapi.plugins['users-permissions'].models.user.associations, 'alias'))));
await strapi.hook.mongoose.manageRelations('user', _.merge(_.clone(data), { values }));
return data;
},
@ -65,7 +65,7 @@ module.exports = {
// To get the updated object, you have to execute the `findOne()` method
// or use the `findOneOrUpdate()` method with `{ new:true }` option.
await strapi.hook.mongoose.manageRelations('user', _.merge(_.clone(params), { values }));
return User.update(params, values, { multi: true });
return strapi.plugins['users-permissions'].models.user.update(params, values, { multi: true });
},
/**
@ -77,8 +77,8 @@ module.exports = {
remove: async params => {
// Note: To get the full response of Mongo, use the `remove()` method
// or add spent the parameter `{ passRawResult: true }` as second argument.
const data = await User.findOneAndRemove(params, {})
.populate(_.keys(_.groupBy(_.reject(strapi.models.user.associations, {autoPopulate: false}), 'alias')).join(' '));
const data = await strapi.plugins['users-permissions'].models.user.findOneAndRemove(params, {})
.populate(_.keys(_.groupBy(_.reject(strapi.plugins['users-permissions'].models.user.associations, {autoPopulate: false}), 'alias')).join(' '));
_.forEach(User.associations, async association => {
const search = (_.endsWith(association.nature, 'One')) ? { [association.via]: data._id } : { [association.via]: { $in: [data._id] } };

View File

@ -268,12 +268,17 @@ module.exports = {
}
const model = entity.toLowerCase();
let models = _.clone(strapi.models);
_.assign(models, Object.keys(strapi.plugins).reduce((acc, current) => {
_.assign(acc, _.get(strapi.plugins[current], ['models'], {}));
return acc;
}, {}));
if (!strapi.models.hasOwnProperty(model)) {
if (!models.hasOwnProperty(model)) {
return this.log.error(`The model ${model} can't be found.`);
}
const connector = strapi.models[model].orm;
const connector = models[model].orm;
if (!connector) {
throw new Error(`Impossible to determine the use ORM for the model ${model}.`);