Skip to content
This repository has been archived by the owner on Oct 14, 2020. It is now read-only.

Commit

Permalink
Merge pull request #388 from RealEskalate/lydia.people
Browse files Browse the repository at this point in the history
[backend] Activate route to get all users
  • Loading branch information
LydiaGA authored Aug 26, 2020
2 parents 214021c + 6167961 commit 884e0bf
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 4 deletions.
45 changes: 42 additions & 3 deletions Backend/controllers/UserController.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ const jwt = require("jsonwebtoken");
const axios = require("axios");
const User = UserModels.User;
const emailSender = require("../services/EmailSender");
const { LocationUser } = require("../models/LocationUserModel.js");
const { DistrictModel } = require("../models/DistrictModel.js");

// Get All Users. - [DEPRECATED: The information is too sensitive to share with API consumers]
exports.get_all_users = async (req, res) => {
if (req.query.demo && req.query.demo == "true") {
var User = UserModels.DemoUser;
Expand All @@ -15,9 +16,47 @@ exports.get_all_users = async (req, res) => {
} else {
var User = UserModels.User;
}
const users = await User.find();

let filter = {};

if(req.query.username){
filter.username = {$regex: req.query.username, $options: 'i'};
}

if(req.query.country){
filter.current_country = req.query.country;
}

if(req.query.district){
let district = await DistrictModel.findOne({name: req.query.district});
let locationUsers = await LocationUser.find({'location.district': district._id}).distinct("user_id");
filter._id = {$in : locationUsers};
}

if(req.query.region){
let districts = await DistrictModel.find({state : req.query.region}).distinct("_id");
let locationUsers = await LocationUser.find({'location.district' : {$in : districts}}).distinct("user_id");
filter._id = {$in : locationUsers};
}

let page = parseInt(req.query.page) || 1;
let size = parseInt(req.query.size) || 15;

const users = await User.find(
filter,
{},
{ skip: (page - 1) * size, limit: size * 1 }
);

let result = {
data_count: await User.countDocuments(filter),
page_size: size,
current_page: page,
data: users,
};

try {
res.send(users);
res.send(result);
} catch (err) {
res.status(500).send(err.toString());
}
Expand Down
2 changes: 1 addition & 1 deletion Backend/routes/UserRoutes.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const grant_access = require("../middlewares/auth.js").grant_access;
var user_controller = require("../controllers/UserController.js");


// router.get("/api/users", verifyToken.verifyToken, user_controller.get_all_users);
router.get("/api/users", verifyToken.verifyToken, user_controller.get_all_users);
router.get("/api/users/:id", verifyToken.verifyToken, user_controller.get_user_by_id);
router.post("/api/auth/login", user_controller.get_user_by_credentials);
router.post("/api/auth/register", user_controller.post_user);
Expand Down

0 comments on commit 884e0bf

Please sign in to comment.