From 37fec06537fefb22900e4bb589cf6218bf7ef3f6 Mon Sep 17 00:00:00 2001 From: joffinjoy Date: Tue, 12 Dec 2023 20:40:57 +0530 Subject: [PATCH 1/9] Debug Log --- src/middlewares/authenticator.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/middlewares/authenticator.js b/src/middlewares/authenticator.js index 193cc6002..f56d84685 100644 --- a/src/middlewares/authenticator.js +++ b/src/middlewares/authenticator.js @@ -22,6 +22,8 @@ module.exports = async function (req, res, next) { common.internalAccessUrls.map(function (path) { if (req.path.includes(path)) { + console.log('REQUEST PATH: ', req.path) + console.log('INTERNAL ACCESS PATH: ', path) if ( req.headers.internal_access_token && process.env.INTERNAL_ACCESS_TOKEN == req.headers.internal_access_token From d88dcaaf7fdffb8fd2a762ac631e28437180fc1e Mon Sep 17 00:00:00 2001 From: Priyanka Pradeep Date: Thu, 14 Dec 2023 11:09:14 +0530 Subject: [PATCH 2/9] fix for admin email --- src/services/organization.js | 1 + 1 file changed, 1 insertion(+) diff --git a/src/services/organization.js b/src/services/organization.js index 8a961dfd1..a0d1a2d61 100644 --- a/src/services/organization.js +++ b/src/services/organization.js @@ -99,6 +99,7 @@ module.exports = class OrganizationsHelper { role: common.ORG_ADMIN_ROLE, orgName: bodyData.name, appName: process.env.APP_NAME, + portalURL: process.env.PORTAL_URL, }), }, } From 8165ccbb7adbcb90152ad286f781430a0c73d66b Mon Sep 17 00:00:00 2001 From: Priyanka Pradeep Date: Thu, 14 Dec 2023 11:42:47 +0530 Subject: [PATCH 3/9] refresh token expiry change for testing --- src/constants/common.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/constants/common.js b/src/constants/common.js index 2b8ecf34c..e9c12f5e8 100644 --- a/src/constants/common.js +++ b/src/constants/common.js @@ -60,7 +60,7 @@ module.exports = { '/user/v1/account/search', ], notificationEmailType: 'email', - accessTokenExpiry: `${process.env.ACCESS_TOKEN_EXPIRY}d`, + accessTokenExpiry: `${process.env.ACCESS_TOKEN_EXPIRY}m`, refreshTokenExpiry: `${process.env.REFRESH_TOKEN_EXPIRY}d`, refreshTokenExpiryInMs: Number(process.env.REFRESH_TOKEN_EXPIRY) * 24 * 60 * 60 * 1000, refreshTokenLimit: 3, From 9a3132a1860793689ae8af83c95288311af950ad Mon Sep 17 00:00:00 2001 From: Priyanka Pradeep Date: Thu, 14 Dec 2023 18:03:06 +0530 Subject: [PATCH 4/9] fix for change the organization_id of the user --- src/database/queries/users.js | 35 +++++++++++++++++++++++++++++++++++ src/services/admin.js | 20 +++++++++++++------- 2 files changed, 48 insertions(+), 7 deletions(-) diff --git a/src/database/queries/users.js b/src/database/queries/users.js index 9cd0ee9f2..e3a84b90e 100644 --- a/src/database/queries/users.js +++ b/src/database/queries/users.js @@ -234,3 +234,38 @@ exports.listUsersFromView = async (roleId, organization_id, page, limit, search, throw error } } + +exports.changeOrganization = async (id, currentOrgId, newOrgId, updateBody = {}) => { + const transaction = await Sequelize.transaction() + try { + const existingUserRow = await database.User.findOne({ + where: { id, organization_id: currentOrgId }, + raw: true, + transaction, + }) + + if (!existingUserRow) throw new Error('User not found') + + await database.User.destroy({ + where: { id, organization_id: currentOrgId }, + force: true, + transaction, + }) + + const newUserRow = await database.User.create( + { + ...existingUserRow, + ...updateBody, + organization_id: newOrgId, + id, + }, + { transaction } + ) + + await transaction.commit() + return newUserRow + } catch (error) { + await transaction.rollback() + throw error + } +} diff --git a/src/services/admin.js b/src/services/admin.js index 9a74b74e6..83c53f31c 100644 --- a/src/services/admin.js +++ b/src/services/admin.js @@ -229,6 +229,7 @@ module.exports = class AdminHelper { responseCode: 'CLIENT_ERROR', }) } + const user = await userQueries.findOne({ id: userCredentials.user_id, organization_id: userCredentials.organization_id, @@ -288,10 +289,6 @@ module.exports = class AdminHelper { const roles = _.uniq([...(user.roles || []), role.id]) - let updateObj = { - roles, - } - if (userOrg.code != process.env.DEFAULT_ORGANISATION_CODE && userOrg.id != organizationId) { return common.failureResponse({ message: 'FAILED_TO_ASSIGN_AS_ADMIN', @@ -300,9 +297,18 @@ module.exports = class AdminHelper { }) } - updateObj.organization_id = organizationId + await userQueries.updateUser( + { id: userId, organization_id: userCredentials.organization_id }, + { roles: roles } + ) + + //update organization + if (userOrg.id != organizationId) { + await userQueries.changeOrganization(userId, userOrg.id, organizationId, { + organization_id: organizationId, + }) + } - await userQueries.updateUser({ id: userId, organization_id: userCredentials.organization_id }, updateObj) await UserCredentialQueries.updateUser( { email: userCredentials.email, @@ -322,7 +328,7 @@ module.exports = class AdminHelper { } ) - //update organization in mentoring + // update organization in mentoring eventBroadcaster('updateOrganization', { requestBody: { user_id: userId, From 8ff30f3bac730676584692539885fe2fbb2cd69b Mon Sep 17 00:00:00 2001 From: Priyanka Pradeep Date: Thu, 14 Dec 2023 18:59:34 +0530 Subject: [PATCH 5/9] user org update while csv upload --- src/services/userInvite.js | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/src/services/userInvite.js b/src/services/userInvite.js index b4a8f60d0..eebb71d4d 100644 --- a/src/services/userInvite.js +++ b/src/services/userInvite.js @@ -191,6 +191,7 @@ module.exports = class UserInviteHelper { let input = [] let isErrorOccured = false + let isOrgUpdate = false //fetch email template const mentorTemplateCode = process.env.MENTOR_INVITATION_EMAIL_TEMPLATE_CODE || null @@ -237,6 +238,7 @@ module.exports = class UserInviteHelper { //update user details if the user exist and in default org const existingUser = existingEmailsMap.get(invitee.email) + if (existingUser) { invitee.statusOrUserId = 'USER_ALREADY_EXISTS' isErrorOccured = true @@ -246,8 +248,17 @@ module.exports = class UserInviteHelper { existingUser.organization_id === user.organization_id if (isOrganizationMatch) { let userUpdateData = {} + if (existingUser.organization_id != user.organization_id) { - userUpdateData.organization_id = user.organization_id + await userQueries.changeOrganization( + existingUser.id, + existingUser.organization_id, + user.organization_id, + { + organization_id: user.organization_id, + } + ) + isOrgUpdate = true userUpdateData.refresh_tokens = [] } const areAllElementsInArray = _.every(roleTitlesToIds[invitee.roles], (element) => @@ -258,19 +269,19 @@ module.exports = class UserInviteHelper { userUpdateData.refresh_tokens = [] } - if (userUpdateData.organization_id || userUpdateData.roles) { + if (isOrgUpdate || userUpdateData.roles) { const userCredentials = await UserCredentialQueries.findOne({ - email: invitee.email.toLowerCase(), + email: invitee.email, }) await userQueries.updateUser({ id: userCredentials.user_id }, userUpdateData) - await UserCredentialQueries.updateUser( { - email: invitee.email.toLowerCase(), + email: invitee.email, }, - { organization_id: userUpdateData.organization_id } + { organization_id: user.organization_id } ) + const userRoles = await roleQueries.findAll({ id: existingUser.roles }) //call event to update in mentoring if (!userUpdateData?.roles) { From d2f7786395b5c3070d0f55555d616ec80a2dc7e2 Mon Sep 17 00:00:00 2001 From: Priyanka Pradeep Date: Fri, 15 Dec 2023 10:22:33 +0530 Subject: [PATCH 6/9] fix --- src/services/admin.js | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/services/admin.js b/src/services/admin.js index 83c53f31c..aba1118a3 100644 --- a/src/services/admin.js +++ b/src/services/admin.js @@ -297,16 +297,17 @@ module.exports = class AdminHelper { }) } - await userQueries.updateUser( - { id: userId, organization_id: userCredentials.organization_id }, - { roles: roles } - ) - //update organization if (userOrg.id != organizationId) { await userQueries.changeOrganization(userId, userOrg.id, organizationId, { organization_id: organizationId, + roles: roles, }) + } else { + await userQueries.updateUser( + { id: userId, organization_id: userCredentials.organization_id }, + { roles: roles } + ) } await UserCredentialQueries.updateUser( From 69859397e3b6e892d973c785b4a4882cd671a5aa Mon Sep 17 00:00:00 2001 From: joffinjoy Date: Fri, 15 Dec 2023 13:50:50 +0530 Subject: [PATCH 7/9] Reverting Testing Changes To Access Token Expiry --- src/constants/common.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/constants/common.js b/src/constants/common.js index e9c12f5e8..2b8ecf34c 100644 --- a/src/constants/common.js +++ b/src/constants/common.js @@ -60,7 +60,7 @@ module.exports = { '/user/v1/account/search', ], notificationEmailType: 'email', - accessTokenExpiry: `${process.env.ACCESS_TOKEN_EXPIRY}m`, + accessTokenExpiry: `${process.env.ACCESS_TOKEN_EXPIRY}d`, refreshTokenExpiry: `${process.env.REFRESH_TOKEN_EXPIRY}d`, refreshTokenExpiryInMs: Number(process.env.REFRESH_TOKEN_EXPIRY) * 24 * 60 * 60 * 1000, refreshTokenLimit: 3, From 87015c7be5fc18f9213a558f0b8153ee550ca817 Mon Sep 17 00:00:00 2001 From: joffinjoy Date: Fri, 15 Dec 2023 17:39:09 +0530 Subject: [PATCH 8/9] Broadcast Change --- src/services/userInvite.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/services/userInvite.js b/src/services/userInvite.js index eebb71d4d..98f51b3e6 100644 --- a/src/services/userInvite.js +++ b/src/services/userInvite.js @@ -298,7 +298,7 @@ module.exports = class UserInviteHelper { new_roles: [invitee.roles], current_roles: _.map(userRoles, 'title'), } - if (userUpdateData.organization_id) requestBody.organization_id = user.organization_id + if (isOrgUpdate) requestBody.organization_id = user.organization_id eventBroadcaster('roleChange', { requestBody, }) From 87b8dc7c336ac49745ce051d14f65ed4746dd923 Mon Sep 17 00:00:00 2001 From: joffinjoy Date: Mon, 18 Dec 2023 13:29:34 +0530 Subject: [PATCH 9/9] OrgIdFixes --- src/services/admin.js | 7 +++++-- src/services/userInvite.js | 8 +------- 2 files changed, 6 insertions(+), 9 deletions(-) diff --git a/src/services/admin.js b/src/services/admin.js index aba1118a3..07c999e1b 100644 --- a/src/services/admin.js +++ b/src/services/admin.js @@ -241,7 +241,7 @@ module.exports = class AdminHelper { responseCode: 'CLIENT_ERROR', }) } - userId = user.id + userId = user.id //un-necessary let organization = await organizationQueries.findByPk(organizationId) if (!organization?.id) { @@ -261,6 +261,9 @@ module.exports = class AdminHelper { }) } + // Create a unique array of organization administrators (orgAdmins) by combining the existing + // organization admins (organization.org_admin) with the userId. The lodash uniq function ensures + // that the resulting array contains only unique values. const orgAdmins = _.uniq([...(organization.org_admin || []), userId]) const orgRowsAffected = await organizationQueries.update( @@ -300,7 +303,7 @@ module.exports = class AdminHelper { //update organization if (userOrg.id != organizationId) { await userQueries.changeOrganization(userId, userOrg.id, organizationId, { - organization_id: organizationId, + //organization_id: organizationId, roles: roles, }) } else { diff --git a/src/services/userInvite.js b/src/services/userInvite.js index 98f51b3e6..49367cfdc 100644 --- a/src/services/userInvite.js +++ b/src/services/userInvite.js @@ -155,10 +155,7 @@ module.exports = class UserInviteHelper { static async createUserInvites(csvData, user, fileUploadId) { try { const outputFileName = utils.generateFileName(common.inviteeOutputFile, common.csvExtension) - - // get the role data from db const allRoles = _.uniq(_.map(csvData, 'roles').map((role) => role.toLowerCase())) - const roleList = await roleQueries.findAll({ title: allRoles }) const roleTitlesToIds = {} roleList.forEach((role) => { @@ -167,7 +164,6 @@ module.exports = class UserInviteHelper { //get all existing user const emailArray = _.uniq(_.map(csvData, 'email')) - const userCredentials = await UserCredentialQueries.findAll( { email: { [Op.in]: emailArray } }, { @@ -175,14 +171,12 @@ module.exports = class UserInviteHelper { } ) const userIds = _.map(userCredentials, 'user_id') - const existingUsers = await userQueries.findAll( { id: userIds }, { attributes: ['id', 'email', 'organization_id', 'roles'], } ) - const existingEmailsMap = new Map(existingUsers.map((eachUser) => [eachUser.email, eachUser])) //find default org id @@ -288,7 +282,7 @@ module.exports = class UserInviteHelper { eventBroadcaster('updateOrganization', { requestBody: { user_id: existingUser.id, - organization_id: existingUser.organization_id, + organization_id: user.organization_id, roles: _.map(userRoles, 'title'), }, })