Skip to content

Commit 95c3f02

Browse files
committed
wrote the find function in user controller
1 parent 226de1f commit 95c3f02

File tree

16 files changed

+184
-31
lines changed

16 files changed

+184
-31
lines changed

CHANGELOG.md

Whitespace-only changes.

TODO.md

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,21 @@
11
### TODOs
22
| Filename | line # | TODO
33
|:------|:------:|:------
4+
| gulpfile.js | 146 | Add gulp-banner to add GNU GPL nitice on every js file.
45
| controllers/Initialize.js | 18 | Test initialize controller
5-
| controllers/Users.js | 49 | Finish users controller
6-
| controllers/Users.js | 50 | Finish users route
6+
| controllers/Users.js | 9 | Test buildProjection function
7+
| controllers/Users.js | 110 | Test limiting
8+
| controllers/Users.js | 111 | Test that response contains count of total record for the query
9+
| controllers/Users.js | 112 | Test that the last document Id in the return array of documents is in the response
10+
| controllers/Users.js | 113 | Test that sorting works
11+
| controllers/Users.js | 114 | Test that projection works
12+
| controllers/Users.js | 115 | Test that populating works
13+
| controllers/Users.js | 116 | Test that date range works
14+
| controllers/Users.js | 158 | Finish users controller
15+
| controllers/Users.js | 159 | Finish users route
716
| routes/index.js | 208 | Implement API Generator
817
| routes/initialize.js | 10 | Test initialize route
9-
| services/queue/index.js | 61 | work on a clock functionality so kue can support scheduled jobs
18+
| services/encryption/index.js | 40 | Generate checksum here
19+
| services/queue/clock.js | 11 | work on a clock functionality so kue can support scheduled jobs
20+
| services/queue/jobs.js | 75 | Add webhook Job here
21+
| services/queue/workers.js | 18 | Add Webhook worker here

