Skip to content

Commit 5cd5815

Browse files
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 <[email protected]>
1 parent 24dd9b7 commit 5cd5815

File tree

5 files changed

+108
-0
lines changed

5 files changed

+108
-0
lines changed

controllers/users.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -495,6 +495,11 @@ const filterUsers = async (req, res) => {
495495
}
496496
};
497497

498+
const nonVerifiedDiscordUsers = async (req, res) => {
499+
const data = await userQuery.getDiscordUsers();
500+
return res.json(data);
501+
};
502+
498503
module.exports = {
499504
verifyUser,
500505
generateChaincode,
@@ -514,4 +519,5 @@ module.exports = {
514519
addDefaultArchivedRole,
515520
getUserSkills,
516521
filterUsers,
522+
nonVerifiedDiscordUsers,
517523
};

models/users.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -458,6 +458,31 @@ const getUsersBasedOnFilter = async (query) => {
458458
return [];
459459
};
460460

461+
/**
462+
* Fetch all users
463+
*
464+
* @return {Promise<users>}
465+
*/
466+
467+
const getDiscordUsers = async () => {
468+
try {
469+
const usersRef = await userModel.where("roles.archived", "==", false).get();
470+
const users = [];
471+
usersRef.forEach((user) => {
472+
const userData = user.data();
473+
if (userData?.discordId && userData.roles?.in_discord === false)
474+
users.push({
475+
id: user.id,
476+
...userData,
477+
});
478+
});
479+
return users;
480+
} catch (err) {
481+
logger.error(`Error while fetching all users: ${err}`);
482+
throw err;
483+
}
484+
};
485+
461486
module.exports = {
462487
addOrUpdate,
463488
fetchPaginatedUsers,
@@ -473,4 +498,5 @@ module.exports = {
473498
getRdsUserInfoByGitHubUsername,
474499
fetchUsers,
475500
getUsersBasedOnFilter,
501+
getDiscordUsers,
476502
};

routes/users.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ router.get("/:userId/intro", authenticate, authorizeRoles([SUPERUSER]), users.ge
2121
router.put("/self/intro", authenticate, userValidator.validateJoinData, users.addUserIntro);
2222
router.get("/:id/skills", users.getUserSkills);
2323
router.get("/:id/badges", getUserBadges);
24+
router.patch("/", authenticate, authorizeRoles([SUPERUSER]), users.nonVerifiedDiscordUsers);
2425

2526
// upload.single('profile') -> multer inmemory storage of file for type multipart/form-data
2627
router.post("/picture", authenticate, upload.single("profile"), users.postUserPicture);

test/fixtures/user/inDiscord.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
module.exports = () => {
2+
return [
3+
{
4+
discordId: "1234567890987543",
5+
first_name: "jhon",
6+
last_name: "doe",
7+
username: "jhon-doe",
8+
github_id: "jhon-doe",
9+
github_display_name: "jhon-doe",
10+
incompleteUserDetails: false,
11+
roles: {
12+
archived: false,
13+
in_discord: true,
14+
},
15+
tokens: {
16+
githubAccessToken: "weuytrertyuiiuyrtyui4567yyyuyghy",
17+
},
18+
},
19+
{
20+
discordId: "8494597689576953",
21+
first_name: "test",
22+
last_name: "user",
23+
username: "test-user",
24+
github_id: "test-user",
25+
github_display_name: "test-user",
26+
incompleteUserDetails: false,
27+
roles: {
28+
archived: false,
29+
in_discord: false,
30+
},
31+
tokens: {
32+
githubAccessToken: "weuytrertyuiiuyrtyui4567yyyuyghy",
33+
},
34+
},
35+
{
36+
first_name: "test",
37+
last_name: "user",
38+
username: "test-user",
39+
github_id: "test-user",
40+
github_display_name: "test-user",
41+
incompleteUserDetails: false,
42+
roles: {
43+
archived: false,
44+
in_discord: false,
45+
},
46+
tokens: {
47+
githubAccessToken: "weuytrertyuiiuyrtyui4567yyyuyghy",
48+
},
49+
},
50+
];
51+
};

test/integration/users.test.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ const userData = require("../fixtures/user/user")();
1313
const profileDiffData = require("../fixtures/profileDiffs/profileDiffs")();
1414
const superUser = userData[4];
1515
const searchParamValues = require("../fixtures/user/search")();
16+
const inDiscordUsers = require("../fixtures/user/inDiscord")();
1617

1718
const config = require("config");
1819
const joinData = require("../fixtures/user/join");
@@ -1091,4 +1092,27 @@ describe("Users", function () {
10911092
});
10921093
});
10931094
});
1095+
1096+
describe("PATCH /users", function () {
1097+
beforeEach(async function () {
1098+
await addUser(inDiscordUsers[0]);
1099+
await addUser(inDiscordUsers[1]);
1100+
await addUser(inDiscordUsers[2]);
1101+
});
1102+
it("returns users with discord id and in_discord false", function (done) {
1103+
chai
1104+
.request(app)
1105+
.patch("/users")
1106+
.set("Cookie", `${cookieName}=${superUserAuthToken}`)
1107+
.end((err, res) => {
1108+
if (err) {
1109+
return done(err);
1110+
}
1111+
expect(res).to.have.status(200);
1112+
expect(res.body).to.have.length(1);
1113+
expect(res.body[0].username).equal("test-user");
1114+
return done();
1115+
});
1116+
});
1117+
});
10941118
});

0 commit comments

Comments
 (0)