diff --git a/controllers/external-accounts.js b/controllers/external-accounts.js index 7e5763910..a2995156b 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,16 @@ 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.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" }); } 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(); }); }); });