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

Feature: inDiscord #1060

Merged
merged 11 commits into from
May 19, 2023
1 change: 1 addition & 0 deletions constants/roles.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const ROLES = {
APPOWNER: "app_owner",
MEMBER: "member",
ARCHIVED: "archived",
IN_DISCORD: "inDiscord",
};

module.exports = ROLES;
26 changes: 26 additions & 0 deletions controllers/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const { getPaginationLink, getUsernamesFromPRs } = require("../utils/users");
const { getQualifiers } = require("../utils/helper");
const { SOMETHING_WENT_WRONG, INTERNAL_SERVER_ERROR } = require("../constants/errorMessages");
const { getFilteredPRsOrIssues } = require("../utils/pullRequests");
const { IN_DISCORD } = require("../constants/roles");

const verifyUser = async (req, res) => {
const userId = req.userData.id;
Expand Down Expand Up @@ -107,6 +108,7 @@ const getUsers = async (req, res) => {
*/

const getUser = async (req, res) => {
console.log(req.params.username);
try {
const result = await userQuery.fetchUser({ username: req.params.username });
const { phone, email, ...user } = result.user;
Expand Down Expand Up @@ -462,6 +464,29 @@ const filterUsers = async (req, res) => {
}
};

// const syncInDiscordRole = async (req, res) => {
// try {
// } catch (error) {
// logger.error(`Error while fetching all users: ${error}`);
// return res.boom.serverUnavailable("Something went wrong please contact admin");
// }
// };

const fetchInDiscordUsers = async (req, res) => {
try {
const allUsers = await userQuery.fetchUsersWithRole(IN_DISCORD);

return res.json({
message: "Users found successfully!",
users: allUsers,
count: allUsers.length,
});
} catch (error) {
logger.error(`Error while fetching all users: ${error}`);
return res.boom.serverUnavailable("Something went wrong please contact admin");
}
};

module.exports = {
verifyUser,
generateChaincode,
Expand All @@ -481,4 +506,5 @@ module.exports = {
addDefaultArchivedRole,
getUserSkills,
filterUsers,
fetchInDiscordUsers,
};
25 changes: 25 additions & 0 deletions models/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,30 @@ const getUsersBasedOnFilter = async (query) => {
return userDocs;
};

const fetchUsersWithRole = async (role) => {
try {
// console.log(role);
const snapshot = await userModel.where(`roles.${role}`, "==", true).get();
const onlyMembers = [];

if (!snapshot.empty) {
snapshot.forEach((doc) => {
onlyMembers.push({
id: doc.id,
...doc.data(),
phone: undefined,
email: undefined,
tokens: undefined,
});
});
}
return onlyMembers;
} catch (err) {
logger.error("Error retrieving users data with roles of inDiscord", err);
throw err;
}
};

module.exports = {
addOrUpdate,
fetchPaginatedUsers,
Expand All @@ -424,4 +448,5 @@ module.exports = {
getRdsUserInfoByGitHubUsername,
fetchUsers,
getUsersBasedOnFilter,
fetchUsersWithRole,
};
3 changes: 3 additions & 0 deletions routes/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ router.get("/self", authenticate, users.getSelfDetails);
router.get("/isUsernameAvailable/:username", authenticate, users.getUsernameAvailabilty);
router.get("/chaincode", authenticate, users.generateChaincode);
router.get("/search", userValidator.validateUserQueryParams, users.filterUsers);
router.get("/InDiscord", authenticate, users.fetchInDiscordUsers);
Fixed Show fixed Hide fixed
router.get("/:username", users.getUser);
router.get("/:userId/intro", authenticate, authorizeRoles([SUPERUSER]), users.getUserIntro);
router.put("/self/intro", authenticate, userValidator.validateJoinData, users.addUserIntro);
Expand All @@ -29,4 +30,6 @@ router.patch("/rejectDiff", authenticate, authorizeRoles([SUPERUSER]), users.rej
router.patch("/:userId", authenticate, authorizeRoles([SUPERUSER]), users.updateUser);
router.get("/suggestedUsers/:skillId", authenticate, authorizeRoles([SUPERUSER]), users.getSuggestedUsers);

router.post("/syncInDiscord", authenticate, users.getSelfDetails);
Fixed Show fixed Hide fixed

module.exports = router;