-
Notifications
You must be signed in to change notification settings - Fork 7
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
Updated notification support #131
Conversation
@@ -20,6 +20,12 @@ const getNotifyDefaults = async (advertiserId, user) => { | |||
return notify; | |||
}; | |||
|
|||
const updateNotifications = async (campaignId) => { | |||
contactNotifier.scheduleCampaignCreated({ campaignId }); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The updateNotifications
isn't awaiting the scheduled email calls, is that intentional?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes
@@ -20,6 +20,12 @@ const getNotifyDefaults = async (advertiserId, user) => { | |||
return notify; | |||
}; | |||
|
|||
const updateNotifications = async (campaignId) => { | |||
contactNotifier.scheduleCampaignCreated({ campaignId }); | |||
contactNotifier.scheduleCampaignStarted({ campaignId }); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, have a look at the each
function within the async
package. These are probably a good candidates for running in parallel.
https://caolan.github.io/async/docs.html#each
@@ -148,16 +159,20 @@ module.exports = { | |||
const campaign = await Campaign.findById(campaignId); | |||
if (!campaign) throw new Error(`Unable to set campaign URL: no campaign found for '${campaignId}'`); | |||
campaign.url = url; | |||
return campaign.save(); | |||
await campaign.save(); | |||
await updateNotifications(campaignId); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does updateNotifications
need to be called on every mutation?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IMO yes, as any campaign field could be used within the email message itself, and changes to criteria would necessitate changes to delivery
src/schema/campaign-notification.js
Outdated
required: true, | ||
validate: { | ||
async validator(v) { | ||
const doc = await connection.model('campaign').findOne({ _id: v }, { _id: 1 }); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Change to .findById(v, { _id: 1 })
My bad for not doing that before you Cmd C+V
src/schema/campaign-notification.js
Outdated
cc: [String], | ||
bcc: [String], | ||
|
||
subject: String, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should subject, text, and html all be required? Well, maybe not text
// Run every 10 minutes | ||
const job = new CronJob({ | ||
cronTime: '* */10 * * * *', | ||
onTick: this.check, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this need a bind?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, the context: this
parameter handles the binding
src/services/contact-notifier.js
Outdated
async sendInternalCampaignCreated({ campaign }) { | ||
const html = await emailTemplates.render('internal/campaign.created', { campaign }); | ||
async scheduleCampaignCreated({ campaignId }) { | ||
const campaign = await Campaign.findOne({ _id: campaignId }); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
findById
src/services/contact-notifier.js
Outdated
}, | ||
|
||
async scheduleCampaignEnded({ campaignId }) { | ||
const campaign = await Campaign.findOne({ _id: campaignId }); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
findById
src/services/contact-notifier.js
Outdated
const html = await emailTemplates.render('external/campaign.created', { campaign }); | ||
const subject = 'A new campaign was created!'; | ||
async scheduleCampaignStarted({ campaignId }) { | ||
const campaign = await Campaign.findOne({ _id: campaignId }); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
findById
const to = await this.resolveAddresses(campaign.get('notify.internal')); | ||
return this.send({ to, subject, html }); | ||
const to = await this.resolveAddresses(campaign.get('notify.external')); | ||
const cc = await this.resolveAddresses(campaign.get('notify.internal')); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
All these functions seem pretty repetitive... any way we can clean them up / consolidate?
src/services/contact-notifier.js
Outdated
notification.set('status', 'Sent'); | ||
output.write('✉️ ✉️ ✉️ Successfully sent a notification!'); | ||
} catch (e) { | ||
notification.set('error', e); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just to make it more clear... e.message
please :)
src/services/contact-notifier.js
Outdated
} catch (e) { | ||
notification.set('error', e); | ||
notification.set('status', 'Errored'); | ||
notification.save(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove me
Resolves #49