Skip to content

Commit d7bd6d4

Browse files
committed
added user routes and started generating checksum for encrypted data
1 parent 53fc73f commit d7bd6d4

File tree

11 files changed

+210
-185
lines changed

11 files changed

+210
-185
lines changed

TODO.md

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,24 @@
11
### TODOs
22
| Filename | line # | TODO
33
|:------|:------:|:------
4-
| gulpfile.js | 146 | Add gulp-banner to add GNU GPL notice on every js file.
54
| controllers/Initialize.js | 18 | Test initialize controller
65
| controllers/Users.js | 13 | Test buildProjection function
7-
| controllers/Users.js | 116 | Test limiting
8-
| controllers/Users.js | 117 | Test that response contains count of total record for the query
9-
| controllers/Users.js | 118 | Test that the last document Id in the return array of documents is in the response
10-
| controllers/Users.js | 119 | Test that sorting works
11-
| controllers/Users.js | 120 | Test that projection works
12-
| controllers/Users.js | 121 | Test that populating works
13-
| controllers/Users.js | 122 | Test that date range works
14-
| controllers/Users.js | 295 | Test users controller
15-
| controllers/Users.js | 296 | Finish users route
16-
| controllers/Users.js | 297 | Test that any deleted data is backed up
6+
| controllers/Users.js | 45 | Test that search works
7+
| controllers/Users.js | 128 | Test limiting
8+
| controllers/Users.js | 129 | Test that response contains count of total record for the query
9+
| controllers/Users.js | 130 | Test that the last document Id in the return array of documents is in the response
10+
| controllers/Users.js | 131 | Test that sorting works
11+
| controllers/Users.js | 132 | Test that projection works
12+
| controllers/Users.js | 133 | Test that populating works
13+
| controllers/Users.js | 134 | Test that date range works
14+
| controllers/Users.js | 284 | Test users controller
15+
| controllers/Users.js | 285 | Test that any deleted data is backed up
1716
| routes/index.js | 214 | Implement API Generator
1817
| routes/initialize.js | 10 | Test initialize route
18+
| routes/users.js | 43 | Test users route
1919
| services/queue/clock.js | 11 | work on a clock functionality so kue can support scheduled jobs
2020
| services/queue/jobs.js | 80 | Test saveToTrash job
2121
| services/queue/jobs.js | 93 | Test Webhook Event
22-
| services/queue/jobs.js | 137 | Test Secure Webhooks
23-
| services/queue/jobs.js | 144 | Test Unsecure Webhooks
24-
| services/queue/jobs.js | 169 | Test sendHTTPRequest Job
25-
| services/encryption/index.js | 40 | Generate checksum here
22+
| services/queue/jobs.js | 133 | Test Secure Webhooks
23+
| services/queue/jobs.js | 140 | Test Unsecure Webhooks
24+
| services/queue/jobs.js | 165 | Test sendHTTPRequest Job

controllers/Users.js

Lines changed: 102 additions & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -32,99 +32,113 @@ UsersController.buildProjection = function(projection){
3232
};
3333

