@@ -4,26 +4,36 @@ var Users = require('../models').Users;
44var Trash = require ( '../models' ) . Trash ;
55var q = require ( 'q' ) ;
66var queue = require ( '../services/queue' ) ;
7+ var debug = require ( 'debug' ) ( 'usersController' ) ;
78
89var service = 'Users' ;
910
1011var UsersController = { } ;
1112
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
1316 // ToDo: Test buildProjection function
1417 return q . Promise ( function ( resolve , reject , notify ) {
18+ debug ( 'This is a promise...' ) ;
1519 var num = projection . length ;
1620 var last = num - 1 ;
1721 var select = { } ;
1822 for ( var n in projection ) {
1923 if ( typeof projection [ n ] === 'string' ) {
24+ debug ( 'Processing...' , projection [ n ] ) ;
2025 notify ( 'Adding ' + projection [ n ] + ' to projection' ) ;
2126 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.' ) ;
2330 return resolve ( select ) ;
2431 }
2532 } 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..' ) ;
2737 return resolve ( select ) ;
2838 }
2939 }
@@ -34,7 +44,7 @@ UsersController.buildProjection = function(projection){
3444UsersController . find = function ( req , res , next ) {
3545 var query ;
3646 if ( req . query . search ) {
37- query = req . query . query ;
47+ query = req . query . search ;
3848 Users . search ( query )
3949 . then ( function ( resp ) {
4050 res . ok ( resp ) ;
@@ -45,26 +55,37 @@ UsersController.find = function(req,res,next){
4555 // ToDo: Test that search works
4656 } else {
4757 query = req . query ;
48- var projection = query . projection . split ( ',' ) ;
58+ var projection = query . projection ; // Projection should be comma separated. eg. name,location
4959 var ourProjection ;
50- query . createdAt = { } ;
60+
5161 if ( projection ) {
5262 ourProjection = this . buildProjection ( projection ) ;
5363 delete query . projection ;
5464 }
55- var limit = query . limit ;
65+ var limit = query . limit * 1 ;
5666 if ( limit ) {
5767 delete query . limit ;
5868 }
59- var to = query . to ;
60- if ( to ) {
61- delete query . to ;
62- }
69+
6370 var from = query . from ;
71+ var to = query . to ;
6472 if ( from ) {
73+ query . createdAt = { } ;
6574 query . createdAt . $gt = from ;
6675 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 {
6889 to = new Date ( ) . toISOString ( ) ;
6990 }
7091 query . createdAt . $lt = to ;
@@ -80,6 +101,9 @@ UsersController.find = function(req,res,next){
80101 delete query . sort ;
81102 }
82103 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+ }
83107 var total = Users . count ( query ) ;
84108 var question = Users . find ( query ) ;
85109
@@ -115,7 +139,12 @@ UsersController.find = function(req,res,next){
115139 } else {
116140 q . all ( [ question , total ] )
117141 . 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+ }
119148 var extraData = { } ;
120149 extraData . limit = limit * 1 ;
121150 extraData . total = total ;
@@ -203,11 +232,11 @@ UsersController.delete = function(req,res,next){
203232
204233 queue . create ( 'saveToTrash' , backupData )
205234 . save ( ) ;
206- if ( n === last ) {
235+ if ( n * 1 === last ) {
207236 return resp ;
208237 }
209238 } else {
210- if ( n === last ) {
239+ if ( n * 1 === last ) {
211240 return resp ;
212241 }
213242 }
0 commit comments