From abd6089c2e0257c90ce9ddfce382130413feac5e Mon Sep 17 00:00:00 2001 From: yash raj Date: Sat, 17 Aug 2024 23:29:09 +0530 Subject: [PATCH 1/2] add roles query param in /users api to get all members --- controllers/users.js | 27 +++++++++++++++++++++++++++ middlewares/validators/user.js | 7 +++++++ models/users.js | 14 +++++++++++++- 3 files changed, 47 insertions(+), 1 deletion(-) diff --git a/controllers/users.js b/controllers/users.js index e76b33f90..25e7ec510 100644 --- a/controllers/users.js +++ b/controllers/users.js @@ -111,6 +111,22 @@ const getUsers = async (req, res) => { user, }); } + + /** + * !!NOTE: At the time of writing we are only supporting the member role + * this will be fixed in the new onboarding flow, contact @tejaskh3 for more info + * + * if you're making changes to this code remove the role === 'member' check from middleware/validators/user.js + */ + if (req.query.roles === "member") { + const data = await dataAccess.retrieveUsers({ query: req.query }); + + return res.json({ + message: "members returned successfully!", + users: data.users, + }); + } + if (!transformedQuery?.days && transformedQuery?.filterBy === "unmerged_prs") { return res.boom.badRequest(`Days is required for filterBy ${transformedQuery?.filterBy}`); } @@ -919,6 +935,16 @@ async function usersPatchHandler(req, res) { } } +async function addMembersRole(params) { + try { + const data = await userQuery.batchUpdateAllUsersRoles(); + return data; + } catch (error) { + logger.error(`Error while updating all users roles: ${error}`); + throw Error(INTERNAL_SERVER_ERROR); + } +} + module.exports = { verifyUser, generateChaincode, @@ -949,4 +975,5 @@ module.exports = { archiveUserIfNotInDiscord, usersPatchHandler, isDeveloper, + addMembersRole, }; diff --git a/middlewares/validators/user.js b/middlewares/validators/user.js index 8be954e2e..387591d8b 100644 --- a/middlewares/validators/user.js +++ b/middlewares/validators/user.js @@ -190,6 +190,13 @@ async function getUsers(req, res, next) { filterBy: joi.string().optional(), days: joi.string().optional(), dev: joi.string().optional(), + roles: joi.optional().custom((value, helpers) => { + if (value !== "member") { + return helpers.message("only member role is supported"); + } + + return value; + }), }); try { await schema.validateAsync(req.query); diff --git a/models/users.js b/models/users.js index 087e18a9e..9c7b0c342 100644 --- a/models/users.js +++ b/models/users.js @@ -169,7 +169,19 @@ const fetchPaginatedUsers = async (query) => { const size = parseInt(query.size) || 100; const doc = (query.next || query.prev) && (await userModel.doc(query.next || query.prev).get()); - let dbQuery = userModel.where("roles.archived", "==", false).orderBy("username"); + let dbQuery; + /** + * !!NOTE : At the time of writing we only support member in the role query + * this will get fixed with the new onboarding flow, contact @tejaskh3 for more info + * + * if you're making changes to this code remove the archived check in the role query, example: role=archived,member + */ + if (query.roles === "member") { + dbQuery = userModel.where("roles.archived", "==", false).where("roles.member", "==", true); + } else { + dbQuery = userModel.where("roles.archived", "==", false).orderBy("username"); + } + let compositeQuery = [dbQuery]; if (isDevMode) { const usernameQuery = userModel.where("roles.archived", "==", false).orderBy("username"); From d80690dbb091ffaab77ed35ef9df178cb5063150 Mon Sep 17 00:00:00 2001 From: yash raj Date: Sun, 18 Aug 2024 00:19:06 +0530 Subject: [PATCH 2/2] remove roles check from controller --- controllers/users.js | 15 --------------- 1 file changed, 15 deletions(-) diff --git a/controllers/users.js b/controllers/users.js index 8740be33b..858212fa4 100644 --- a/controllers/users.js +++ b/controllers/users.js @@ -112,21 +112,6 @@ const getUsers = async (req, res) => { }); } - /** - * !!NOTE: At the time of writing we are only supporting the member role - * this will be fixed in the new onboarding flow, contact @tejaskh3 for more info - * - * if you're making changes to this code remove the role === 'member' check from middleware/validators/user.js - */ - if (req.query.roles === "member") { - const data = await dataAccess.retrieveUsers({ query: req.query }); - - return res.json({ - message: "members returned successfully!", - users: data.users, - }); - } - if (!transformedQuery?.days && transformedQuery?.filterBy === "unmerged_prs") { return res.boom.badRequest(`Days is required for filterBy ${transformedQuery?.filterBy}`); }