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

feat: API to sync in_discord role with discord members and add joined_discord date #1097

Merged
merged 26 commits into from
Jun 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
ebe549e
feat: sync-indiscord role
bhtibrewal May 17, 2023
fa48a59
test: fix failing test
bhtibrewal May 17, 2023
6b0186b
chore: rename variable
bhtibrewal May 17, 2023
f86620b
feat: implement background worker using bull
bhtibrewal May 19, 2023
76c61b6
feat: sync in_discord API
bhtibrewal May 21, 2023
1424fe0
chore: remove commented code and add comments
bhtibrewal May 21, 2023
3a62f8a
Merge remote-tracking branch 'origin/develop' into feat/sync-indiscor…
bhtibrewal May 21, 2023
375aba7
chore : remove test
bhtibrewal May 21, 2023
ab9f99f
Update users.js
bhtibrewal May 23, 2023
2c8b802
chore: fix prettier issue
bhtibrewal May 23, 2023
f0bbe82
chore: address PR comments
bhtibrewal May 24, 2023
9956719
chore: remove id from update data
bhtibrewal May 25, 2023
54c7f93
Merge branch 'develop' into feat/sync-indiscord-role
bhtibrewal May 25, 2023
1e6583e
resolve merge conflicts
RitikJaiswal75 Jun 6, 2023
7463bec
remove bull and unwanted code
RitikJaiswal75 Jun 6, 2023
497464e
remove unwanted changes from controller
RitikJaiswal75 Jun 6, 2023
c03bf64
temporary commit
RitikJaiswal75 Jun 6, 2023
f8e7a16
remove yarn lock changes
RitikJaiswal75 Jun 6, 2023
4592e15
add filter queries
RitikJaiswal75 Jun 6, 2023
73cfa3f
rename controll for better understanding
RitikJaiswal75 Jun 6, 2023
1f8861b
remove changes made to levels api
RitikJaiswal75 Jun 6, 2023
24fb190
add tests for returning discord users
RitikJaiswal75 Jun 6, 2023
a520a64
make the api only for superuser
RitikJaiswal75 Jun 6, 2023
1435ec7
fix authorizeroles function call
RitikJaiswal75 Jun 6, 2023
e284cb6
store user.data() in a variable to reuse
RitikJaiswal75 Jun 7, 2023
d42050e
rename controller
RitikJaiswal75 Jun 7, 2023
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
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 @@ -450,6 +450,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)
iamitprakash marked this conversation as resolved.
Show resolved Hide resolved
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 @@ -465,4 +490,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);

Check failure

Code scanning / CodeQL

Missing rate limiting

This route handler performs [authorization](1), but is not rate-limited. This route handler performs [authorization](2), but is not rate-limited. This route handler performs [authorization](3), but is not rate-limited.

// 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 @@ -1071,4 +1072,27 @@ describe("Users", function () {
});
});
});

describe("PATCH /users", function () {
iamitprakash marked this conversation as resolved.
Show resolved Hide resolved
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();
});
});
});
});