config/development.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ module.exports = {
1717
letsencryptSSLVerificationBody: process.env.LETSENCRYPT_VERIFICATION_BODY || 'xvArhQBSilF4V30dGUagNAZ96ASipB0b0ex0kXn0za8._v6aFbaRYWeOmSebtlD-X4Ixf5tPsyULMsXM8HjsK-Q',
1818
maxContentLength: process.env.MAX_CONTENT_LENGTH || '9999',
1919
enforceSSL: process.env.ENFORCE_SSL || 'no',
20-
gitOAuthToken: process.env.GIT_OAUTH_TOKEN || '7cb5c1d28e92e8c1cf957d25b59e211f5a47edb0',
20+
gitOAuthToken: process.env.GIT_OAUTH_TOKEN || 'b5c90047c21b74b9dc0dbf90c2552cc5e419b9a2',
2121
queueUIUsername: process.env.QUEUE_UI_USERNAME || 'admin',
2222
queueUIPassword: process.env.QUEUE_UI_PASSWORD || 'password123/',
2323
queueUIPort: process.env.QUEUE_UI_PORT || 3000

config/production.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ module.exports = {
1717
letsencryptSSLVerificationBody: process.env.LETSENCRYPT_VERIFICATION_BODY || 'xvArhQBSilF4V30dGUagNAZ96ASipB0b0ex0kXn0za8._v6aFbaRYWeOmSebtlD-X4Ixf5tPsyULMsXM8HjsK-Q',
1818
maxContentLength: process.env.MAX_CONTENT_LENGTH || '9999',
1919
enforceSSL: process.env.ENFORCE_SSL || 'no',
20-
gitOAuthToken: process.env.GIT_OAUTH_TOKEN || '7cb5c1d28e92e8c1cf957d25b59e211f5a47edb0',
20+
gitOAuthToken: process.env.GIT_OAUTH_TOKEN || 'b5c90047c21b74b9dc0dbf90c2552cc5e419b9a2',
2121
queueUIUsername: process.env.QUEUE_UI_USERNAME || 'admin',
2222
queueUIPassword: process.env.QUEUE_UI_PASSWORD || 'password123/',
2323
queueUIPort: process.env.QUEUE_UI_PORT || 3000

controllers/Users.js

Lines changed: 113 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,122 @@
11
"use strict";
22

33
var Users = require('../models').Users;
4+
var q = require('q');
45

56
var UsersController = {};
67

7-
UsersController.find = function(query,projection,options){
8-
return Users.find(query,projection,options);
8+
UsersController.buildProjection = function(projection){
9+
// ToDo: Test buildProjection function
10+
return q.Promise(function(resolve,reject,notify){
11+
var num = projection.length;
12+
var last = num - 1;
13+
var select = {};
14+
for(var n in projection){
15+
if(typeof projection[n] === 'string'){
16+
notify('Adding '+projection[n]+' to projection');
17+
select[projection[n]] = 1;
18+
if(n === last){
19+
return resolve(select);
20+
}
21+
}else{
22+
if(n === last){
23+
return resolve(select);
24+
}
25+
}
26+
}
27+
});
28+
};
29+
30+
UsersController.find = function(req,res,next){
31+
var query = req.query;
32+
var projection = query.projection.split(',');
33+
var ourProjection;
34+
query.createdAt = {};
35+
if(projection){
36+
ourProjection = this.buildProjection(projection);
37+
delete query.projection;
38+
}
39+
var limit = query.limit;
40+
if(limit){
41+
delete query.limit;
42+
}
43+
var to = query.to;
44+
if(to){
45+
delete query.to;
46+
}
47+
var from = query.from;
48+
if(from){
49+
query.createdAt.$gt = from;
50+
delete query.from;
51+
if(!to){
52+
to = new Date().toISOString();
53+
}
54+
query.createdAt.$lt = to;
55+
}
56+
var lastId = query.lastId;
57+
if(lastId){
58+
query._id = {};
59+
query._id.$gt = lastId;
60+
delete query.lastId;
61+
}
62+
var sort = query.sort; // -fieldName: means descending while fieldName without the minus mean ascending bith by fieldName. eg, '-fieldName1 fieldName2'
63+
if(sort){
64+
delete query.sort;
65+
}
66+
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
67+
var total = Users.count(query);
68+
var question = Users.find(query);
69+
70+
if(limit){
71+
var ourLimit = limit * 1;
72+
question = question.limit(limit);
73+
}else{
74+
limit = 0;
75+
}
76+
if(sort){
77+
question = question.sort(sort);
78+
}
79+
if(populate){
80+
question = question.populate(populate);
81+
}
82+
83+
if(projection){
84+
q.all([ourProjection,total])
85+
.spread(function(resp,total){
86+
return [question.select(resp),total];
87+
})
88+
.spread(function(resp,total){
89+
var ourLastId = resp[resp.length - 1]._id;
90+
var extraData = {};
91+
extraData.limit = limit * 1;
92+
extraData.total = total;
93+
extraData.lastId = ourLastId;
94+
res.ok(resp, false, extraData);
95+
})
96+
.catch(function(err){
97+
next(err);
98+
});
99+
}else{
100+
q.all([question,total])
101+
.spread(function(resp,total){
102+
var ourLastId = resp[resp.length - 1]._id;
103+
var extraData = {};
104+
extraData.limit = limit * 1;
105+
extraData.total = total;
106+
extraData.lastId = ourLastId;
107+
res.ok(resp, false, extraData);
108+
})
109+
.catch(function(err){
110+
next(err);
111+
});
112+
// ToDo: Test limiting
113+
// ToDO: Test that response contains count of total record for the query
114+
// ToDo: Test that the last document Id in the return array of documents is in the response
115+
// ToDo: Test that sorting works
116+
// ToDo: Test that projection works
117+
// ToDo: Test that populating works
118+
// ToDo: Test that date range works
119+
}
9120
};
10121

11122
UsersController.findOne = function(id,projection,options){

gulpfile.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ gulp.task('sanity',['lint','test','todo']);
6666
// Release
6767

6868
gulp.task('changelog', function () {
69-
return gulp.src('CHANGELOG.md', {
69+
return gulp.src('./CHANGELOG.md', {
7070
buffer: false
7171
})
7272
.pipe(conventionalChangelog({
@@ -142,3 +142,5 @@ gulp.task('release', function (callback) {
142142
callback(error);
143143
});
144144
});
145+
146+
// ToDo: Add gulp-banner to add GNU GPL nitice on every js file.

models/RequestLogs.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@ Schema.pre('update', function(next) {
165165

166166
// Indexing for search
167167
var ourDoc = this._update.$set;
168+
debug('What we are updating: ', ourDoc);
168169
ourDoc.model = collection;
169170
ourDoc.update = true;
170171
debug('what do we have here: ', ourDoc);

services/encryption/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ module.exports = {
3737
debug('our key: ', key);
3838
key = aesjs.utils.hex.toBytes(key);
3939
debug('in buffer: ', key);
40-
40+
// ToDo: Generate checksum here
4141
return q.Promise(function(resolve){
4242
debug('encrypting...');
4343
debug('our key: ', key);

services/queue/clock.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// {
2+
// task
3+
// crontab
4+
// last runed at
5+
// next run at
6+
// name
7+
// enabled
8+
// arguments
9+
// }
10+
11+
// ToDo: work on a clock functionality so kue can support scheduled jobs

services/queue/index.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,5 +57,3 @@ queue.watchStuckJobs(1000);
5757
// Process Jobs Here
5858
module.exports = queue;
5959
module.exports.kue = kue;
60-
61-
// ToDo: work on a clock functionality so kue can support scheduled jobs

0 commit comments

Comments
 (0)