Skip to content

Commit

Permalink
Case assignee updated (#296)
Browse files Browse the repository at this point in the history
  • Loading branch information
kaihendry authored and nbiton committed Jun 20, 2018
1 parent 598dd8a commit bb36702
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 18 deletions.
67 changes: 49 additions & 18 deletions imports/api/rest/post-process-db-change-message.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { Meteor } from 'meteor/meteor'
import { Email } from 'meteor/email'
import MessagePayloads from '../message-payloads'
import emailTemplate from '../../email-templates/user-invited-to-case'
import caseUserInvitedTemplate from '../../email-templates/user-invited-to-case'
import caseAssigneeUpdateTemplate from '../../email-templates/case-assignee-updated'
import caseNewTemplate from '../../email-templates/case-new'

export default (req, res) => {
if (req.query.accessToken !== process.env.API_ACCESS_TOKEN) {
Expand All @@ -12,36 +14,65 @@ export default (req, res) => {
const message = req.body
console.log('Incoming to /api/db-change-message/process', message)

if (MessagePayloads.findOne({notification_id: message.notification_id})) {
res.send(400, `Duplicate message ${message.notification_id}`)
return
}

MessagePayloads.insert(message)

const {
notification_type: type,
invitee_user_id: inviteeId,
case_title: caseTitle,
case_id: caseId
} = message

let userId, templateFunction

switch (type) {
case 'case_new':
// https://github.com/unee-t/sns2email/issues/1
// When a new case is created, we need to inform the person who is assigned to that case.
userId = message.assignee_user_id
templateFunction = caseNewTemplate
break

case 'case_assignee_updated':
// https://github.com/unee-t/sns2email/issues/2
// When the user assigned to a case change, we need to inform the person who is the new assignee to that case.
userId = message.assignee_user_id
templateFunction = caseAssigneeUpdateTemplate
break

case 'case_user_invited':
const invitee = Meteor.users.findOne({'bugzillaCreds.id': parseInt(inviteeId)})
if (!invitee) {
console.error('Could deliver message to missing user of BZ ID: ' + inviteeId)
return
}
const emailAddr = invitee.emails[0].address
const emailContent = emailTemplate(invitee, caseTitle, caseId)
try {
Email.send(Object.assign({
to: emailAddr,
from: process.env.FROM_EMAIL
}, emailContent))
console.log('Sent', emailAddr, 'notification type:', type)
} catch (e) {
console.error(`An error occurred while sending an email to ${emailAddr}`)
}
// https://github.com/unee-t/sns2email/issues/3
userId = message.invitee_user_id
templateFunction = caseUserInvitedTemplate
break

default:
console.log('Unimplemented type:', type)
res.send(400)
return
}

const assignee = Meteor.users.findOne({'bugzillaCreds.id': parseInt(userId)})
if (!assignee) {
console.error('Could deliver message to missing user of BZ ID: ' + userId)
res.send(400)
return
}
const emailAddr = assignee.emails[0].address
const emailContent = templateFunction(assignee, caseTitle, caseId)
try {
Email.send(Object.assign({
to: emailAddr,
from: process.env.FROM_EMAIL
}, emailContent))
console.log('Sent', emailAddr, 'notification type:', type)
} catch (e) {
console.error(`An error ${e} occurred while sending an email to ${emailAddr}`)
}

res.send(200)
}
27 changes: 27 additions & 0 deletions imports/email-templates/case-assignee-updated.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import url from 'url'

export default (assignee, caseTitle, caseId) => ({
subject: `Assigned to "${caseTitle}"`,
html: `<img src="cid:logo@unee-t.com"/>
<p>Hi ${assignee.profile.name || assignee.emails[0].address.split('@')[0]},</p>
<p>You have been assigned the case <strong>${caseTitle}</strong> in Unee-T.</p>
<p>Please follow <a href='${url.resolve(process.env.ROOT_URL, `/case/${caseId}`)}'>${url.resolve(process.env.ROOT_URL, `/case/${caseId}`)}</a> to participate.</p>
<p><a href=https://unee-t.com>Unee-T</a>, managing and sharing 'To Do's for your properties has never been easier.</p>
`,
text: `Hi ${assignee.profile.name || assignee.emails[0].address.split('@')[0]},
You have been assigned the case ${caseTitle}.
Please follow ${url.resolve(process.env.ROOT_URL, `/case/${caseId}`)} to participate.
Unee-T, managing and sharing 'To Do's for your properties has never been easier.
`,
attachments: [{
path: 'https://s3-ap-southeast-1.amazonaws.com/prod-media-unee-t/2018-06-14/unee-t_logo_email.png',
cid: 'logo@unee-t.com'
}]
})
27 changes: 27 additions & 0 deletions imports/email-templates/case-new.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import url from 'url'

export default (assignee, caseTitle, caseId) => ({
subject: `New case "${caseTitle}"`,
html: `<img src="cid:logo@unee-t.com"/>
<p>Hi ${assignee.profile.name || assignee.emails[0].address.split('@')[0]},</p>
<p>You have been assigned the new case <strong>${caseTitle}</strong> in Unee-T.</p>
<p>Please follow <a href='${url.resolve(process.env.ROOT_URL, `/case/${caseId}`)}'>${url.resolve(process.env.ROOT_URL, `/case/${caseId}`)}</a> to participate.</p>
<p><a href=https://unee-t.com>Unee-T</a>, managing and sharing 'To Do's for your properties has never been easier.</p>
`,
text: `Hi ${assignee.profile.name || assignee.emails[0].address.split('@')[0]},
You have been assigned the new case ${caseTitle}.
Please follow ${url.resolve(process.env.ROOT_URL, `/case/${caseId}`)} to participate.
Unee-T, managing and sharing 'To Do's for your properties has never been easier.
`,
attachments: [{
path: 'https://s3-ap-southeast-1.amazonaws.com/prod-media-unee-t/2018-06-14/unee-t_logo_email.png',
cid: 'logo@unee-t.com'
}]
})

0 comments on commit bb36702

Please sign in to comment.