diff --git a/src/constants/common.js b/src/constants/common.js index e88a60a5..bcbee008 100644 --- a/src/constants/common.js +++ b/src/constants/common.js @@ -94,5 +94,11 @@ module.exports = { captchaEnabledAPIs: ['/user/v1/account/login', '/user/v1/account/generateOtp', '/user/v1/account/registrationOtp'], WRITE_ACCESS: 'w', READ_ACCESS: 'r', + ROLE_DESCRIPTION: { + MENTOR: 'As a Mentor: You will have the opportunity to guide and support mentees, sharing your knowledge and expertise to help them achieve their goals.', + MENTEE: 'As a Mentee: You will benefit from the guidance and support of a mentor, helping you to grow and develop in your personal and professional journey.', + SESSION_MANAGER: + 'As a Session Manager: You will be responsible for organizing and managing mentoring sessions, ensuring that they run smoothly and effectively.', + }, TYPE_ALL: 'all', } diff --git a/src/database/migrations/20240805072454-user_invite_role_change_email_template.js b/src/database/migrations/20240805072454-user_invite_role_change_email_template.js new file mode 100644 index 00000000..6f39d83d --- /dev/null +++ b/src/database/migrations/20240805072454-user_invite_role_change_email_template.js @@ -0,0 +1,67 @@ +'use strict' + +require('module-alias/register') +const moment = require('moment') + +module.exports = { + up: async (queryInterface, Sequelize) => { + try { + const defaultOrgId = queryInterface.sequelize.options.defaultOrgId + // Your email template data + const emailTemplates = [ + { + body: '
Dear {name},
We wanted to inform you that your role on our platform has been updated.
Your new role : {role}
What this means for you:
{description}
', + code: 'user_invites_role_change', + subject: 'Important: Role Updated on MentorEd', + }, + ] + // Check if email templates exist + const existingTemplates = await queryInterface.sequelize.query( + 'SELECT code FROM notification_templates WHERE organization_id = :orgId', + { + replacements: { orgId: defaultOrgId }, + type: Sequelize.QueryTypes.SELECT, + } + ) + + const newTemplates = emailTemplates.filter((template) => { + return !existingTemplates.some((existingTemplate) => existingTemplate.code === template.code) + }) + + // Insert new email templates + const notificationTemplateData = newTemplates.map((emailTemplate) => { + emailTemplate['status'] = 'ACTIVE' + emailTemplate['type'] = 'email' + emailTemplate['updated_at'] = moment().format() + emailTemplate['created_at'] = moment().format() + emailTemplate['organization_id'] = defaultOrgId + if (emailTemplate.code == 'email_footer') { + emailTemplate['type'] = 'emailFooter' + } else if (emailTemplate.code == 'email_header') { + emailTemplate['type'] = 'emailHeader' + } else { + emailTemplate['email_footer'] = 'email_footer' + emailTemplate['email_header'] = 'email_header' + } + return emailTemplate + }) + if (notificationTemplateData.length != 0) { + await queryInterface.bulkInsert('notification_templates', notificationTemplateData, {}) + } + + const body = `Dear {name},
Please find attached the status of your bulk upload activity.` + const updateData = { body } + + const updateFilter = { code: 'invitee_upload_status', organization_id: defaultOrgId } + await queryInterface.bulkUpdate('notification_templates', updateData, updateFilter) + } catch (error) { + console.log('Error:', error) + } + }, + + down: async (queryInterface, Sequelize) => { + const defaultOrgId = queryInterface.sequelize.options.defaultOrgId + + await queryInterface.bulkDelete('notification_templates', { organization_id: defaultOrgId }, {}) + }, +} diff --git a/src/services/userInvite.js b/src/services/userInvite.js index b79f212c..86264e7d 100644 --- a/src/services/userInvite.js +++ b/src/services/userInvite.js @@ -236,6 +236,12 @@ module.exports = class UserInviteHelper { user.organization_id ) + //fetch role change email template + const roleChangeEmailTemplate = await notificationTemplateQueries.findOneEmailTemplate( + process.env.ROLE_CHANGE_EMAIL_TEMPLATE_CODE, + user.organization_id + ) + //find already invited users const emailList = await userInviteQueries.findAll({ email: emailArray }) const existingInvitees = {} @@ -387,10 +393,30 @@ module.exports = class UserInviteHelper { }) } + let roleDescription + + if (newRoles.includes('Mentor')) { + roleDescription = common.ROLE_DESCRIPTION.MENTOR + } else if (newRoles.includes('Mentee')) { + roleDescription = common.ROLE_DESCRIPTION.MENTEE + } else if (newRoles.includes('Session Manager')) { + roleDescription = common.ROLE_DESCRIPTION.SESSION_MANAGER + } + + const updatedRoles = newRoles.length > 0 ? newRoles.join(',') : '' + const changeRoleRequestBody = { + email: invitee.email, + name: invitee.name, + role: updatedRoles, + description: roleDescription, + } //remove user data from redis const redisUserKey = common.redisUserPrefix + existingUser.id.toString() await utils.redisDel(redisUserKey) invitee.statusOrUserId = 'Success' + if (newRoles.length > 0 && roleChangeEmailTemplate) { + await this.sendInviteeEmail(roleChangeEmailTemplate, changeRoleRequestBody, null, {}) + } } else { invitee.statusOrUserId = 'No updates needed. User details are already up to date' } @@ -547,6 +573,7 @@ module.exports = class UserInviteHelper { appName: process.env.APP_NAME, portalURL: process.env.PORTAL_URL, roles: userData.roles || '', + description: userData.description || '', }), }, }