@@ -4,26 +4,36 @@ var Users = require('../models').Users;
4
4
var Trash = require ( '../models' ) . Trash ;
5
5
var q = require ( 'q' ) ;
6
6
var queue = require ( '../services/queue' ) ;
7
+ var debug = require ( 'debug' ) ( 'usersController' ) ;
7
8
8
9
var service = 'Users' ;
9
10
10
11
var UsersController = { } ;
11
12
12
- UsersController . buildProjection = function ( projection ) {
13
+ UsersController . buildProjection = function ( projections ) {
14
+ debug ( 'starting build...' ) ;
15
+ var projection = projections . split ( ',' ) ; // Projection should be comma separated. eg. name,location
13
16
// ToDo: Test buildProjection function
14
17
return q . Promise ( function ( resolve , reject , notify ) {
18
+ debug ( 'This is a promise...' ) ;
15
19
var num = projection . length ;
16
20
var last = num - 1 ;
17
21
var select = { } ;
18
22
for ( var n in projection ) {
19
23
if ( typeof projection [ n ] === 'string' ) {
24
+ debug ( 'Processing...' , projection [ n ] ) ;
20
25
notify ( 'Adding ' + projection [ n ] + ' to projection' ) ;
21
26
select [ projection [ n ] ] = 1 ;
22
- if ( n === last ) {
27
+ if ( n * 1 === last ) {
28
+ debug ( 'Coming out of the loop...' , select ) ;
29
+ notify ( 'Ending Build.' ) ;
23
30
return resolve ( select ) ;
24
31
}
25
32
} else {
26
- if ( n === last ) {
33
+ debug ( 'Skiping...' , projection [ n ] ) ;
34
+ if ( n * 1 === last ) {
35
+ debug ( 'Coming out of the loop......' , select ) ;
36
+ notify ( 'Ending Build..' ) ;
27
37
return resolve ( select ) ;
28
38
}
29
39
}
@@ -34,7 +44,7 @@ UsersController.buildProjection = function(projection){
34
44
UsersController . find = function ( req , res , next ) {
35
45
var query ;
36
46
if ( req . query . search ) {
37
- query = req . query . query ;
47
+ query = req . query . search ;
38
48
Users . search ( query )
39
49
. then ( function ( resp ) {
40
50
res . ok ( resp ) ;
@@ -45,26 +55,37 @@ UsersController.find = function(req,res,next){
45
55
// ToDo: Test that search works
46
56
} else {
47
57
query = req . query ;
48
- var projection = query . projection . split ( ',' ) ;
58
+ var projection = query . projection ; // Projection should be comma separated. eg. name,location
49
59
var ourProjection ;
50
- query . createdAt = { } ;
60
+
51
61
if ( projection ) {
52
62
ourProjection = this . buildProjection ( projection ) ;
53
63
delete query . projection ;
54
64
}
55
- var limit = query . limit ;
65
+ var limit = query . limit * 1 ;
56
66
if ( limit ) {
57
67
delete query . limit ;
58
68
}
59
- var to = query . to ;
60
- if ( to ) {
61
- delete query . to ;
62
- }
69
+
63
70
var from = query . from ;
71
+ var to = query . to ;
64
72
if ( from ) {
73
+ query . createdAt = { } ;
65
74
query . createdAt . $gt = from ;
66
75
delete query . from ;
67
- if ( ! to ) {
76
+ if ( to ) {
77
+ delete query . to ;
78
+ } else {
79
+ to = new Date ( ) . toISOString ( ) ;
80
+ }
81
+ query . createdAt . $lt = to ;
82
+ } else {
83
+ query . createdAt = { } ;
84
+ query . createdAt . $gt = new Date ( '1989-03-15T00:00:00' ) . toISOString ( ) ;
85
+ delete query . from ;
86
+ if ( to ) {
87
+ delete query . to ;
88
+ } else {
68
89
to = new Date ( ) . toISOString ( ) ;
69
90
}
70
91
query . createdAt . $lt = to ;
@@ -80,6 +101,9 @@ UsersController.find = function(req,res,next){
80
101
delete query . sort ;
81
102
}
82
103
var 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
104
+ if ( populate ) {
105
+ delete query . populate ;
106
+ }
83
107
var total = Users . count ( query ) ;
84
108
var question = Users . find ( query ) ;
85
109
@@ -115,7 +139,12 @@ UsersController.find = function(req,res,next){
115
139
} else {
116
140
q . all ( [ question , total ] )
117
141
. spread ( function ( resp , total ) {
118
- var ourLastId = resp [ resp . length - 1 ] . _id ;
142
+ var ourLastId ;
143
+ if ( resp . length === 0 ) {
144
+ ourLastId = null ;
145
+ } else {
146
+ ourLastId = resp [ resp . length - 1 ] . _id ;
147
+ }
119
148
var extraData = { } ;
120
149
extraData . limit = limit * 1 ;
121
150
extraData . total = total ;
@@ -203,11 +232,11 @@ UsersController.delete = function(req,res,next){
203
232
204
233
queue . create ( 'saveToTrash' , backupData )
205
234
. save ( ) ;
206
- if ( n === last ) {
235
+ if ( n * 1 === last ) {
207
236
return resp ;
208
237
}
209
238
} else {
210
- if ( n === last ) {
239
+ if ( n * 1 === last ) {
211
240
return resp ;
212
241
}
213
242
}
0 commit comments