diff --git a/server/src/controllers/myclients.controller.js b/server/src/controllers/myclients.controller.js new file mode 100644 index 0000000..98b288a --- /dev/null +++ b/server/src/controllers/myclients.controller.js @@ -0,0 +1,33 @@ +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 search = req.query.search; + const data = await this.repository.findUserClients(id, pageNum, pageSize, search); + 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/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(), diff --git a/server/src/repositories/user.repository.js b/server/src/repositories/user.repository.js index ad0f16b..1b69edb 100644 --- a/server/src/repositories/user.repository.js +++ b/server/src/repositories/user.repository.js @@ -17,9 +17,21 @@ 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, 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: count.clients.length + } } /** 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