| 
									
										
										
										
											2018-02-19 15:41:26 +01:00
										 |  |  | const _ = require('lodash'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | module.exports = { | 
					
						
							|  |  |  |   find: async function (params = {}, populate) { | 
					
						
							|  |  |  |     return this | 
					
						
							|  |  |  |       .find(params.where) | 
					
						
							|  |  |  |       .limit(Number(params.limit)) | 
					
						
							|  |  |  |       .sort(params.sort) | 
					
						
							| 
									
										
										
										
											2018-02-23 12:21:57 +01:00
										 |  |  |       .skip(Number(params.start)) | 
					
						
							| 
									
										
										
										
											2018-02-19 15:41:26 +01:00
										 |  |  |       .populate(populate || this.associations.map(x => x.alias).join(' ')) | 
					
						
							|  |  |  |       .lean(); | 
					
						
							|  |  |  |   }, | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   count: async function (params = {}) { | 
					
						
							|  |  |  |     return Number(await this | 
					
						
							| 
									
										
										
										
											2019-01-23 16:56:04 +01:00
										 |  |  |       .countDocuments(params)); | 
					
						
							| 
									
										
										
										
											2018-02-19 15:41:26 +01:00
										 |  |  |   }, | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   findOne: async function (params, populate) { | 
					
						
							|  |  |  |     const primaryKey = params[this.primaryKey] || params.id; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (primaryKey) { | 
					
						
							|  |  |  |       params = { | 
					
						
							|  |  |  |         [this.primaryKey]: primaryKey | 
					
						
							| 
									
										
										
										
											2018-04-30 18:00:01 +02:00
										 |  |  |       }; | 
					
						
							| 
									
										
										
										
											2018-02-19 15:41:26 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return this | 
					
						
							|  |  |  |       .findOne(params) | 
					
						
							|  |  |  |       .populate(populate || this.associations.map(x => x.alias).join(' ')) | 
					
						
							|  |  |  |       .lean(); | 
					
						
							|  |  |  |   }, | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   create: async function (params) { | 
					
						
							| 
									
										
										
										
											2018-02-27 11:52:18 +01:00
										 |  |  |     // Exclude relationships.
 | 
					
						
							|  |  |  |     const values = Object.keys(params).reduce((acc, current) => { | 
					
						
							| 
									
										
										
										
											2018-02-19 15:41:26 +01:00
										 |  |  |       if (_.get(this._attributes, [current, 'type']) || _.get(this._attributes, [current, 'model'])) { | 
					
						
							|  |  |  |         acc[current] = params[current]; | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       return acc; | 
					
						
							| 
									
										
										
										
											2018-02-27 11:52:18 +01:00
										 |  |  |     }, {}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return this.create(values) | 
					
						
							|  |  |  |       .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 }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |           throw error; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2018-02-19 15:41:26 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-27 11:52:18 +01:00
										 |  |  |         throw err; | 
					
						
							|  |  |  |       }); | 
					
						
							| 
									
										
										
										
											2018-02-19 15:41:26 +01:00
										 |  |  |   }, | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   update: async function (search, params = {}) { | 
					
						
							|  |  |  |     if (_.isEmpty(params)) { | 
					
						
							|  |  |  |       params = search; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     const primaryKey = search[this.primaryKey] || search.id; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (primaryKey) { | 
					
						
							|  |  |  |       search = { | 
					
						
							|  |  |  |         [this.primaryKey]: primaryKey | 
					
						
							| 
									
										
										
										
											2018-04-30 18:00:01 +02:00
										 |  |  |       }; | 
					
						
							| 
									
										
										
										
											2018-02-19 15:41:26 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-23 16:56:04 +01:00
										 |  |  |     return this.updateOne(search, params, { | 
					
						
							| 
									
										
										
										
											2018-02-19 15:41:26 +01:00
										 |  |  |       strict: false | 
					
						
							|  |  |  |     }) | 
					
						
							| 
									
										
										
										
											2018-04-30 18:00:01 +02:00
										 |  |  |       .catch((error) => { | 
					
						
							|  |  |  |         const field = _.last(_.words(error.message.split('_')[0])); | 
					
						
							|  |  |  |         const err = { message: `This ${field} is already taken`, field }; | 
					
						
							| 
									
										
										
										
											2018-02-19 15:41:26 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-30 18:00:01 +02:00
										 |  |  |         throw err; | 
					
						
							|  |  |  |       }); | 
					
						
							| 
									
										
										
										
											2018-02-19 15:41:26 +01:00
										 |  |  |   }, | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   delete: async function (params) { | 
					
						
							|  |  |  |     // Delete entry.
 | 
					
						
							|  |  |  |     return this | 
					
						
							|  |  |  |       .remove({ | 
					
						
							|  |  |  |         [this.primaryKey]: params[this.primaryKey] || params.id | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  |   }, | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   search: async function (params) { | 
					
						
							| 
									
										
										
										
											2018-03-06 16:24:16 +01:00
										 |  |  |     const re = new RegExp(params.id, 'i'); | 
					
						
							| 
									
										
										
										
											2018-02-19 15:41:26 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     return this | 
					
						
							|  |  |  |       .find({ | 
					
						
							|  |  |  |         '$or': [ | 
					
						
							| 
									
										
										
										
											2018-02-22 17:12:03 +01:00
										 |  |  |           { hash: re }, | 
					
						
							|  |  |  |           { name: re } | 
					
						
							| 
									
										
										
										
											2018-02-19 15:41:26 +01:00
										 |  |  |         ] | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  |   }, | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   addPermission: async function (params) { | 
					
						
							|  |  |  |     return this | 
					
						
							|  |  |  |       .create(params); | 
					
						
							|  |  |  |   }, | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   removePermission: async function (params) { | 
					
						
							|  |  |  |     return this | 
					
						
							|  |  |  |       .remove(params); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | }; |