-
Notifications
You must be signed in to change notification settings - Fork 6
PM- 3351 Send notifications to resources on manual phase change #70
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
Changes from all commits
6d39268
f7ee6df
23c3a60
c17d651
95fd2e6
57c5063
4ecb6bf
e814a6d
b39f104
b1812d7
1bd719a
b235f71
eaca715
aa80953
629b0b2
979eb82
867eec4
1c0edd7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -91,7 +91,7 @@ workflows: | |
| only: | ||
| - develop | ||
| - security | ||
| - PM-3327 | ||
| - PM-3351 | ||
|
|
||
| - "build-qa": | ||
| context: org-global | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1638,6 +1638,72 @@ async function sendSelfServiceNotification(type, recipients, data) { | |
| } | ||
| } | ||
|
|
||
| /** | ||
| * Build payload for phase change email notification | ||
| * @param {String} challenge Id | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [💡 |
||
| * @param {String} challenge name | ||
| * @param {String} challenge phase name | ||
| * @param {String} operation to be performed on the phase - open | close | reopen | ||
| * @param {String|Date} at - The date/time when the phase opened/closed | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [ |
||
| */ | ||
| function buildPhaseChangeEmailData({ challengeId, challengeName, phaseName, operation, at }) { | ||
| const isOpen = operation === 'open' || operation === 'reopen'; | ||
| const isClose = operation === 'close'; | ||
|
|
||
| return { | ||
| challengeURL: `${config.CHALLENGE_URL}/${challengeId}`, | ||
| challengeName, | ||
| phaseOpen: isOpen ? phaseName : null, | ||
| phaseOpenDate: isOpen ? at : null, | ||
| phaseClose: isClose ? phaseName : null, | ||
| phaseCloseDate: isClose ? at : null, | ||
| }; | ||
| } | ||
|
|
||
|
|
||
| /** | ||
| * Send phase change notification | ||
| * @param {String} type the notification type | ||
| * @param {Array} recipients the array of recipients emails | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [ |
||
| * @param {Object} data the data | ||
| */ | ||
| async function sendPhaseChangeNotification(type, recipients, data) { | ||
| try { | ||
| const settings = constants.PhaseChangeNotificationSettings?.[type]; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [ |
||
|
|
||
| if (!settings) { | ||
| logger.debug(`sendPhaseChangeNotification: unknown type ${type}`); | ||
| return; | ||
| } | ||
|
|
||
| if (!settings.sendgridTemplateId) { | ||
| logger.debug( | ||
| `sendPhaseChangeNotification: sendgridTemplateId not configured for type ${type}` | ||
| ); | ||
| return; | ||
| } | ||
| const safeRecipients = Array.isArray(recipients) ? recipients.filter(Boolean) : []; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [💡 |
||
|
|
||
| if (!safeRecipients.length) { | ||
| logger.debug(`sendPhaseChangeNotification: no recipients for type ${type}`); | ||
| return; | ||
| } | ||
|
|
||
| await postBusEvent('external.action.email', | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [ |
||
| { | ||
| from: config.EMAIL_FROM, | ||
| replyTo: config.EMAIL_FROM, | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [ |
||
| recipients: safeRecipients, | ||
| data: data, | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [❗❗ |
||
| sendgrid_template_id: settings.sendgridTemplateId, | ||
| version: 'v3', | ||
| }, | ||
| ); | ||
| } catch (e) { | ||
| logger.debug(`Failed to post notification ${type}: ${e.message}`); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [ |
||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Submit a request to zendesk | ||
| * @param {Object} request the request | ||
|
|
@@ -1756,6 +1822,8 @@ module.exports = { | |
| setToInternalCache, | ||
| flushInternalCache, | ||
| removeNullProperties, | ||
| buildPhaseChangeEmailData, | ||
| sendPhaseChangeNotification | ||
| }; | ||
|
|
||
| logger.buildService(module.exports); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -813,9 +813,74 @@ async function partiallyUpdateChallengePhase(currentUser, challengeId, id, data) | |
| _.assignIn({ id: result.id }, data) | ||
| ); | ||
| await postChallengeUpdatedNotification(challengeId); | ||
|
|
||
| // send notification logic | ||
| try { | ||
| const shouldNotifyClose = Boolean(isClosingPhase); | ||
| const shouldNotifyOpen = Boolean(isOpeningPhase); // includes reopen | ||
|
|
||
| if (shouldNotifyClose || shouldNotifyOpen) { | ||
| // Single template - single type | ||
| const notificationType = "PHASE_CHANGE"; | ||
|
|
||
| const operation = shouldNotifyClose | ||
| ? "close" | ||
| : (isReopeningPhase ? "reopen" : "open"); | ||
|
|
||
| const at = shouldNotifyClose | ||
himaniraghav3 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ? (result.actualEndDate || new Date().toISOString()) | ||
| : (result.actualStartDate || new Date().toISOString()); | ||
|
|
||
| // fetch challenge name | ||
| const challenge = await prisma.challenge.findUnique({ | ||
| where: { id: challengeId }, | ||
| select: { name: true }, | ||
| }); | ||
|
|
||
| const challengeName = challenge?.name; | ||
|
|
||
| // build recipients | ||
| const resources = await helper.getChallengeResources(challengeId); | ||
|
|
||
| const recipients = Array.from( | ||
| new Set( | ||
| (resources || []) | ||
| .map(r => r?.email || r?.memberEmail) | ||
| .filter(Boolean) | ||
| .map(e => String(e).trim().toLowerCase()) | ||
| ) | ||
| ); | ||
|
|
||
| if (!recipients.length) { | ||
| logger.debug( | ||
| `phase change notification skipped: no recipients for challenge ${challengeId}` | ||
| ); | ||
| return _.omit(result, constants.auditFields); | ||
| } | ||
|
|
||
| // build payload that matches the SendGrid HTML template | ||
| const phaseName = result.name || data.name || challengePhase.name; | ||
|
|
||
| const payload = helper.buildPhaseChangeEmailData({ | ||
| challengeId, | ||
| challengeName, | ||
| phaseName, | ||
| operation, | ||
| at, | ||
| }); | ||
|
|
||
| await helper.sendPhaseChangeNotification(notificationType, recipients, payload); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [ |
||
| } | ||
| } catch (e) { | ||
| logger.debug( | ||
| `phase change notification failed for challenge ${challengeId}, phase ${id}: ${e.message}` | ||
| ); | ||
| } | ||
|
|
||
| return _.omit(result, constants.auditFields); | ||
| } | ||
|
|
||
|
|
||
| partiallyUpdateChallengePhase.schema = { | ||
| currentUser: Joi.any(), | ||
| challengeId: Joi.id(), | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.