Skip to content

Commit

Permalink
Merge pull request #2061 from Real-Dev-Squad/dashboard/I-758/identity…
Browse files Browse the repository at this point in the history
…-stats

Added an API for identity stats
  • Loading branch information
lakshayman authored Aug 6, 2024
2 parents f73f04e + 1352dc1 commit 72d8f08
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 0 deletions.
36 changes: 36 additions & 0 deletions controllers/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -919,6 +919,41 @@ async function usersPatchHandler(req, res) {
}
}

const getIdentityStats = async (req, res) => {
const verifiedUsers = await userQuery.fetchUserForKeyValue("profileStatus", "VERIFIED");
const blockedUsers = await userQuery.fetchUserForKeyValue("profileStatus", "BLOCKED");
let developers = [];
const membersInDiscord = await getDiscordMembers();
if (membersInDiscord) {
const developersInDiscord = membersInDiscord.filter(
(discordMember) => discordMember && discordMember.roles && discordMember.roles.includes(discordDeveloperRoleId)
);
developers = developersInDiscord;
}

const findUserByDiscordId = (usersArray, discordId) => usersArray.find((user) => user.discordId === discordId);

const verifiedDeveloperCount = developers.filter((developer) =>
findUserByDiscordId(verifiedUsers, developer.user.id)
).length;
const blockedDeveloperCount = developers.filter((developer) =>
findUserByDiscordId(blockedUsers, developer.user.id)
).length;
const developersLeftToVerifyCount = developers.filter(
(developer) =>
!findUserByDiscordId(verifiedUsers, developer.user.id) && !findUserByDiscordId(blockedUsers, developer.user.id)
).length;

return res.status(200).json({
verifiedUsersCount: verifiedUsers.length,
blockedUsersCount: blockedUsers.length,
verifiedDeveloperCount,
blockedDeveloperCount,
developersLeftToVerifyCount,
developersCount: developers.length,
});
};

module.exports = {
verifyUser,
generateChaincode,
Expand Down Expand Up @@ -949,4 +984,5 @@ module.exports = {
archiveUserIfNotInDiscord,
usersPatchHandler,
isDeveloper,
getIdentityStats,
};
1 change: 1 addition & 0 deletions routes/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ router.get("/isUsernameAvailable/:username", authenticate, users.getUsernameAvai
router.get("/username", authenticate, userValidator.validateGenerateUsernameQuery, users.generateUsername);
router.get("/chaincode", authenticate, users.generateChaincode);
router.get("/search", userValidator.validateUserQueryParams, users.filterUsers);
router.get("/identity-stats", authenticate, authorizeRoles([SUPERUSER]), users.getIdentityStats);
router.patch(
"/:userId/update-nickname",
authenticate,
Expand Down
3 changes: 3 additions & 0 deletions test/fixtures/user/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ module.exports = () => {
},
status: "active",
profileURL: "https://abcde.com",
profileStatus: "BLOCKED",
picture: {
publicId: "profile/mtS4DhUvNYsKqI7oCWVB/aenklfhtjldc5ytei3ar",
url: "https://res.cloudinary.com/realdevsquad/image/upload/v1667685133/profile/mtS4DhUvNYsKqI7oCWVB/aenklfhtjldc5ytei3ar.jpg",
Expand All @@ -59,6 +60,7 @@ module.exports = () => {
url: "https://res.cloudinary.com/realdevsquad/image/upload/v1667685133/profile/mtS4DhUvNYsKqI7oCWVB/aenklfhtjldc5ytei3ar.jpg",
},
nickname_synced: false,
profileStatus: "BLOCKED",
},
{
username: "pranavg",
Expand All @@ -81,6 +83,7 @@ module.exports = () => {
url: "https://res.cloudinary.com/realdevsquad/image/upload/v1667685133/profile/mtS4DhUvNYsKqI7oCWVB/aenklfhtjldc5ytei3ar.jpg",
},
nickname_synced: false,
profileStatus: "VERIFIED",
},
{
username: "sagar",
Expand Down
58 changes: 58 additions & 0 deletions test/integration/users.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,64 @@ describe("Users", function () {
await cleanDb();
});

describe("GET /users/identity-stats", function () {
beforeEach(function () {
fetchStub = Sinon.stub(global, "fetch");
fetchStub.returns(
Promise.resolve({
status: 200,
json: () => Promise.resolve(getDiscordMembers),
})
);
});

afterEach(function () {
Sinon.restore();
});

it("Should return when only one user", function (done) {
chai
.request(app)
.get("/users/identity-stats")
.set("cookie", `${cookieName}=${superUserAuthToken}`)
.end((err, res) => {
if (err) {
return done(err);
}

expect(res).to.have.status(200);
expect(res.body.blockedDeveloperCount).to.equal(0);
expect(res.body.blockedUsersCount).to.equal(1);
expect(res.body.developersCount).to.equal(0);
expect(res.body.developersLeftToVerifyCount).to.equal(0);
expect(res.body.verifiedDeveloperCount).to.equal(0);
expect(res.body.verifiedUsersCount).to.equal(0);

return done();
});
});

it("Should return verified and blocked users", async function () {
await addOrUpdate(userData[0]);
await addOrUpdate(userData[1]);
await addOrUpdate(userData[2]);
await addOrUpdate(userData[3]);

const res = await chai
.request(app)
.get("/users/identity-stats")
.set("cookie", `${cookieName}=${superUserAuthToken}`);

expect(res).to.have.status(200);
expect(res.body.blockedDeveloperCount).to.equal(0);
expect(res.body.blockedUsersCount).to.equal(2);
expect(res.body.developersCount).to.equal(0);
expect(res.body.developersLeftToVerifyCount).to.equal(0);
expect(res.body.verifiedDeveloperCount).to.equal(0);
expect(res.body.verifiedUsersCount).to.equal(1);
});
});

describe("PATCH /users/self", function () {
beforeEach(function () {
fetchStub = Sinon.stub(global, "fetch");
Expand Down

0 comments on commit 72d8f08

Please sign in to comment.