Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

filter members option in user API #1001

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
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
filter members option in user API
  • Loading branch information
vinayak-trivedi committed Apr 6, 2023
commit e2742a1b6fe5863d2031efe9368e0503a04cab74
1 change: 0 additions & 1 deletion controllers/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ const getUserById = async (req, res) => {
const getUsers = async (req, res) => {
try {
const { allUsers, nextId, prevId } = await userQuery.fetchUsers(req.query);

return res.json({
message: "Users returned successfully!",
users: allUsers,
Expand Down
1 change: 1 addition & 0 deletions middlewares/validators/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ async function getUsers(req, res, next) {
search: joi.string().optional().messages({
"string.empty": "search value must not be empty",
}),
member: joi.string().optional(),
next: joi
.string()
.optional()
Expand Down
19 changes: 19 additions & 0 deletions models/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,25 @@ const fetchUsers = async (query) => {
try {
// INFO: default user size set to 100
// INFO: https://github.com/Real-Dev-Squad/website-backend/pull/873#discussion_r1064229932
if (query.member === "true") {
const membersArray = [];
const snapshot = await userModel.where("roles.member", "==", true).where("roles.archived", "==", false).get();
if (!snapshot.empty) {
snapshot.forEach((doc) => {
membersArray.push({
id: doc.id,
...doc.data(),
phone: undefined,
email: undefined,
tokens: undefined,
chaincode: undefined,
vinayak-trivedi marked this conversation as resolved.
Show resolved Hide resolved
});
});
}
return {
allUsers: membersArray,
};
}
Comment on lines +133 to +151
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please handle this in the bottom part, where users are being fetched. We are just repeating code here.
There is no pagination here, even though user API has pagination.

And instead of having a key where member=true, let's generic field like a filter or role
Where we pass filterBy=MEMBERS, filterBy=AO or role=ARCHIVED, role=NEW, with paginations

const size = parseInt(query.size) || 100;
const doc = (query.next || query.prev) && (await userModel.doc(query.next || query.prev).get());
let dbQuery = (query.prev ? userModel.limitToLast(size) : userModel.limit(size)).orderBy("username");
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/user/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ module.exports = () => {
roles: {
super_user: true,
archived: false,
member: true,
},
picture: {
publicId: "profile/mtS4DhUvNYsKqI7oCWVB/aenklfhtjldc5ytei3ar",
Expand Down
15 changes: 15 additions & 0 deletions test/integration/users.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ describe("Users", function () {
await addOrUpdate(userData[1]);
await addOrUpdate(userData[2]);
await addOrUpdate(userData[3]);
await addOrUpdate(userData[4]);
});

afterEach(async function () {
Expand Down Expand Up @@ -357,6 +358,20 @@ describe("Users", function () {
expect(previousPageResponse.body.links).to.have.property("prev");
expect(previousPageResponse.body.users).to.have.length(2);
});

it("should return all the user who are member and not archieved if members=true is sent in the query", async function () {
const response = await chai.request(app).get(`/users?member=true`).set("cookie", `${cookieName}=${jwt}`);

expect(response).to.have.status(200);
expect(response.body).to.be.a("object");
expect(response.body.message).to.equal("Users returned successfully!");
expect(response.body).to.have.property("links");
expect(response.body.links).to.have.property("next");
expect(response.body.links).to.have.property("prev");

expect(response.body.users[0].roles.member).to.be.equal(true);
expect(response.body.users[0].roles.archived).to.be.equal(false);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we also test the length of the response returned?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we do not need to test it

});
});

describe("GET /users/self", function () {
Expand Down