Skip to content

Commit 9e9916a

Browse files
Merge pull request #451 from topcoder-platform/feature/self-service-email
v1 of email notifications for self-service
2 parents fd0df23 + 797e66f commit 9e9916a

File tree

4 files changed

+87
-4
lines changed

4 files changed

+87
-4
lines changed

app-constants.js

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
const config = require('config')
2+
13
/**
24
* App constants
35
*/
@@ -72,7 +74,9 @@ const Topics = {
7274
ChallengeTypeTimelineTemplateDeleted: 'test.new.bus.events', // 'challenge.action.type.timeline.template.deleted'
7375
ChallengeAttachmentCreated: 'test.new.bus.events', // 'challenge.action.attachment.created',
7476
ChallengeAttachmentUpdated: 'test.new.bus.events', // 'challenge.action.attachment.updated',
75-
ChallengeAttachmentDeleted: 'test.new.bus.events' // 'challenge.action.attachment.deleted',
77+
ChallengeAttachmentDeleted: 'test.new.bus.events', // 'challenge.action.attachment.deleted',
78+
// Self Service topics
79+
Notifications: 'notifications.action.create'
7680
}
7781

7882
const challengeTracks = {
@@ -92,6 +96,32 @@ const reviewTypes = {
9296
Internal: 'INTERNAL'
9397
}
9498

99+
const SelfServiceNotificationTypes = {
100+
WORK_REQUEST_SUBMITTED: 'self-service.notifications.work-request-submitted',
101+
WORK_REQUEST_STARTED: 'self-service.notifications.work-request-started',
102+
WORK_REQUEST_REDIRECTED: 'self-service.notifications.work-request-redirected',
103+
WORK_COMPLETED: 'self-service.notifications.work-completed'
104+
}
105+
106+
const SelfServiceNotificationSettings = {
107+
[SelfServiceNotificationTypes.WORK_REQUEST_SUBMITTED]: {
108+
sendgridTemplateId: config.SENDGRID_TEMPLATES.WORK_REQUEST_SUBMITTED,
109+
cc: []
110+
},
111+
[SelfServiceNotificationTypes.WORK_REQUEST_STARTED]: {
112+
sendgridTemplateId: config.SENDGRID_TEMPLATES.WORK_REQUEST_STARTED,
113+
cc: []
114+
},
115+
[SelfServiceNotificationTypes.WORK_REQUEST_REDIRECTED]: {
116+
sendgridTemplateId: config.SENDGRID_TEMPLATES.WORK_REQUEST_REDIRECTED,
117+
cc: [...config.SELF_SERVICE_EMAIL_CC_ACCOUNTS]
118+
},
119+
[SelfServiceNotificationTypes.WORK_COMPLETED]: {
120+
sendgridTemplateId: config.SENDGRID_TEMPLATES.WORK_COMPLETED,
121+
cc: []
122+
}
123+
}
124+
95125
module.exports = {
96126
UserRoles,
97127
prizeSetTypes,
@@ -103,5 +133,7 @@ module.exports = {
103133
challengeTracks,
104134
challengeTextSortField,
105135
DiscussionTypes,
106-
reviewTypes
136+
reviewTypes,
137+
SelfServiceNotificationTypes,
138+
SelfServiceNotificationSettings
107139
}

config/default.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/**
22
* The configuration file.
33
*/
4+
const _ = require('lodash')
45
require('dotenv').config()
56
module.exports = {
67
READONLY: process.env.READONLY === 'true' || false,
@@ -86,5 +87,16 @@ module.exports = {
8687

8788
NEW_SELF_SERVICE_PROJECT_TYPE: process.env.NEW_SELF_SERVICE_PROJECT_TYPE || 'self-service',
8889

90+
SELF_SERVICE_WHITELIST_HANDLES: process.env.SELF_SERVICE_WHITELIST_HANDLES || [],
91+
92+
SENDGRID_TEMPLATES: {
93+
WORK_REQUEST_SUBMITTED: process.env.WORK_REQUEST_SUBMITTED || '',
94+
WORK_REQUEST_STARTED: process.env.WORK_REQUEST_STARTED || '',
95+
WORK_REQUEST_REDIRECTED: process.env.WORK_REQUEST_REDIRECTED || '',
96+
WORK_COMPLETED: process.env.WORK_COMPLETED || ''
97+
},
98+
99+
EMAIL_FROM: process.env.EMAIL_FROM || 'no-reply@topcoder.com',
100+
SELF_SERVICE_EMAIL_CC_ACCOUNTS: process.env.SELF_SERVICE_EMAIL_CC_ACCOUNTS ? _.map(process.env.SELF_SERVICE_EMAIL_CC_ACCOUNTS.split(','), email => ({ email })) : [],
89101
SELF_SERVICE_WHITELIST_HANDLES: process.env.SELF_SERVICE_WHITELIST_HANDLES || ['TCConnCopilot', 'sstestcopilot']
90102
}

src/common/helper.js

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1126,6 +1126,35 @@ async function getMemberById (userId) {
11261126
return {}
11271127
}
11281128

1129+
/**
1130+
* Send self service notification
1131+
* @param {String} type the notification type
1132+
* @param {Array} recipients the array of recipients in { userId || email || handle } format
1133+
* @param {Object} data the data
1134+
*/
1135+
async function sendSelfServiceNotification (type, recipients, data) {
1136+
try {
1137+
await postBusEvent(constants.Topics.Notifications, {
1138+
notifications: [
1139+
{
1140+
serviceId: 'email',
1141+
type,
1142+
details: {
1143+
from: config.EMAIL_FROM,
1144+
recipients: [...recipients],
1145+
cc: [...constants.SelfServiceNotificationSettings[type].cc],
1146+
data,
1147+
sendgridTemplateId: constants.SelfServiceNotificationSettings[type].sendgridTemplateId,
1148+
version: 'v3'
1149+
}
1150+
}
1151+
]
1152+
})
1153+
} catch (e) {
1154+
logger.debug(`Failed to post notification ${type}: ${e.message}`)
1155+
}
1156+
}
1157+
11291158
module.exports = {
11301159
wrapExpress,
11311160
autoWrapExpress,
@@ -1175,5 +1204,6 @@ module.exports = {
11751204
cancelProject,
11761205
getProjectPayment,
11771206
capturePayment,
1178-
cancelPayment
1207+
cancelPayment,
1208+
sendSelfServiceNotification
11791209
}

src/services/ChallengeService.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1096,7 +1096,16 @@ async function createChallenge (currentUser, challenge, userToken) {
10961096

10971097
// post bus event
10981098
await helper.postBusEvent(constants.Topics.ChallengeCreated, ret)
1099-
1099+
// send email notification
1100+
if (challenge.legacy.selfService && currentUser.handle) {
1101+
await helper.sendSelfServiceNotification(
1102+
constants.SelfServiceNotificationTypes.WORK_REQUEST_SUBMITTED,
1103+
[{ userId: currentUser.userId }],
1104+
{
1105+
handle: currentUser.handle,
1106+
workItemName: ret.name
1107+
})
1108+
}
11001109
return ret
11011110
}
11021111

0 commit comments

Comments
 (0)