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

Feature: inDiscord #1060

Merged
merged 11 commits into from
May 19, 2023
1 change: 1 addition & 0 deletions constants/roles.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const ROLES = {
APPOWNER: "app_owner",
MEMBER: "member",
ARCHIVED: "archived",
INDISCORD: "in_discord",
DashDeipayan marked this conversation as resolved.
Show resolved Hide resolved
};

module.exports = ROLES;
1 change: 1 addition & 0 deletions constants/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const USER_STATUS = {
const ALLOWED_FILTER_PARAMS = {
ITEM_TAG: ["levelId", "levelName", "levelValue", "tagId"],
USER_STATE: ["state"],
ROLE: ["role"],
};

module.exports = { profileStatus, USER_STATUS, ALLOWED_FILTER_PARAMS };
8 changes: 6 additions & 2 deletions controllers/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const logsQuery = require("../models/logs");
const imageService = require("../services/imageService");
const { profileDiffStatus } = require("../constants/profileDiff");
const { logType } = require("../constants/logs");
const { fetch } = require("../utils/fetch");

const logger = require("../utils/logger");
const obfuscate = require("../utils/obfuscate");
const { getPaginationLink, getUsernamesFromPRs } = require("../utils/users");
Expand All @@ -24,7 +24,11 @@ const verifyUser = async (req, res) => {
logger.error(`Error while verifying user: ${error}`);
return res.boom.serverUnavailable(SOMETHING_WENT_WRONG);
}
fetch(process.env.IDENTITY_SERVICE_URL, "POST", null, { userId }, { "Content-Type": "application/json" });
fetch(process.env.IDENTITY_SERVICE_URL, {
method: "POST",
body: { userId },
headers: { "Content-Type": "application/json" },
});
heyrandhir marked this conversation as resolved.
Show resolved Hide resolved
return res.json({
DashDeipayan marked this conversation as resolved.
Show resolved Hide resolved
message: "Your request has been queued successfully",
});
Expand Down
3 changes: 3 additions & 0 deletions middlewares/validators/user.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const joi = require("joi");
const { USER_STATUS } = require("../../constants/users");
const ROLES = require("../../constants/roles");

const updateUser = async (req, res, next) => {
const schema = joi
Expand Down Expand Up @@ -172,6 +173,8 @@ async function validateUserQueryParams(req, res, next) {
joi.array().items(joi.string().valid("IDLE", "OOO", "ACTIVE"))
)
.optional(),
role: joi.string().valid(ROLES.MEMBER, ROLES.INDISCORD).optional(),
verified: joi.string().optional(),
heyrandhir marked this conversation as resolved.
Show resolved Hide resolved
})
.messages({
"object.min": "Please provide at least one filter criteria",
Expand Down
29 changes: 29 additions & 0 deletions models/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,8 @@ const getRdsUserInfoByGitHubUsername = async (githubUsername) => {
* @param {Array} query.levelNumber - Array of levelNumbers to filter the users on
* @param {Array} query.tagId - Array of tagIds to filter the users on
* @param {Array} query.state - Array of states to filter the users on
* @param {String} query.role - filter the users on role
* @param {String} query.verified - filter the users on verified i.e, discordId data
* @return {Promise<Array>} - Array of user documents that match the filter criteria
*/

Expand Down Expand Up @@ -412,6 +414,33 @@ const getUsersBasedOnFilter = async (query) => {
const filteredUserDocs = userDocs.filter((doc) => !doc.roles?.archived);
return filteredUserDocs;
}

const { role: roleQuery, verified: verifiedQuery } = query;

if (roleQuery) {
const filteredUsers = [];
const snapshot = await userModel.where(`roles.${roleQuery}`, "==", true).get();
snapshot.forEach((doc) => {
filteredUsers.push({
id: doc.id,
...doc.data(),
});
});

return filteredUsers.filter((user) => !user.roles?.archived);
}
if (verifiedQuery === "true") {
const filteredUsers = [];
const snapshot = await userModel.where("discordId", "!=", null).get();
bhtibrewal marked this conversation as resolved.
Show resolved Hide resolved
snapshot.forEach((doc) => {
filteredUsers.push({
id: doc.id,
...doc.data(),
});
});

return filteredUsers.filter((user) => !user.roles?.archived);
heyrandhir marked this conversation as resolved.
Show resolved Hide resolved
}
return [];
};

Expand Down
4 changes: 4 additions & 0 deletions test/fixtures/user/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@ module.exports = () => {
isMember: true,
phone: "1234567890",
email: "[email protected]",
joined_discord: "2023-01-13T18:21:09.278000+00:00",
roles: {
member: true,
in_discord: true,
DashDeipayan marked this conversation as resolved.
Show resolved Hide resolved
},
tokens: {
githubAccessToken: "githubAccessToken",
Expand Down Expand Up @@ -105,13 +107,15 @@ module.exports = () => {
github_display_name: "Ankush Dharkar",
phone: "1234567890",
email: "[email protected]",
joined_discord: "2023-01-13T18:21:09.278000+00:00",
DashDeipayan marked this conversation as resolved.
Show resolved Hide resolved
status: "idle",
tokens: {
githubAccessToken: "githubAccessToken",
},
roles: {
super_user: true,
archived: false,
in_discord: true,
},
picture: {
publicId: "profile/mtS4DhUvNYsKqI7oCWVB/aenklfhtjldc5ytei3ar",
Expand Down
12 changes: 12 additions & 0 deletions test/unit/models/users.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,4 +88,16 @@ describe("users", function () {
expect(userExists).to.equal(true);
});
});

describe(" search users API: getUsersBasedOnFilter", function () {
it("should return an empty array if no query is provided", async function () {
const result = await users.getUsersBasedOnFilter({});
expect(result).to.deep.equal([]);
});

it("should return an array of verified users", async function () {
const result = await users.getUsersBasedOnFilter({ verified: "true" });
expect(result).to.deep.equal(userDataArray.filter((user) => user.discordId));
});
});
});