Skip to content

Commit

Permalink
feat: API to sync in_discord role with discord members and add joined…
Browse files Browse the repository at this point in the history
…_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 <[email protected]>
  • Loading branch information
bhtibrewal and RitikJaiswal75 authored Jun 7, 2023
1 parent 24dd9b7 commit 5cd5815
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 0 deletions.
6 changes: 6 additions & 0 deletions controllers/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -514,4 +519,5 @@ module.exports = {
addDefaultArchivedRole,
getUserSkills,
filterUsers,
nonVerifiedDiscordUsers,
};
26 changes: 26 additions & 0 deletions models/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,31 @@ const getUsersBasedOnFilter = async (query) => {
return [];
};

/**
* Fetch all users
*
* @return {Promise<users>}
*/

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,
Expand All @@ -473,4 +498,5 @@ module.exports = {
getRdsUserInfoByGitHubUsername,
fetchUsers,
getUsersBasedOnFilter,
getDiscordUsers,
};
1 change: 1 addition & 0 deletions routes/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
51 changes: 51 additions & 0 deletions test/fixtures/user/inDiscord.js
Original file line number Diff line number Diff line change
@@ -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",
},
},
];
};
24 changes: 24 additions & 0 deletions test/integration/users.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -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();
});
});
});
});

0 comments on commit 5cd5815

Please sign in to comment.