Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions server/src/controllers/myclients.controller.js
Original file line number Diff line number Diff line change
@@ -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;
2 changes: 1 addition & 1 deletion server/src/database.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion server/src/models/user.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
18 changes: 15 additions & 3 deletions server/src/repositories/user.repository.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

/**
Expand Down
4 changes: 3 additions & 1 deletion server/src/routes/routes.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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