Skip to content

Commit 23e594c

Browse files
committed
func: search roles and verified users
1 parent 1a45af3 commit 23e594c

File tree

5 files changed

+66
-2
lines changed

5 files changed

+66
-2
lines changed

constants/users.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ const USER_STATUS = {
1313
const ALLOWED_FILTER_PARAMS = {
1414
ITEM_TAG: ["levelId", "levelName", "levelValue", "tagId"],
1515
USER_STATE: ["state"],
16+
ROLE: ["role"],
1617
};
1718

1819
module.exports = { profileStatus, USER_STATUS, ALLOWED_FILTER_PARAMS };

controllers/users.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -473,7 +473,7 @@ const syncInDiscordRole = async (req, res) => {
473473
const authToken = jwt.sign({}, config.get("botToken.botPrivateKey"), {
474474
algorithm: "RS256",
475475
});
476-
// console.log(authToken);
476+
477477
const response = await fetch(`${DISCORD_BASE_URL}/discord-members`, {
478478
headers: {
479479
"Content-Type": "application/json",

middlewares/validators/user.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,11 @@ async function validateUserQueryParams(req, res, next) {
172172
joi.array().items(joi.string().valid("IDLE", "OOO", "ACTIVE"))
173173
)
174174
.optional(),
175+
role: joi
176+
.alternatives()
177+
.try(joi.string().valid("in_discord", "member"), joi.array().items(joi.string().valid("in_discord", "member")))
178+
.optional(),
179+
verified: joi.string().optional(),
175180
})
176181
.messages({
177182
"object.min": "Please provide at least one filter criteria",

test/unit/utils/users.test.js

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
const chai = require("chai");
22
const { expect } = chai;
33

4+
const sinon = require("sinon");
5+
46
const usersUtils = require("../../../utils/users");
57
const cleanDb = require("../../utils/cleanDb");
68
const addUser = require("../../utils/addUser");
@@ -65,4 +67,60 @@ describe("users", function () {
6567
expect(participantUserId).to.include(userId);
6668
});
6769
});
70+
71+
describe("mapDiscordMembersDataAndSyncRole", function () {
72+
it("should update roles and joined_discord fields for users with matching Discord IDs", function () {
73+
const allUsers = [
74+
{
75+
data: () => ({ roles: { archived: false }, discordId: "123" }),
76+
ref: { update: sinon.spy() },
77+
},
78+
];
79+
const discordMembers = [
80+
{
81+
user: { id: "123" },
82+
joined_at: "2022-05-01T00:00:00.000Z",
83+
},
84+
];
85+
86+
usersUtils.mapDiscordMembersDataAndSyncRole(allUsers, discordMembers);
87+
88+
sinon.assert.calledWithExactly(allUsers[0].ref.update, {
89+
roles: { archived: false, inDiscord: true },
90+
joined_discord: discordMembers[0].joined_at,
91+
});
92+
});
93+
94+
it("should update roles field to inDiscord: false for users with no matching Discord ID", function () {
95+
const allUsers = [
96+
{
97+
data: () => ({ roles: { archived: false }, discordId: "123" }),
98+
ref: { update: sinon.spy() },
99+
},
100+
];
101+
const discordMembers = [];
102+
103+
usersUtils.mapDiscordMembersDataAndSyncRole(allUsers, discordMembers);
104+
105+
sinon.assert.calledWithExactly(allUsers[0].ref.update, {
106+
roles: { archived: false, inDiscord: false },
107+
});
108+
});
109+
110+
it("should update roles field to inDiscord: false for users with archived roles", function () {
111+
const allUsers = [
112+
{
113+
data: () => ({ roles: { archived: true }, discordId: "123" }),
114+
ref: { update: () => sinon.spy() },
115+
},
116+
];
117+
const discordMembers = [];
118+
119+
usersUtils.mapDiscordMembersDataAndSyncRole(allUsers, discordMembers);
120+
121+
expect(allUsers[0].ref.update).to.have.been.calledWith({
122+
roles: { archived: true, inDiscord: false },
123+
});
124+
});
125+
});
68126
});

utils/users.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ function mapDiscordMembersDataAndSyncRole(allUsers, discordMembers) {
154154
const discordUserData = discordMembers.find((item) => item.user.id === user.discordId);
155155
if (discordUserData) {
156156
const roles = user.roles ? { ...user.roles, inDiscord: true } : { inDiscord: true };
157-
doc.ref.update({ roles, joined_RDS: discordUserData.joined_at });
157+
doc.ref.update({ roles, joined_discord: discordUserData.joined_at });
158158
} else {
159159
const roles = user.roles ? { ...user.roles, inDiscord: false } : { inDiscord: false };
160160
doc.ref.update({ roles });

0 commit comments

Comments
 (0)