From be25e2abe90181576898efa2d34d5c89535f11a1 Mon Sep 17 00:00:00 2001 From: santiago ferlatti Date: Wed, 4 Dec 2019 13:01:30 -0300 Subject: [PATCH 1/4] first mini patch --- server/src/migration/migrate.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/server/src/migration/migrate.js b/server/src/migration/migrate.js index d6fb4e3..5073e37 100644 --- a/server/src/migration/migrate.js +++ b/server/src/migration/migrate.js @@ -12,7 +12,7 @@ async function run() { const migration = require(migrationsUrl + file); if( await isMigrated(file)) { await migration.up(); - migrationsModel.create({name: file}); + await migrationsModel.create({name: file}); } else { console.log("The migration " + file + " is already up"); } From c80802b4ccc75677b34b7dcd7379dd0f1bdfecf5 Mon Sep 17 00:00:00 2001 From: santiago ferlatti Date: Wed, 4 Dec 2019 13:08:15 -0300 Subject: [PATCH 2/4] first try of forking --- server/src/models/user.model.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/server/src/models/user.model.js b/server/src/models/user.model.js index 46fd07a..63bd368 100644 --- a/server/src/models/user.model.js +++ b/server/src/models/user.model.js @@ -56,7 +56,7 @@ const userValidation = Joi.object().keys({ __v: Joi.any(), username: Joi.string().min(3).max(15).required(), email: argenissEmail.required(), - password: Joi.string().min(6).max(16), + password: Joi.string().min(6).max(16).required(), active: Joi.boolean(), name: Joi.string().min(2).max(50).regex(notNumbers).required(), surname: Joi.string().min(2).max(50).regex(notNumbers).required(), From fc77db4f785dc6805b4173ddc6536710cf48d0b5 Mon Sep 17 00:00:00 2001 From: santiago ferlatti Date: Tue, 24 Dec 2019 10:11:52 -0300 Subject: [PATCH 3/4] feature/my-clients --- .../src/controllers/myclients.controller.js | 32 +++++++++++++++++++ server/src/database.js | 2 +- server/src/repositories/user.repository.js | 11 +++++-- server/src/routes/routes.config.js | 4 ++- 4 files changed, 44 insertions(+), 5 deletions(-) create mode 100644 server/src/controllers/myclients.controller.js diff --git a/server/src/controllers/myclients.controller.js b/server/src/controllers/myclients.controller.js new file mode 100644 index 0000000..f3b1271 --- /dev/null +++ b/server/src/controllers/myclients.controller.js @@ -0,0 +1,32 @@ +const CrudRestController = require('./crud-rest.controller'); +const UserRepository = require('../repositories/user.repository'); + +class MyClientsController extends CrudRestController { + + /** + * Constructor + * @param {string} basePath + * @param {parentRouter} parentRouter + */ + constructor(basePath, parentRouter) { + super(basePath, parentRouter, new UserRepository()); + } + + registerRoutes() { + this.router.get('/page/:pageNum/size/:pageSize', this.getMyClients.bind(this)); + } + + async getMyClients(req, res) { + try { + const pageNum = req.params.pageNum; + const pageSize = req.params.pageSize; + const id = req.user._id; + const data = await this.repository.findUserClients(id, pageNum, pageSize); + this._success(res, data); + } catch (e) { + this._error(res, e); + } + } +} + +module.exports = MyClientsController; \ No newline at end of file diff --git a/server/src/database.js b/server/src/database.js index 5a8efb6..89d3205 100644 --- a/server/src/database.js +++ b/server/src/database.js @@ -16,7 +16,7 @@ exports.initializeMongo = function () { const db = mongoose.connection; db.on('error', console.error.bind(console, 'Connection error')); db.once('open', function() { - console.log("Connection successfull"); + console.log("Connection successful"); }); return db; diff --git a/server/src/repositories/user.repository.js b/server/src/repositories/user.repository.js index ad0f16b..08194f7 100644 --- a/server/src/repositories/user.repository.js +++ b/server/src/repositories/user.repository.js @@ -17,9 +17,14 @@ class UsersRepository extends MongooseRepository { * @param {number} pageNum - amount of records to skip * @param {number} pageSize - amount of records to return */ - findUserClients(id, pageNum, pageSize) { - const query = super.paginationQueryOptions(pageNum, pageSize); - return this.model.findById(id, 'clients').populate({ path: 'clients', options: query }).lean().exec(); + async findUserClients(id, pageNum, pageSize) { + const query = super.paginationQueryOptions(pageNum, pageSize, {}); + const clients = await this.model.findById(id, 'clients').populate({ path: 'clients', options: query }).lean().exec(); + + return { + list: clients.clients || [], + count: await this.model.countDocuments(query) + } } /** diff --git a/server/src/routes/routes.config.js b/server/src/routes/routes.config.js index 7a3527f..875d349 100644 --- a/server/src/routes/routes.config.js +++ b/server/src/routes/routes.config.js @@ -6,12 +6,14 @@ const ClientsController = require('../controllers/clients.controller'); const MeController = require('../controllers/me.controller'); const UsersController = require('../controllers/users.controller'); const RolesController = require('../controllers/roles.controller'); - +const MyClientsController = require('../controllers/myclients.controller'); new AuthController('/auth', router); new ClientsController('/clients', router); new MeController('/me', router); new UsersController('/users', router); new RolesController('/roles', router); +new MyClientsController('/myclients', router); + module.exports = router From 64af0788fb47003b6cc56d084b8503859436a756 Mon Sep 17 00:00:00 2001 From: santiago ferlatti Date: Fri, 27 Dec 2019 12:33:12 -0300 Subject: [PATCH 4/4] pagination alpha --- server/src/controllers/myclients.controller.js | 5 +++-- server/src/repositories/user.repository.js | 17 ++++++++++++----- 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/server/src/controllers/myclients.controller.js b/server/src/controllers/myclients.controller.js index f3b1271..98b288a 100644 --- a/server/src/controllers/myclients.controller.js +++ b/server/src/controllers/myclients.controller.js @@ -15,13 +15,14 @@ class MyClientsController extends CrudRestController { registerRoutes() { this.router.get('/page/:pageNum/size/:pageSize', this.getMyClients.bind(this)); } - + async getMyClients(req, res) { try { const pageNum = req.params.pageNum; const pageSize = req.params.pageSize; const id = req.user._id; - const data = await this.repository.findUserClients(id, pageNum, pageSize); + const search = req.query.search; + const data = await this.repository.findUserClients(id, pageNum, pageSize, search); this._success(res, data); } catch (e) { this._error(res, e); diff --git a/server/src/repositories/user.repository.js b/server/src/repositories/user.repository.js index 08194f7..1b69edb 100644 --- a/server/src/repositories/user.repository.js +++ b/server/src/repositories/user.repository.js @@ -17,13 +17,20 @@ class UsersRepository extends MongooseRepository { * @param {number} pageNum - amount of records to skip * @param {number} pageSize - amount of records to return */ - async findUserClients(id, pageNum, pageSize) { - const query = super.paginationQueryOptions(pageNum, pageSize, {}); - const clients = await this.model.findById(id, 'clients').populate({ path: 'clients', options: query }).lean().exec(); - + + async findUserClients(id, pageNum, pageSize, search, query = {active: true}) { + this.queryFields = '-'; + const myfieldsSearch = ['name', 'email', 'contactName']; + const options = {}; + super.paginationQueryOptions(pageNum, pageSize, options); + if (search) super.searchQueryOptions(search, myfieldsSearch, query); + + const clients = await this.model.findById(id, 'clients').populate({ path: 'clients', options: options, match: query}).lean().exec(); + const count = await this.model.findById(id, 'clients').populate({ path: 'clients', match: query}).lean().exec(); + return { list: clients.clients || [], - count: await this.model.countDocuments(query) + count: count.clients.length } }