From 8604f614c829676e4523658b7d6ded139fc3b2bb Mon Sep 17 00:00:00 2001 From: Shobhan Sundar Goutam Date: Mon, 28 Oct 2024 00:17:11 +0530 Subject: [PATCH 1/2] fix: staging issue --- controllers/external-accounts.js | 10 +++++++++ test/integration/external-accounts.test.js | 24 ++++++++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/controllers/external-accounts.js b/controllers/external-accounts.js index 7e5763910..0968d54bc 100644 --- a/controllers/external-accounts.js +++ b/controllers/external-accounts.js @@ -4,6 +4,8 @@ const { getDiscordMembers } = require("../services/discordService"); const { addOrUpdate, getUsersByRole, updateUsersInBatch } = require("../models/users"); const { retrieveDiscordUsers, fetchUsersForKeyValues } = require("../services/dataAccessLayer"); const { EXTERNAL_ACCOUNTS_POST_ACTIONS } = require("../constants/external-accounts"); +const discordServices = require("../services/discordService"); +const config = require("config"); const logger = require("../utils/logger"); const { markUnDoneTasksOfArchivedUsersBacklog } = require("../models/tasks"); @@ -69,6 +71,14 @@ const linkExternalAccount = async (req, res) => { userId ); + try { + const unverifiedRoleId = config.get("discordUnverifiedRoleId"); + await discordServices.removeRoleFromUser(unverifiedRoleId, attributes.discordId, req.userData); + } catch (error) { + logger.error(`Error getting external account data: ${error}`); + return res.status(500).json({ message: `Role Deletion failed. Please contact admin.` }); + } + return res.status(204).json({ message: "Your discord profile has been linked successfully" }); } catch (error) { logger.error(`Error getting external account data: ${error}`); diff --git a/test/integration/external-accounts.test.js b/test/integration/external-accounts.test.js index 19cdc602c..d585565eb 100644 --- a/test/integration/external-accounts.test.js +++ b/test/integration/external-accounts.test.js @@ -16,6 +16,7 @@ const firestore = require("../../utils/firestore"); const userData = require("../fixtures/user/user")(); const userModel = firestore.collection("users"); const tasksModel = firestore.collection("tasks"); +const discordServices = require("../../services/discordService"); const { EXTERNAL_ACCOUNTS_POST_ACTIONS } = require("../../constants/external-accounts"); chai.use(chaiHttp); const cookieName = config.get("userToken.cookieName"); @@ -537,6 +538,8 @@ describe("External Accounts", function () { expect(getUserResponseBeforeUpdate.body).to.not.have.property("discordId"); expect(getUserResponseBeforeUpdate.body).to.not.have.property("discordJoinedAt"); + const removeRoleFromUserStub = Sinon.stub(discordServices, "removeRoleFromUser").resolves(); + const response = await chai .request(app) .patch(`/external-accounts/link/${externalAccountData[2].token}`) @@ -553,6 +556,27 @@ describe("External Accounts", function () { expect(updatedUserDetails.body.roles.in_discord).to.equal(true); expect(updatedUserDetails.body).to.have.property("discordId"); expect(updatedUserDetails.body).to.have.property("discordJoinedAt"); + + removeRoleFromUserStub.restore(); + }); + + it("Should return 500 when unverified role deletion failed", async function () { + await externalAccountsModel.addExternalAccountData(externalAccountData[2]); + + const removeRoleFromUserStub = Sinon.stub(discordServices, "removeRoleFromUser").rejects(); + + const response = await chai + .request(app) + .patch(`/external-accounts/link/${externalAccountData[2].token}`) + .query({ action: EXTERNAL_ACCOUNTS_POST_ACTIONS.DISCORD_USERS_SYNC }) + .set("Cookie", `${cookieName}=${newUserJWT}`); + + expect(response).to.have.status(500); + expect(response.body).to.be.an("object"); + expect(response.body).to.have.property("message"); + expect(response.body.message).to.equal(`Role Deletion failed. Please contact admin.`); + + removeRoleFromUserStub.restore(); }); }); }); From 6b849e53417e1eb56b65e40e88b34418c641dec9 Mon Sep 17 00:00:00 2001 From: Shobhan Sundar Goutam Date: Tue, 5 Nov 2024 18:52:03 +0530 Subject: [PATCH 2/2] updated response to res.boom --- controllers/external-accounts.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/controllers/external-accounts.js b/controllers/external-accounts.js index 0968d54bc..a2995156b 100644 --- a/controllers/external-accounts.js +++ b/controllers/external-accounts.js @@ -76,7 +76,9 @@ const linkExternalAccount = async (req, res) => { await discordServices.removeRoleFromUser(unverifiedRoleId, attributes.discordId, req.userData); } catch (error) { logger.error(`Error getting external account data: ${error}`); - return res.status(500).json({ message: `Role Deletion failed. Please contact admin.` }); + return res.boom.internal("Role Deletion failed. Please contact admin.", { + message: "Role Deletion failed. Please contact admin.", + }); } return res.status(204).json({ message: "Your discord profile has been linked successfully" });