diff --git a/app-constants.js b/app-constants.js index 4b89f079..81cd442a 100644 --- a/app-constants.js +++ b/app-constants.js @@ -1,3 +1,5 @@ +const config = require('config') + /** * App constants */ @@ -72,7 +74,9 @@ const Topics = { ChallengeTypeTimelineTemplateDeleted: 'test.new.bus.events', // 'challenge.action.type.timeline.template.deleted' ChallengeAttachmentCreated: 'test.new.bus.events', // 'challenge.action.attachment.created', ChallengeAttachmentUpdated: 'test.new.bus.events', // 'challenge.action.attachment.updated', - ChallengeAttachmentDeleted: 'test.new.bus.events' // 'challenge.action.attachment.deleted', + ChallengeAttachmentDeleted: 'test.new.bus.events', // 'challenge.action.attachment.deleted', + // Self Service topics + Notifications: 'notifications.action.create' } const challengeTracks = { @@ -92,6 +96,32 @@ const reviewTypes = { Internal: 'INTERNAL' } +const SelfServiceNotificationTypes = { + WORK_REQUEST_SUBMITTED: 'self-service.notifications.work-request-submitted', + WORK_REQUEST_STARTED: 'self-service.notifications.work-request-started', + WORK_REQUEST_REDIRECTED: 'self-service.notifications.work-request-redirected', + WORK_COMPLETED: 'self-service.notifications.work-completed' +} + +const SelfServiceNotificationSettings = { + [SelfServiceNotificationTypes.WORK_REQUEST_SUBMITTED]: { + sendgridTemplateId: config.SENDGRID_TEMPLATES.WORK_REQUEST_SUBMITTED, + cc: [] + }, + [SelfServiceNotificationTypes.WORK_REQUEST_STARTED]: { + sendgridTemplateId: config.SENDGRID_TEMPLATES.WORK_REQUEST_STARTED, + cc: [] + }, + [SelfServiceNotificationTypes.WORK_REQUEST_REDIRECTED]: { + sendgridTemplateId: config.SENDGRID_TEMPLATES.WORK_REQUEST_REDIRECTED, + cc: [...config.SELF_SERVICE_EMAIL_CC_ACCOUNTS] + }, + [SelfServiceNotificationTypes.WORK_COMPLETED]: { + sendgridTemplateId: config.SENDGRID_TEMPLATES.WORK_COMPLETED, + cc: [] + } +} + module.exports = { UserRoles, prizeSetTypes, @@ -103,5 +133,7 @@ module.exports = { challengeTracks, challengeTextSortField, DiscussionTypes, - reviewTypes + reviewTypes, + SelfServiceNotificationTypes, + SelfServiceNotificationSettings } diff --git a/config/default.js b/config/default.js index 2d19d167..3a081582 100644 --- a/config/default.js +++ b/config/default.js @@ -1,6 +1,7 @@ /** * The configuration file. */ +const _ = require('lodash') require('dotenv').config() module.exports = { READONLY: process.env.READONLY === 'true' || false, @@ -86,5 +87,16 @@ module.exports = { NEW_SELF_SERVICE_PROJECT_TYPE: process.env.NEW_SELF_SERVICE_PROJECT_TYPE || 'self-service', + SELF_SERVICE_WHITELIST_HANDLES: process.env.SELF_SERVICE_WHITELIST_HANDLES || [], + + SENDGRID_TEMPLATES: { + WORK_REQUEST_SUBMITTED: process.env.WORK_REQUEST_SUBMITTED || '', + WORK_REQUEST_STARTED: process.env.WORK_REQUEST_STARTED || '', + WORK_REQUEST_REDIRECTED: process.env.WORK_REQUEST_REDIRECTED || '', + WORK_COMPLETED: process.env.WORK_COMPLETED || '' + }, + + EMAIL_FROM: process.env.EMAIL_FROM || 'no-reply@topcoder.com', + SELF_SERVICE_EMAIL_CC_ACCOUNTS: process.env.SELF_SERVICE_EMAIL_CC_ACCOUNTS ? _.map(process.env.SELF_SERVICE_EMAIL_CC_ACCOUNTS.split(','), email => ({ email })) : [], SELF_SERVICE_WHITELIST_HANDLES: process.env.SELF_SERVICE_WHITELIST_HANDLES || ['TCConnCopilot', 'sstestcopilot'] } diff --git a/src/common/helper.js b/src/common/helper.js index 96e91071..ac0353e8 100644 --- a/src/common/helper.js +++ b/src/common/helper.js @@ -1126,6 +1126,35 @@ async function getMemberById (userId) { return {} } +/** + * Send self service notification + * @param {String} type the notification type + * @param {Array} recipients the array of recipients in { userId || email || handle } format + * @param {Object} data the data + */ +async function sendSelfServiceNotification (type, recipients, data) { + try { + await postBusEvent(constants.Topics.Notifications, { + notifications: [ + { + serviceId: 'email', + type, + details: { + from: config.EMAIL_FROM, + recipients: [...recipients], + cc: [...constants.SelfServiceNotificationSettings[type].cc], + data, + sendgridTemplateId: constants.SelfServiceNotificationSettings[type].sendgridTemplateId, + version: 'v3' + } + } + ] + }) + } catch (e) { + logger.debug(`Failed to post notification ${type}: ${e.message}`) + } +} + module.exports = { wrapExpress, autoWrapExpress, @@ -1175,5 +1204,6 @@ module.exports = { cancelProject, getProjectPayment, capturePayment, - cancelPayment + cancelPayment, + sendSelfServiceNotification } diff --git a/src/services/ChallengeService.js b/src/services/ChallengeService.js index e0a1ba8b..796116ad 100644 --- a/src/services/ChallengeService.js +++ b/src/services/ChallengeService.js @@ -1096,7 +1096,16 @@ async function createChallenge (currentUser, challenge, userToken) { // post bus event await helper.postBusEvent(constants.Topics.ChallengeCreated, ret) - + // send email notification + if (challenge.legacy.selfService && currentUser.handle) { + await helper.sendSelfServiceNotification( + constants.SelfServiceNotificationTypes.WORK_REQUEST_SUBMITTED, + [{ userId: currentUser.userId }], + { + handle: currentUser.handle, + workItemName: ret.name + }) + } return ret }