Skip to content

v1 of email notifications for self-service #451

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 34 additions & 2 deletions app-constants.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const config = require('config')

/**
* App constants
*/
Expand Down Expand Up @@ -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 = {
Expand All @@ -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,
Expand All @@ -103,5 +133,7 @@ module.exports = {
challengeTracks,
challengeTextSortField,
DiscussionTypes,
reviewTypes
reviewTypes,
SelfServiceNotificationTypes,
SelfServiceNotificationSettings
}
12 changes: 12 additions & 0 deletions config/default.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/**
* The configuration file.
*/
const _ = require('lodash')
require('dotenv').config()
module.exports = {
READONLY: process.env.READONLY === 'true' || false,
Expand Down Expand Up @@ -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']
}
32 changes: 31 additions & 1 deletion src/common/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -1175,5 +1204,6 @@ module.exports = {
cancelProject,
getProjectPayment,
capturePayment,
cancelPayment
cancelPayment,
sendSelfServiceNotification
}
11 changes: 10 additions & 1 deletion src/services/ChallengeService.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down