Skip to content

Commit

Permalink
added user routes and started generating checksum for encrypted data
Browse files Browse the repository at this point in the history
  • Loading branch information
iolufemi committed Aug 17, 2017
1 parent 53fc73f commit d7bd6d4
Show file tree
Hide file tree
Showing 11 changed files with 210 additions and 185 deletions.
29 changes: 14 additions & 15 deletions TODO.md
Original file line number Diff line number Diff line change
@@ -1,25 +1,24 @@
### TODOs
| Filename | line # | TODO
|:------|:------:|:------
| gulpfile.js | 146 | Add gulp-banner to add GNU GPL notice on every js file.
| controllers/Initialize.js | 18 | Test initialize controller
| controllers/Users.js | 13 | Test buildProjection function
| controllers/Users.js | 116 | Test limiting
| controllers/Users.js | 117 | Test that response contains count of total record for the query
| controllers/Users.js | 118 | Test that the last document Id in the return array of documents is in the response
| controllers/Users.js | 119 | Test that sorting works
| controllers/Users.js | 120 | Test that projection works
| controllers/Users.js | 121 | Test that populating works
| controllers/Users.js | 122 | Test that date range works
| controllers/Users.js | 295 | Test users controller
| controllers/Users.js | 296 | Finish users route
| controllers/Users.js | 297 | Test that any deleted data is backed up
| controllers/Users.js | 45 | Test that search works
| controllers/Users.js | 128 | Test limiting
| controllers/Users.js | 129 | Test that response contains count of total record for the query
| controllers/Users.js | 130 | Test that the last document Id in the return array of documents is in the response
| controllers/Users.js | 131 | Test that sorting works
| controllers/Users.js | 132 | Test that projection works
| controllers/Users.js | 133 | Test that populating works
| controllers/Users.js | 134 | Test that date range works
| controllers/Users.js | 284 | Test users controller
| controllers/Users.js | 285 | Test that any deleted data is backed up
| routes/index.js | 214 | Implement API Generator
| routes/initialize.js | 10 | Test initialize route
| routes/users.js | 43 | Test users route
| services/queue/clock.js | 11 | work on a clock functionality so kue can support scheduled jobs
| services/queue/jobs.js | 80 | Test saveToTrash job
| services/queue/jobs.js | 93 | Test Webhook Event
| services/queue/jobs.js | 137 | Test Secure Webhooks
| services/queue/jobs.js | 144 | Test Unsecure Webhooks
| services/queue/jobs.js | 169 | Test sendHTTPRequest Job
| services/encryption/index.js | 40 | Generate checksum here
| services/queue/jobs.js | 133 | Test Secure Webhooks
| services/queue/jobs.js | 140 | Test Unsecure Webhooks
| services/queue/jobs.js | 165 | Test sendHTTPRequest Job
216 changes: 102 additions & 114 deletions controllers/Users.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,99 +32,113 @@ UsersController.buildProjection = function(projection){
};