3434
UsersController.find = function(req,res,next){
35-
var query = req.query;
36-
var projection = query.projection.split(',');
37-
var ourProjection;
38-
query.createdAt = {};
39-
if(projection){
40-
ourProjection = this.buildProjection(projection);
41-
delete query.projection;
42-
}
43-
var limit = query.limit;
44-
if(limit){
45-
delete query.limit;
46-
}
47-
var to = query.to;
48-
if(to){
49-
delete query.to;
50-
}
51-
var from = query.from;
52-
if(from){
53-
query.createdAt.$gt = from;
54-
delete query.from;
55-
if(!to){
56-
to = new Date().toISOString();
57-
}
58-
query.createdAt.$lt = to;
59-
}
60-
var lastId = query.lastId;
61-
if(lastId){
62-
query._id = {};
63-
query._id.$gt = lastId;
64-
delete query.lastId;
65-
}
66-
var sort = query.sort; // -fieldName: means descending while fieldName without the minus mean ascending bith by fieldName. eg, '-fieldName1 fieldName2'
67-
if(sort){
68-
delete query.sort;
69-
}
70-
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
71-
var total = Users.count(query);
72-
var question = Users.find(query);
73-
74-
if(limit){
75-
var ourLimit = limit * 1;
76-
question = question.limit(limit);
77-
}else{
78-
limit = 0;
79-
}
80-
if(sort){
81-
question = question.sort(sort);
82-
}
83-
if(populate){
84-
question = question.populate(populate);
85-
}
86-
87-
if(projection){
88-
q.all([ourProjection,total])
89-
.spread(function(resp,total){
90-
return [question.select(resp),total];
91-
})
92-
.spread(function(resp,total){
93-
var ourLastId = resp[resp.length - 1]._id;
94-
var extraData = {};
95-
extraData.limit = limit * 1;
96-
extraData.total = total;
97-
extraData.lastId = ourLastId;
98-
res.ok(resp, false, extraData);
35+
var query;
36+
if(req.query.search){
37+
query = req.query.query;
38+
Users.search(query)
39+
.then(function(resp){
40+
res.ok(resp);
9941
})
10042
.catch(function(err){
10143
next(err);
10244
});
45+
// ToDo: Test that search works
10346
}else{
104-
q.all([question,total])
105-
.spread(function(resp,total){
106-
var ourLastId = resp[resp.length - 1]._id;
107-
var extraData = {};
108-
extraData.limit = limit * 1;
109-
extraData.total = total;
110-
extraData.lastId = ourLastId;
111-
res.ok(resp, false, extraData);
112-
})
113-
.catch(function(err){
114-
next(err);
115-
});
116-
// ToDo: Test limiting
117-
// ToDO: Test that response contains count of total record for the query
118-
// ToDo: Test that the last document Id in the return array of documents is in the response
119-
// ToDo: Test that sorting works
120-
// ToDo: Test that projection works
121-
// ToDo: Test that populating works
122-
// ToDo: Test that date range works
47+
query = req.query;
48+
var projection = query.projection.split(',');
49+
var ourProjection;
50+
query.createdAt = {};
51+
if(projection){
52+
ourProjection = this.buildProjection(projection);
53+
delete query.projection;
54+
}
55+
var limit = query.limit;
56+
if(limit){
57+
delete query.limit;
58+
}
59+
var to = query.to;
60+
if(to){
61+
delete query.to;
62+
}
63+
var from = query.from;
64+
if(from){
65+
query.createdAt.$gt = from;
66+
delete query.from;
67+
if(!to){
68+
to = new Date().toISOString();
69+
}
70+
query.createdAt.$lt = to;
71+
}
72+
var lastId = query.lastId;
73+
if(lastId){
74+
query._id = {};
75+
query._id.$gt = lastId;
76+
delete query.lastId;
77+
}
78+
var sort = query.sort; // -fieldName: means descending while fieldName without the minus mean ascending bith by fieldName. eg, '-fieldName1 fieldName2'
79+
if(sort){
80+
delete query.sort;
81+
}
82+
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
83+
var total = Users.count(query);
84+
var question = Users.find(query);
85+
86+
if(limit){
87+
var ourLimit = limit * 1;
88+
question = question.limit(limit);
89+
}else{
90+
limit = 0;
91+
}
92+
if(sort){
93+
question = question.sort(sort);
94+
}
95+
if(populate){
96+
question = question.populate(populate);
97+
}
98+
99+
if(projection){
100+
q.all([ourProjection,total])
101+
.spread(function(resp,total){
102+
return [question.select(resp),total];
103+
})
104+
.spread(function(resp,total){
105+
var ourLastId = resp[resp.length - 1]._id;
106+
var extraData = {};
107+
extraData.limit = limit * 1;
108+
extraData.total = total;
109+
extraData.lastId = ourLastId;
110+
res.ok(resp, false, extraData);
111+
})
112+
.catch(function(err){
113+
next(err);
114+
});
115+
}else{
116+
q.all([question,total])
117+
.spread(function(resp,total){
118+
var ourLastId = resp[resp.length - 1]._id;
119+
var extraData = {};
120+
extraData.limit = limit * 1;
121+
extraData.total = total;
122+
extraData.lastId = ourLastId;
123+
res.ok(resp, false, extraData);
124+
})
125+
.catch(function(err){
126+
next(err);
127+
});
128+
// ToDo: Test limiting
129+
// ToDO: Test that response contains count of total record for the query
130+
// ToDo: Test that the last document Id in the return array of documents is in the response
131+
// ToDo: Test that sorting works
132+
// ToDo: Test that projection works
133+
// ToDo: Test that populating works
134+
// ToDo: Test that date range works
135+
}
136+
123137
}
124138
};
125139

126140
UsersController.findOne = function(req,res,next){
127-
var id = req.query.id;
141+
var id = req.params.id;
128142
Users.findById(id)
129143
.then(function(resp){
130144
res.ok(resp);
@@ -134,17 +148,6 @@ UsersController.findOne = function(req,res,next){
134148
});
135149
};
136150

137-
UsersController.search = function(req,res,next){
138-
var query = req.query.query;
139-
Users.search(query)
140-
.then(function(resp){
141-
res.ok(resp);
142-
})
143-
.catch(function(err){
144-
next(err);
145-
});
146-
};
147-
148151
UsersController.create = function(req,res,next){
149152
var data = req.body;
150153
Users.create(data)
@@ -159,7 +162,7 @@ UsersController.create = function(req,res,next){
159162
UsersController.update = function(req,res,next){
160163
var query = req.query;
161164
var data = req.body;
162-
Users.update(query,data)
165+
Users.updateMany(query,data)
163166
.then(function(resp){
164167
res.ok(resp);
165168
})
@@ -169,7 +172,7 @@ UsersController.update = function(req,res,next){
169172
};
170173

171174
UsersController.updateOne = function(req,res,next){
172-
var id = req.query.id;
175+
var id = req.params.id;
173176
var data = req.body;
174177
Users.findByIdAndUpdate(id,data)
175178
.then(function(resp){
@@ -180,20 +183,6 @@ UsersController.updateOne = function(req,res,next){
180183
});
181184
};
182185

183-
UsersController.count = function(req,res,next){
184-
var query = req.query;
185-
if(!query){
186-
query = {};
187-
}
188-
Users.count(query)
189-
.then(function(resp){
190-
res.ok(resp);
191-
})
192-
.catch(function(err){
193-
next(err);
194-
});
195-
};
196-
197186
UsersController.delete = function(req,res,next){
198187
var query = req.query;
199188
// Find match
@@ -237,7 +226,7 @@ UsersController.delete = function(req,res,next){
237226
};
238227

239228
UsersController.deleteOne = function(req,res,next){
240-
var id = req.query.id;
229+
var id = req.params.id;
241230
// Find match
242231
Users.findById(id)
243232
.then(function(resp){
@@ -267,7 +256,7 @@ UsersController.deleteOne = function(req,res,next){
267256
};
268257

269258
UsersController.restore = function(req,res,next){
270-
var id = req.query.id;
259+
var id = req.params.id;
271260
// Find data by ID from trash
272261
Trash.findById(id)
273262
.then(function(resp){
@@ -293,5 +282,4 @@ UsersController.restore = function(req,res,next){
293282
module.exports = UsersController;
294283

295284
// Todo: Test users controller
296-
// Todo: Finish users route
297285
// ToDo: Test that any deleted data is backed up

gulpfile.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,5 +142,3 @@ gulp.task('release', function (callback) {
142142
callback(error);
143143
});
144144
});
145-
146-
// ToDo: Add gulp-banner to add GNU GPL notice on every js file.

routes/users.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,43 @@
11
"use strict";
2+
var express = require('express');
3+
var router = express.Router();
4+
var usersController = require('../controllers/Users');
5+
6+
var service = 'users';
7+
8+
// get users or search users
9+
router.get('/'+service, usersController.find);
10+
11+
// get user
12+
router.get('/'+service+'/:id', usersController.findOne);
13+
14+
// To add validation, add a middlewave like the below. Works for just POST calls only
15+
// function(req,res,next){
16+
// req._required = [ // _required should contain all the fails that are required
17+
// 'name',
18+
// 'name2'
19+
// ];
20+
21+
// next();
22+
// }
23+
24+
// create user(s) a single user object will create one user while an array of users will create multiple users
25+
router.post('/'+service, usersController.create);
26+
27+
// update all records that matches the query
28+
router.put('/'+service, usersController.updateOne);
29+
30+
// update a single record
31+
router.put('/'+service+'/:id', usersController.findOne);
32+
33+
// delete all records that matches the query
34+
router.delete('/'+service, usersController.delete);
35+
36+
// Delete a single record
37+
router.delete('/'+service+'/:id', usersController.deleteOne);
38+
39+
// restore a previously deleted record
40+
router.post('/'+service+'/:id/restore', usersController.restore);
41+
42+
module.exports = router;
43+
// Todo: Test users route

0 commit comments

Comments
 (0)