@@ -44,6 +44,14 @@ UsersController.find = function(req,res,next){
4444 var query ;
4545 if ( req . query . search ) {
4646 query = req . query . search ;
47+ // Clean appId and userId
48+ if ( query && query . appId ) {
49+ delete query . appId ;
50+ }
51+ if ( query && query . userId ) {
52+ delete query . userId ;
53+ }
54+
4755 Users . search ( query )
4856 . then ( function ( resp ) {
4957 res . ok ( resp ) ;
@@ -54,15 +62,18 @@ UsersController.find = function(req,res,next){
5462 } else {
5563 query = req . query ;
5664 // Clean appId and userId
57- delete query . appId ;
58- delete query . userId ;
59- delete query . developer ;
60- var projection = query . projection ; // Projection should be comma separated. eg. name,location
65+ if ( query && query . appId ) {
66+ delete query . appId ;
67+ }
68+ if ( query && query . userId ) {
69+ delete query . userId ;
70+ }
71+ var projection = query . select ; // Projection should be comma separated. eg. name,location
6172 var ourProjection ;
6273
6374 if ( projection ) {
64- ourProjection = this . buildProjection ( projection ) ;
65- delete query . projection ;
75+ ourProjection = UsersController . buildProjection ( projection ) ;
76+ delete query . select ;
6677 }
6778 var limit = query . limit * 1 ;
6879 if ( limit ) {
@@ -84,7 +95,6 @@ UsersController.find = function(req,res,next){
8495 } else {
8596 query . createdAt = { } ;
8697 query . createdAt . $gt = new Date ( '1989-03-15T00:00:00' ) . toISOString ( ) ;
87- delete query . from ;
8898 if ( to ) {
8999 delete query . to ;
90100 } else {
@@ -106,6 +116,8 @@ UsersController.find = function(req,res,next){
106116 if ( populate ) {
107117 delete query . populate ;
108118 }
119+
120+ console . log ( 'our query: >>>>>>>>>>>' , query ) ;
109121 var totalResult = Users . count ( query ) ;
110122 var total = Users . count ( { } ) ;
111123 var question = Users . find ( query ) ;
@@ -131,7 +143,12 @@ UsersController.find = function(req,res,next){
131143 return [ question . select ( resp ) , total , totalResult ] ;
132144 } )
133145 . spread ( function ( resp , total , totalResult ) {
134- var ourLastId = resp [ resp . length - 1 ] . _id ;
146+ var ourLastId ;
147+ if ( resp . length === 0 ) {
148+ ourLastId = null ;
149+ } else {
150+ ourLastId = resp [ resp . length - 1 ] . _id ;
151+ }
135152 var extraData = { } ;
136153 extraData . limit = limit * 1 ;
137154 extraData . total = total ;
@@ -170,7 +187,18 @@ UsersController.find = function(req,res,next){
170187
171188UsersController . findOne = function ( req , res , next ) {
172189 var id = req . params . id ;
173- Users . findById ( id )
190+ var query = req . query ;
191+ var populate ;
192+ if ( query ) {
193+ populate = query . populate ; // Samples: 'name location' will populate name and location references. only supports this for now | 'name', 'firstname' will populate name referenece and only pick the firstname attribute
194+ }
195+ var question = Users . findById ( id ) ;
196+ if ( populate ) {
197+ delete query . populate ;
198+ question = question . populate ( populate ) ;
199+ }
200+
201+ question
174202 . then ( function ( resp ) {
175203 res . ok ( resp ) ;
176204 } )
@@ -181,6 +209,9 @@ UsersController.findOne = function(req,res,next){
181209
182210UsersController . create = function ( req , res , next ) {
183211 var data = req . body ;
212+ if ( data && data . secure ) {
213+ delete data . secure ;
214+ }
184215 Users . create ( data )
185216 . then ( function ( resp ) {
186217 res . ok ( resp ) ;
@@ -192,7 +223,17 @@ UsersController.create = function(req,res,next){
192223
193224UsersController . update = function ( req , res , next ) {
194225 var query = req . query ;
226+ // Clean appId and userId
227+ if ( query && query . appId ) {
228+ delete query . appId ;
229+ }
230+ if ( query && query . userId ) {
231+ delete query . userId ;
232+ }
195233 var data = req . body ;
234+ if ( data && data . secure ) {
235+ delete data . secure ;
236+ }
196237 Users . updateMany ( query , data )
197238 . then ( function ( resp ) {
198239 res . ok ( resp ) ;
@@ -205,6 +246,10 @@ UsersController.update = function(req,res,next){
205246UsersController . updateOne = function ( req , res , next ) {
206247 var id = req . params . id ;
207248 var data = req . body ;
249+ if ( data && data . secure ) {
250+ delete data . secure ;
251+ }
252+
208253 Users . findByIdAndUpdate ( id , data )
209254 . then ( function ( resp ) {
210255 res . ok ( resp ) ;
@@ -216,6 +261,13 @@ UsersController.updateOne = function(req,res,next){
216261
217262UsersController . delete = function ( req , res , next ) {
218263 var query = req . query ;
264+ // Clean appId and userId
265+ if ( query && query . appId ) {
266+ delete query . appId ;
267+ }
268+ if ( query && query . userId ) {
269+ delete query . userId ;
270+ }
219271 // Find match
220272 Users . find ( query )
221273 . then ( function ( resp ) {
0 commit comments