From 5cd58150ee3bdd4635816ca9c7b0802f7343b2c4 Mon Sep 17 00:00:00 2001 From: Bhavika Tibrewal Date: Thu, 8 Jun 2023 00:17:11 +0530 Subject: [PATCH] feat: API to sync in_discord role with discord members and add joined_discord date (#1097) * feat: sync-indiscord role * test: fix failing test * chore: rename variable * feat: implement background worker using bull * feat: sync in_discord API * chore: remove commented code and add comments * chore : remove test * Update users.js * chore: fix prettier issue * chore: address PR comments * chore: remove id from update data * remove bull and unwanted code * remove unwanted changes from controller * temporary commit * remove yarn lock changes * add filter queries * rename controll for better understanding * remove changes made to levels api * add tests for returning discord users * make the api only for superuser * fix authorizeroles function call * store user.data() in a variable to reuse * rename controller --------- Co-authored-by: ritikjaiswal75 --- controllers/users.js | 6 ++++ models/users.js | 26 +++++++++++++++++ routes/users.js | 1 + test/fixtures/user/inDiscord.js | 51 +++++++++++++++++++++++++++++++++ test/integration/users.test.js | 24 ++++++++++++++++ 5 files changed, 108 insertions(+) create mode 100644 test/fixtures/user/inDiscord.js diff --git a/controllers/users.js b/controllers/users.js index 4bb5b72a8..2f39784fd 100644 --- a/controllers/users.js +++ b/controllers/users.js @@ -495,6 +495,11 @@ const filterUsers = async (req, res) => { } }; +const nonVerifiedDiscordUsers = async (req, res) => { + const data = await userQuery.getDiscordUsers(); + return res.json(data); +}; + module.exports = { verifyUser, generateChaincode, @@ -514,4 +519,5 @@ module.exports = { addDefaultArchivedRole, getUserSkills, filterUsers, + nonVerifiedDiscordUsers, }; diff --git a/models/users.js b/models/users.js index 73da7722d..ac01a3fb6 100644 --- a/models/users.js +++ b/models/users.js @@ -458,6 +458,31 @@ const getUsersBasedOnFilter = async (query) => { return []; }; +/** + * Fetch all users + * + * @return {Promise} + */ + +const getDiscordUsers = async () => { + try { + const usersRef = await userModel.where("roles.archived", "==", false).get(); + const users = []; + usersRef.forEach((user) => { + const userData = user.data(); + if (userData?.discordId && userData.roles?.in_discord === false) + users.push({ + id: user.id, + ...userData, + }); + }); + return users; + } catch (err) { + logger.error(`Error while fetching all users: ${err}`); + throw err; + } +}; + module.exports = { addOrUpdate, fetchPaginatedUsers, @@ -473,4 +498,5 @@ module.exports = { getRdsUserInfoByGitHubUsername, fetchUsers, getUsersBasedOnFilter, + getDiscordUsers, }; diff --git a/routes/users.js b/routes/users.js index 047d8eb6f..66bbf48e8 100644 --- a/routes/users.js +++ b/routes/users.js @@ -21,6 +21,7 @@ router.get("/:userId/intro", authenticate, authorizeRoles([SUPERUSER]), users.ge router.put("/self/intro", authenticate, userValidator.validateJoinData, users.addUserIntro); router.get("/:id/skills", users.getUserSkills); router.get("/:id/badges", getUserBadges); +router.patch("/", authenticate, authorizeRoles([SUPERUSER]), users.nonVerifiedDiscordUsers); // upload.single('profile') -> multer inmemory storage of file for type multipart/form-data router.post("/picture", authenticate, upload.single("profile"), users.postUserPicture); diff --git a/test/fixtures/user/inDiscord.js b/test/fixtures/user/inDiscord.js new file mode 100644 index 000000000..e58678922 --- /dev/null +++ b/test/fixtures/user/inDiscord.js @@ -0,0 +1,51 @@ +module.exports = () => { + return [ + { + discordId: "1234567890987543", + first_name: "jhon", + last_name: "doe", + username: "jhon-doe", + github_id: "jhon-doe", + github_display_name: "jhon-doe", + incompleteUserDetails: false, + roles: { + archived: false, + in_discord: true, + }, + tokens: { + githubAccessToken: "weuytrertyuiiuyrtyui4567yyyuyghy", + }, + }, + { + discordId: "8494597689576953", + first_name: "test", + last_name: "user", + username: "test-user", + github_id: "test-user", + github_display_name: "test-user", + incompleteUserDetails: false, + roles: { + archived: false, + in_discord: false, + }, + tokens: { + githubAccessToken: "weuytrertyuiiuyrtyui4567yyyuyghy", + }, + }, + { + first_name: "test", + last_name: "user", + username: "test-user", + github_id: "test-user", + github_display_name: "test-user", + incompleteUserDetails: false, + roles: { + archived: false, + in_discord: false, + }, + tokens: { + githubAccessToken: "weuytrertyuiiuyrtyui4567yyyuyghy", + }, + }, + ]; +}; diff --git a/test/integration/users.test.js b/test/integration/users.test.js index 3c09ef5f7..84c13502a 100644 --- a/test/integration/users.test.js +++ b/test/integration/users.test.js @@ -13,6 +13,7 @@ const userData = require("../fixtures/user/user")(); const profileDiffData = require("../fixtures/profileDiffs/profileDiffs")(); const superUser = userData[4]; const searchParamValues = require("../fixtures/user/search")(); +const inDiscordUsers = require("../fixtures/user/inDiscord")(); const config = require("config"); const joinData = require("../fixtures/user/join"); @@ -1091,4 +1092,27 @@ describe("Users", function () { }); }); }); + + describe("PATCH /users", function () { + beforeEach(async function () { + await addUser(inDiscordUsers[0]); + await addUser(inDiscordUsers[1]); + await addUser(inDiscordUsers[2]); + }); + it("returns users with discord id and in_discord false", function (done) { + chai + .request(app) + .patch("/users") + .set("Cookie", `${cookieName}=${superUserAuthToken}`) + .end((err, res) => { + if (err) { + return done(err); + } + expect(res).to.have.status(200); + expect(res.body).to.have.length(1); + expect(res.body[0].username).equal("test-user"); + return done(); + }); + }); + }); });