UsersController.find = function(req,res,next){
var query = req.query;
var projection = query.projection.split(',');
var ourProjection;
query.createdAt = {};
if(projection){
ourProjection = this.buildProjection(projection);
delete query.projection;
}
var limit = query.limit;
if(limit){
delete query.limit;
}
var to = query.to;
if(to){
delete query.to;
}
var from = query.from;
if(from){
query.createdAt.$gt = from;
delete query.from;
if(!to){
to = new Date().toISOString();
}
query.createdAt.$lt = to;
}
var lastId = query.lastId;
if(lastId){
query._id = {};
query._id.$gt = lastId;
delete query.lastId;
}
var sort = query.sort; // -fieldName: means descending while fieldName without the minus mean ascending bith by fieldName. eg, '-fieldName1 fieldName2'
if(sort){
delete query.sort;
}
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
var total = Users.count(query);
var question = Users.find(query);

if(limit){
var ourLimit = limit * 1;
question = question.limit(limit);
}else{
limit = 0;
}
if(sort){
question = question.sort(sort);
}
if(populate){
question = question.populate(populate);
}

if(projection){
q.all([ourProjection,total])
.spread(function(resp,total){
return [question.select(resp),total];
})
.spread(function(resp,total){
var ourLastId = resp[resp.length - 1]._id;
var extraData = {};
extraData.limit = limit * 1;
extraData.total = total;
extraData.lastId = ourLastId;
res.ok(resp, false, extraData);
var query;
if(req.query.search){
query = req.query.query;
Users.search(query)
.then(function(resp){
res.ok(resp);
})
.catch(function(err){
next(err);
});
// ToDo: Test that search works
}else{
q.all([question,total])
.spread(function(resp,total){
var ourLastId = resp[resp.length - 1]._id;
var extraData = {};
extraData.limit = limit * 1;
extraData.total = total;
extraData.lastId = ourLastId;
res.ok(resp, false, extraData);
})
.catch(function(err){
next(err);
});
// ToDo: Test limiting
// ToDO: Test that response contains count of total record for the query
// ToDo: Test that the last document Id in the return array of documents is in the response
// ToDo: Test that sorting works
// ToDo: Test that projection works
// ToDo: Test that populating works
// ToDo: Test that date range works
query = req.query;
var projection = query.projection.split(',');
var ourProjection;
query.createdAt = {};
if(projection){
ourProjection = this.buildProjection(projection);
delete query.projection;
}
var limit = query.limit;
if(limit){
delete query.limit;
}
var to = query.to;
if(to){
delete query.to;
}
var from = query.from;
if(from){
query.createdAt.$gt = from;
delete query.from;
if(!to){
to = new Date().toISOString();
}
query.createdAt.$lt = to;
}
var lastId = query.lastId;
if(lastId){
query._id = {};
query._id.$gt = lastId;
delete query.lastId;
}
var sort = query.sort; // -fieldName: means descending while fieldName without the minus mean ascending bith by fieldName. eg, '-fieldName1 fieldName2'
if(sort){
delete query.sort;
}
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
var total = Users.count(query);
var question = Users.find(query);

if(limit){
var ourLimit = limit * 1;
question = question.limit(limit);
}else{
limit = 0;
}
if(sort){
question = question.sort(sort);
}
if(populate){
question = question.populate(populate);
}

if(projection){
q.all([ourProjection,total])
.spread(function(resp,total){
return [question.select(resp),total];
})
.spread(function(resp,total){
var ourLastId = resp[resp.length - 1]._id;
var extraData = {};
extraData.limit = limit * 1;
extraData.total = total;
extraData.lastId = ourLastId;
res.ok(resp, false, extraData);
})
.catch(function(err){
next(err);
});
}else{
q.all([question,total])
.spread(function(resp,total){
var ourLastId = resp[resp.length - 1]._id;
var extraData = {};
extraData.limit = limit * 1;
extraData.total = total;
extraData.lastId = ourLastId;
res.ok(resp, false, extraData);
})
.catch(function(err){
next(err);
});
// ToDo: Test limiting
// ToDO: Test that response contains count of total record for the query
// ToDo: Test that the last document Id in the return array of documents is in the response
// ToDo: Test that sorting works
// ToDo: Test that projection works
// ToDo: Test that populating works
// ToDo: Test that date range works
}

}
};

UsersController.findOne = function(req,res,next){
var id = req.query.id;
var id = req.params.id;
Users.findById(id)
.then(function(resp){
res.ok(resp);
Expand All @@ -134,17 +148,6 @@ UsersController.findOne = function(req,res,next){
});
};

UsersController.search = function(req,res,next){
var query = req.query.query;
Users.search(query)
.then(function(resp){
res.ok(resp);
})
.catch(function(err){
next(err);
});
};

UsersController.create = function(req,res,next){
var data = req.body;
Users.create(data)
Expand All @@ -159,7 +162,7 @@ UsersController.create = function(req,res,next){
UsersController.update = function(req,res,next){
var query = req.query;
var data = req.body;
Users.update(query,data)
Users.updateMany(query,data)
.then(function(resp){
res.ok(resp);
})
Expand All @@ -169,7 +172,7 @@ UsersController.update = function(req,res,next){
};

UsersController.updateOne = function(req,res,next){
var id = req.query.id;
var id = req.params.id;
var data = req.body;
Users.findByIdAndUpdate(id,data)
.then(function(resp){
Expand All @@ -180,20 +183,6 @@ UsersController.updateOne = function(req,res,next){
});
};

UsersController.count = function(req,res,next){
var query = req.query;
if(!query){
query = {};
}
Users.count(query)
.then(function(resp){
res.ok(resp);
})
.catch(function(err){
next(err);
});
};

UsersController.delete = function(req,res,next){
var query = req.query;
// Find match
Expand Down Expand Up @@ -237,7 +226,7 @@ UsersController.delete = function(req,res,next){
};

UsersController.deleteOne = function(req,res,next){
var id = req.query.id;
var id = req.params.id;
// Find match
Users.findById(id)
.then(function(resp){
Expand Down Expand Up @@ -267,7 +256,7 @@ UsersController.deleteOne = function(req,res,next){
};

UsersController.restore = function(req,res,next){
var id = req.query.id;
var id = req.params.id;
// Find data by ID from trash
Trash.findById(id)
.then(function(resp){
Expand All @@ -293,5 +282,4 @@ UsersController.restore = function(req,res,next){
module.exports = UsersController;

// Todo: Test users controller
// Todo: Finish users route
// ToDo: Test that any deleted data is backed up
2 changes: 0 additions & 2 deletions gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,5 +142,3 @@ gulp.task('release', function (callback) {
callback(error);
});
});

// ToDo: Add gulp-banner to add GNU GPL notice on every js file.
42 changes: 42 additions & 0 deletions routes/users.js
Original file line number Diff line number Diff line change
@@ -1 +1,43 @@
"use strict";
var express = require('express');
var router = express.Router();
var usersController = require('../controllers/Users');

var service = 'users';

// get users or search users
router.get('/'+service, usersController.find);

// get user
router.get('/'+service+'/:id', usersController.findOne);

// To add validation, add a middlewave like the below. Works for just POST calls only
// function(req,res,next){
// req._required = [ // _required should contain all the fails that are required
// 'name',
// 'name2'
// ];

// next();
// }

// create user(s) a single user object will create one user while an array of users will create multiple users
router.post('/'+service, usersController.create);

// update all records that matches the query
router.put('/'+service, usersController.updateOne);

// update a single record
router.put('/'+service+'/:id', usersController.findOne);

// delete all records that matches the query
router.delete('/'+service, usersController.delete);

// Delete a single record
router.delete('/'+service+'/:id', usersController.deleteOne);

// restore a previously deleted record
router.post('/'+service+'/:id/restore', usersController.restore);

module.exports = router;
// Todo: Test users route
Loading

0 comments on commit d7bd6d4

Please sign in to comment.