Skip to content

Commit 53b80cd

Browse files
FEATURE: change in_discord role based on condition (#1324)
* FEATURE: true/false in_discord role based on recieve data from RDS and Discord * TEST: fix failing test * FEATURE: add userUpdatedWithInDiscordFalse in response * REFACTOR: syncExternalAccountData condition * REFACTOR: syncExternalAccountData function * FIX: order of arguments in addOrUpdate function call * Update controllers/external-accounts.js Co-authored-by: Randhir Kumar Singh <[email protected]> --------- Co-authored-by: Randhir Kumar Singh <[email protected]>
1 parent 7ffa20e commit 53b80cd

File tree

3 files changed

+22
-9
lines changed

3 files changed

+22
-9
lines changed

controllers/external-accounts.js

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ const syncExternalAccountData = async (req, res) => {
5555
const [discordUserData, rdsUserData] = await Promise.all([getDiscordMembers(), retrieveDiscordUsers()]);
5656
const rdsUserDataMap = {};
5757
const updateUserDataPromises = [];
58+
const userUpdatedWithInDiscordFalse = [];
5859

5960
rdsUserData.forEach((rdsUser) => {
6061
rdsUserDataMap[rdsUser.discordId] = {
@@ -63,25 +64,36 @@ const syncExternalAccountData = async (req, res) => {
6364
};
6465
});
6566

66-
discordUserData.forEach((discordUser) => {
67-
const mappedRdsUser = rdsUserDataMap[discordUser.user.id];
68-
if (mappedRdsUser) {
69-
const userData = {
67+
for (const rdsUser of rdsUserData) {
68+
const discordUser = discordUserData.find((discordUser) => discordUser.user.id === rdsUser.discordId);
69+
70+
let userData = {};
71+
if (rdsUser.roles?.in_discord && !discordUser) {
72+
userData = {
73+
roles: {
74+
...rdsUser.roles,
75+
in_discord: false,
76+
},
77+
};
78+
userUpdatedWithInDiscordFalse.push(rdsUser);
79+
} else if (discordUser) {
80+
userData = {
7081
discordJoinedAt: discordUser.joined_at,
7182
roles: {
72-
...mappedRdsUser.roles,
83+
...rdsUser.roles,
7384
in_discord: true,
7485
},
7586
};
76-
updateUserDataPromises.push(addOrUpdate(userData, mappedRdsUser.id));
7787
}
78-
});
88+
updateUserDataPromises.push(addOrUpdate(userData, rdsUser.id));
89+
}
7990

8091
await Promise.all(updateUserDataPromises);
8192

8293
return res.json({
8394
rdsUsers: rdsUserData.length,
8495
discordUsers: discordUserData.length,
96+
userUpdatedWithInDiscordFalse: userUpdatedWithInDiscordFalse.length,
8597
message: "Data Sync Complete",
8698
});
8799
} catch (err) {

models/users.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -552,7 +552,7 @@ const getDiscordUsers = async () => {
552552
const users = [];
553553
usersRef.forEach((user) => {
554554
const userData = user.data();
555-
if (userData?.discordId && userData.roles?.in_discord === false)
555+
if (userData?.discordId)
556556
users.push({
557557
id: user.id,
558558
...userData,

test/integration/external-accounts.test.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,8 +262,9 @@ describe("External Accounts", function () {
262262
}
263263
expect(res).to.have.status(200);
264264
expect(res.body).to.deep.equal({
265-
rdsUsers: 2,
265+
rdsUsers: 3,
266266
discordUsers: 2,
267+
userUpdatedWithInDiscordFalse: 1,
267268
message: "Data Sync Complete",
268269
});
269270
return done();

0 commit comments

Comments
 (0)