2017-11-15 16:59:12 +01:00
|
|
|
const _ = require('lodash');
|
2019-03-13 19:27:18 +01:00
|
|
|
const { convertRestQueryParams, buildQuery } = require('strapi-utils');
|
2017-11-15 16:59:12 +01:00
|
|
|
|
2018-09-24 22:30:12 +02:00
|
|
|
module.exports = {
|
2019-03-13 19:27:18 +01:00
|
|
|
find: async function(params, populate) {
|
2019-02-02 13:19:05 +01:00
|
|
|
const model = this;
|
2019-03-13 19:27:18 +01:00
|
|
|
const filters = convertRestQueryParams(params);
|
2019-02-02 13:19:05 +01:00
|
|
|
|
2019-03-13 19:27:18 +01:00
|
|
|
return buildQuery({
|
|
|
|
model,
|
|
|
|
filters,
|
|
|
|
populate: populate || model.associations.map(x => x.alias),
|
|
|
|
}).lean();
|
2017-11-15 16:59:12 +01:00
|
|
|
},
|
|
|
|
|
2019-03-13 19:27:18 +01:00
|
|
|
count: async function(params) {
|
2019-02-02 13:19:05 +01:00
|
|
|
const model = this;
|
2019-03-13 19:27:18 +01:00
|
|
|
|
|
|
|
const filters = convertRestQueryParams(params);
|
|
|
|
|
|
|
|
return buildQuery({
|
|
|
|
model,
|
|
|
|
filters: { where: filters.where },
|
|
|
|
})
|
|
|
|
.count('count')
|
|
|
|
.then(results => _.get(results, [0, 'count'], 0));
|
2017-11-15 16:59:12 +01:00
|
|
|
},
|
|
|
|
|
2019-03-13 19:27:18 +01:00
|
|
|
findOne: async function(params, populate) {
|
2018-01-23 18:54:17 +01:00
|
|
|
const primaryKey = params[this.primaryKey] || params.id;
|
|
|
|
|
|
|
|
if (primaryKey) {
|
|
|
|
params = {
|
2019-03-13 19:27:18 +01:00
|
|
|
[this.primaryKey]: primaryKey,
|
2018-04-30 18:26:56 +02:00
|
|
|
};
|
2017-12-14 15:14:53 +01:00
|
|
|
}
|
2018-01-19 17:57:10 +01:00
|
|
|
|
2019-03-13 19:27:18 +01:00
|
|
|
return this.findOne(params)
|
2018-01-19 17:57:10 +01:00
|
|
|
.populate(populate || this.associations.map(x => x.alias).join(' '))
|
|
|
|
.lean();
|
2017-11-15 16:59:12 +01:00
|
|
|
},
|
|
|
|
|
2019-03-13 19:27:18 +01:00
|
|
|
create: async function(params) {
|
|
|
|
return this.create(
|
|
|
|
Object.keys(params).reduce((acc, current) => {
|
|
|
|
if (
|
|
|
|
_.get(this._attributes, [current, 'type']) ||
|
|
|
|
_.get(this._attributes, [current, 'model'])
|
|
|
|
) {
|
|
|
|
acc[current] = params[current];
|
|
|
|
}
|
2017-12-06 11:47:39 +01:00
|
|
|
|
2019-03-13 19:27:18 +01:00
|
|
|
return acc;
|
|
|
|
}, {})
|
|
|
|
).catch(err => {
|
|
|
|
if (err.message.indexOf('index:') !== -1) {
|
|
|
|
const message = err.message.split('index:');
|
|
|
|
const field = _.words(_.last(message).split('_')[0]);
|
|
|
|
const error = { message: `This ${field} is already taken`, field };
|
2018-01-25 18:12:29 +01:00
|
|
|
|
2019-03-13 19:27:18 +01:00
|
|
|
throw error;
|
|
|
|
}
|
2017-12-06 15:11:55 +01:00
|
|
|
|
2019-03-13 19:27:18 +01:00
|
|
|
throw err;
|
|
|
|
});
|
2017-11-15 16:59:12 +01:00
|
|
|
},
|
|
|
|
|
2019-03-13 19:27:18 +01:00
|
|
|
update: async function(search, params = {}) {
|
2018-01-23 15:38:43 +01:00
|
|
|
if (_.isEmpty(params)) {
|
|
|
|
params = search;
|
2018-01-22 18:19:44 +01:00
|
|
|
}
|
|
|
|
|
2018-01-23 15:38:43 +01:00
|
|
|
const primaryKey = search[this.primaryKey] || search.id;
|
2018-01-22 18:19:44 +01:00
|
|
|
|
|
|
|
if (primaryKey) {
|
|
|
|
search = {
|
2019-03-13 19:27:18 +01:00
|
|
|
[this.primaryKey]: primaryKey,
|
2018-04-30 18:26:56 +02:00
|
|
|
};
|
2018-01-22 18:19:44 +01:00
|
|
|
}
|
|
|
|
|
2019-01-23 16:56:04 +01:00
|
|
|
return this.updateOne(search, params, {
|
2019-03-13 19:27:18 +01:00
|
|
|
strict: false,
|
|
|
|
}).catch(error => {
|
|
|
|
const field = _.last(_.words(error.message.split('_')[0]));
|
|
|
|
const err = { message: `This ${field} is already taken`, field };
|
2017-12-06 15:11:55 +01:00
|
|
|
|
2019-03-13 19:27:18 +01:00
|
|
|
throw err;
|
|
|
|
});
|
2017-11-15 16:59:12 +01:00
|
|
|
},
|
|
|
|
|
2019-03-13 19:27:18 +01:00
|
|
|
delete: async function(params) {
|
2017-11-15 16:59:12 +01:00
|
|
|
// Delete entry.
|
2019-03-13 19:27:18 +01:00
|
|
|
return this.deleteOne({
|
|
|
|
[this.primaryKey]: params[this.primaryKey] || params.id,
|
|
|
|
});
|
2017-11-15 16:59:12 +01:00
|
|
|
},
|
|
|
|
|
2019-03-13 19:27:18 +01:00
|
|
|
deleteMany: async function(params) {
|
2018-05-03 16:26:57 +02:00
|
|
|
// Delete entry.
|
2019-03-13 19:27:18 +01:00
|
|
|
return this.deleteMany({
|
|
|
|
[this.primaryKey]: {
|
|
|
|
$in: params[this.primaryKey] || params.id,
|
|
|
|
},
|
|
|
|
});
|
2018-05-03 16:26:57 +02:00
|
|
|
},
|
|
|
|
|
2019-03-13 19:27:18 +01:00
|
|
|
search: async function(params) {
|
2017-11-27 12:19:36 +01:00
|
|
|
const re = new RegExp(params.id);
|
|
|
|
|
2019-03-13 19:27:18 +01:00
|
|
|
return this.find({
|
|
|
|
$or: [{ username: re }, { email: re }],
|
|
|
|
});
|
2017-11-30 12:27:04 +01:00
|
|
|
},
|
|
|
|
|
2019-03-13 19:27:18 +01:00
|
|
|
addPermission: async function(params) {
|
|
|
|
return this.create(params);
|
2018-01-17 18:50:12 +01:00
|
|
|
},
|
|
|
|
|
2019-03-13 19:27:18 +01:00
|
|
|
removePermission: async function(params) {
|
|
|
|
return this.remove(params);
|
|
|
|
},
|
2017-11-15 16:59:12 +01:00
|
|
|
};
|