-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: assignment email notifications
- Loading branch information
Showing
30 changed files
with
993 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,10 +28,10 @@ const assesmentSecondReviewChange: EmailTemplateProvider = ( | |
emailTo: [34, 71], // Temporary IDs to handle email recipients | ||
emailCC: [], | ||
tag: 'assesment-second-review-change', | ||
subject: `${initiator} has requested a 2nd Review for ${type} - ${ccbcNumber}`, | ||
subject: `${initiator.givenName} has requested a 2nd Review for ${type} - ${ccbcNumber}`, | ||
body: ` | ||
<h1>${initiator} requested a 2nd Review on ${type} - ${ccbcNumber}</h1> | ||
<p>${initiator} requested a 2nd Review on ${type} - ${ccbcNumber}, <a href='${url}/analyst/application/${applicationId}/assessments/${slug}'>Click here</a> to view the ${type} in the CCBC Portal<p> | ||
<h1>${initiator.givenName} requested a 2nd Review on ${type} - ${ccbcNumber}</h1> | ||
<p>${initiator.givenName} requested a 2nd Review on ${type} - ${ccbcNumber}, <a href='${url}/analyst/application/${applicationId}/assessments/${slug}'>Click here</a> to view the ${type} in the CCBC Portal<p> | ||
<p>To unsubscribe from this notification please forward this email with your request to <a href="mailto:[email protected]">[email protected]<a/></p> | ||
`, | ||
}; | ||
|
128 changes: 128 additions & 0 deletions
128
app/backend/lib/emails/templates/assessmentAssigneeChange.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
import { Context } from 'backend/lib/ches/sendEmailMerge'; | ||
import { | ||
EmailTemplate, | ||
EmailTemplateProvider, | ||
getEmailRecipients, | ||
} from '../handleEmailNotification'; | ||
import { performQuery } from '../../graphql'; | ||
|
||
const getCCBCUsersByIds = ` | ||
query getCCBCUsersByIds($_rowIds: [Int!]!) { | ||
allCcbcUsers(filter: {rowId: {in: $_rowIds}}) { | ||
edges { | ||
node { | ||
rowId | ||
givenName | ||
} | ||
} | ||
} | ||
} | ||
`; | ||
|
||
const getUsers = async (ids: number[], req: any) => { | ||
const results = await performQuery(getCCBCUsersByIds, { _rowIds: ids }, req); | ||
return results?.data?.allCcbcUsers?.edges.reduce((acc, user) => { | ||
acc[user.node.rowId] = user.node.givenName; | ||
return acc; | ||
}, {}); | ||
}; | ||
|
||
const getGroupedContentList = (assessments: any[], property: string) => { | ||
const grouped = {}; | ||
return assessments.reduce((result, item) => { | ||
const key = item[property]; | ||
if (!result[key]) { | ||
grouped[key] = []; | ||
} | ||
grouped[key].push(item); | ||
return grouped; | ||
}, {}); | ||
}; | ||
|
||
const assessmentAssigneeChange: EmailTemplateProvider = async ( | ||
applicationId: string, | ||
url: string, | ||
initiator: string, | ||
params: any, | ||
req?: any | ||
): Promise<EmailTemplate> => { | ||
const assessmentsGrouped = getGroupedContentList( | ||
params.assignments, | ||
'assigneeEmail' | ||
); | ||
|
||
/** | ||
* Group the notification content for each assignee | ||
* So we do not send multiple emails but grouped notifications | ||
*/ | ||
const request = Object.entries(assessmentsGrouped).map( | ||
async ([assignee, assessments]) => { | ||
const emailTo = await getEmailRecipients(req, [], [assignee]); | ||
const contentList = getGroupedContentList( | ||
assessments as Array<any>, | ||
'updatedBy' | ||
); | ||
|
||
const ccbcUserList = await getUsers( | ||
Object.keys(contentList).map((key) => Number(key)), | ||
req | ||
); | ||
|
||
/** | ||
* Group each list of notifications by the assignor | ||
*/ | ||
const actions = Object.entries(contentList).map( | ||
([assignor, assignments]) => { | ||
const alerts = (assignments as Array<any>).map((assignment) => { | ||
return { | ||
url: `${url}/analyst/application/${assignment.applicationId}/assessments/${assignment.assessmentType}`, | ||
type: assignment.assessmentType, | ||
ccbcNumber: assignment.ccbcNumber, | ||
}; | ||
}); | ||
return { | ||
assignors: ccbcUserList[assignor], | ||
alerts, | ||
}; | ||
} | ||
); | ||
const assignorList = Object.keys(contentList).map( | ||
(key) => ccbcUserList[key] | ||
); | ||
|
||
return { | ||
to: emailTo, | ||
cc: [], | ||
context: { | ||
assignee, | ||
assignorList, | ||
actions, | ||
}, | ||
delayTS: 0, | ||
tag: 'assignment-assignee-change', | ||
} as Context; | ||
} | ||
); | ||
|
||
const contexts = await Promise.all(request); | ||
|
||
return { | ||
emailTo: [], | ||
emailCC: [], | ||
tag: 'assignment-assignee-change', | ||
subject: `{% if assignorList.length > 1 %} | ||
{{ assignorList | slice(2) | join(", ") }} and others has(/have) assigned you one or more assessment(s) | ||
{% else %} {{ assignorList | join(" ") }} has assigned you one or more assessment(s) | ||
{% endif %}`, | ||
body: `{% for action in actions %} | ||
{{ action.assignors }} has assigned you the following assessment(s): | ||
<ul>{% for alert in action.alerts %} | ||
<li><a href='{{ alert.url }}'>{{ alert.type | capitalize }}</a> for {{ alert.ccbcNumber }}</li> | ||
{% endfor %}</ul> | ||
{% endfor %}`, | ||
contexts, | ||
params: { assessmentsGrouped }, | ||
}; | ||
}; | ||
|
||
export default assessmentAssigneeChange; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.