Skip to content

Commit

Permalink
Add query parameter support for user listing
Browse files Browse the repository at this point in the history
  • Loading branch information
guillecro committed Feb 27, 2024
1 parent 78036e3 commit 92be203
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
12 changes: 12 additions & 0 deletions controllers/userController.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ exports.list = async function (req, res) {
const user = req.user;
const page = parseInt(req.query.page) || 1;
const limit = parseInt(req.query.limit) || 10;
const query = req.query.query || null;
let includeDeleted = false;
let public = true;
let extraQuery = null;
Expand All @@ -22,6 +23,17 @@ exports.list = async function (req, res) {
}
}

// if there is a query
if (query) {
// query can be a string that is for the name or the email
extraQuery = {
$or: [
{ name: { $regex: query, $options: 'i' } },
{ email: { $regex: query, $options: 'i' } }
]
}
}

// get the users
const output = await UserHelper.listUsers(page, limit, extraQuery, public, includeDeleted);
return res.json(output);
Expand Down
1 change: 1 addition & 0 deletions routes/admin/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ router.get('/users',
[
query('page').optional().isInt({ min: 1 }).withMessage('Page must be an integer greater than 0'),
query('limit').optional().isInt({ min: 10 }).withMessage('Limit must be an integer greater than 1'),
query('query').optional().isString().withMessage('Query must be a string'),
query('includeDeleted').optional().isBoolean().withMessage('Include deleted must be a boolean'),
],
validate,
Expand Down

0 comments on commit 92be203

Please sign in to comment.