Skip to content

Commit 92be203

Browse files
committed
Add query parameter support for user listing
1 parent 78036e3 commit 92be203

File tree

2 files changed

+13
-0
lines changed

2 files changed

+13
-0
lines changed

controllers/userController.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ exports.list = async function (req, res) {
1010
const user = req.user;
1111
const page = parseInt(req.query.page) || 1;
1212
const limit = parseInt(req.query.limit) || 10;
13+
const query = req.query.query || null;
1314
let includeDeleted = false;
1415
let public = true;
1516
let extraQuery = null;
@@ -22,6 +23,17 @@ exports.list = async function (req, res) {
2223
}
2324
}
2425

26+
// if there is a query
27+
if (query) {
28+
// query can be a string that is for the name or the email
29+
extraQuery = {
30+
$or: [
31+
{ name: { $regex: query, $options: 'i' } },
32+
{ email: { $regex: query, $options: 'i' } }
33+
]
34+
}
35+
}
36+
2537
// get the users
2638
const output = await UserHelper.listUsers(page, limit, extraQuery, public, includeDeleted);
2739
return res.json(output);

routes/admin/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ router.get('/users',
2828
[
2929
query('page').optional().isInt({ min: 1 }).withMessage('Page must be an integer greater than 0'),
3030
query('limit').optional().isInt({ min: 10 }).withMessage('Limit must be an integer greater than 1'),
31+
query('query').optional().isString().withMessage('Query must be a string'),
3132
query('includeDeleted').optional().isBoolean().withMessage('Include deleted must be a boolean'),
3233
],
3334
validate,

0 commit comments

Comments
 (0)