From 778ad9b64db76369bc37537c39d2be52d3493c35 Mon Sep 17 00:00:00 2001 From: SachaG Date: Tue, 18 Apr 2017 09:56:38 +0900 Subject: [PATCH] =?UTF-8?q?Newsletter=20package=20shouldn=E2=80=99t=20thro?= =?UTF-8?q?w=20errors=20when=20no=20settings=20are=20present?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../lib/server/integrations/mailchimp.js | 177 +++++++++--------- .../lib/server/integrations/sample.js | 96 +++++----- .../lib/server/integrations/sendy.js | 113 +++++------ 3 files changed, 202 insertions(+), 184 deletions(-) diff --git a/packages/vulcan-newsletter/lib/server/integrations/mailchimp.js b/packages/vulcan-newsletter/lib/server/integrations/mailchimp.js index c37eb8cd4..37dd8347c 100644 --- a/packages/vulcan-newsletter/lib/server/integrations/mailchimp.js +++ b/packages/vulcan-newsletter/lib/server/integrations/mailchimp.js @@ -13,110 +13,115 @@ API */ -const { apiKey, listId, fromName, fromEmail } = getSetting('mailchimp'); +const settings = getSetting('mailchimp'); -const mailChimpAPI = new MailChimpNPM.MailChimpAPI(apiKey, { version : '2.0' }); +if (settings) { + + const { apiKey, listId, fromName, fromEmail } = settings; + const mailChimpAPI = new MailChimpNPM.MailChimpAPI(apiKey, { version : '2.0' }); -const callSyncAPI = ( section, method, options, callback ) => { - try { - var wrapped = Meteor.wrapAsync( mailChimpAPI.call, mailChimpAPI ); - return wrapped( section, method, options ); - } catch ( error ) { - // A workaround for https://github.com/meteor/meteor/issues/2774 - console.log('// MailChimp API error') - console.log(error) - } -}; - -/* - -Methods - -*/ - -Newsletters.mailchimp = { - - // add a user to a MailChimp list. - // called when a new user is created, or when an existing user fills in their email - subscribe(email, confirm = false) { + const callSyncAPI = ( section, method, options, callback ) => { try { - const subscribeOptions = { - id: listId, - email: {email: email}, - double_optin: confirm - }; - // subscribe user - const subscribe = callSyncAPI('lists', 'subscribe', subscribeOptions); - return {result: 'subscribed', ...subscribe}; - } catch (error) { - // if the email is already in the Mailchimp list, no need to throw an error - if (error.message === "214") { - return {result: 'already-subscribed'}; - } - throw new Error("subscription-failed", error.message); + var wrapped = Meteor.wrapAsync( mailChimpAPI.call, mailChimpAPI ); + return wrapped( section, method, options ); + } catch ( error ) { + // A workaround for https://github.com/meteor/meteor/issues/2774 + console.log('// MailChimp API error') + console.log(error) } - }, + }; - // remove a user to a MailChimp list. - // called from the user's account - unsubscribe(email) { - try { - const subscribeOptions = { - id: listId, - email: {email: email}, - delete_member: true // delete the member from the list to make it possible for him to *resubscribe* via API (mailchimp's spam prevention policy) - }; - // unsubscribe user - const subscribe = callSyncAPI('lists', 'unsubscribe', subscribeOptions); - return {result: 'unsubscribed', ...subscribe}; - } catch (error) { - throw new Error("unsubscribe-failed", error.message); - } - }, + /* - send({ title, subject, text, html, isTest = false }) { + Methods - try { + */ - var campaignOptions = { - type: 'regular', - options: { - list_id: listId, - subject: subject, - from_email: fromEmail, - from_name: fromName - }, - content: { - html: html, - text: text + Newsletters.mailchimp = { + + // add a user to a MailChimp list. + // called when a new user is created, or when an existing user fills in their email + subscribe(email, confirm = false) { + try { + const subscribeOptions = { + id: listId, + email: {email: email}, + double_optin: confirm + }; + // subscribe user + const subscribe = callSyncAPI('lists', 'subscribe', subscribeOptions); + return {result: 'subscribed', ...subscribe}; + } catch (error) { + // if the email is already in the Mailchimp list, no need to throw an error + if (error.message === "214") { + return {result: 'already-subscribed'}; } - }; + throw new Error("subscription-failed", error.message); + } + }, - // create campaign - const mailchimpNewsletter = callSyncAPI('campaigns', 'create', campaignOptions); + // remove a user to a MailChimp list. + // called from the user's account + unsubscribe(email) { + try { + const subscribeOptions = { + id: listId, + email: {email: email}, + delete_member: true // delete the member from the list to make it possible for him to *resubscribe* via API (mailchimp's spam prevention policy) + }; + // unsubscribe user + const subscribe = callSyncAPI('lists', 'unsubscribe', subscribeOptions); + return {result: 'unsubscribed', ...subscribe}; + } catch (error) { + throw new Error("unsubscribe-failed", error.message); + } + }, - console.log('// Newsletter created'); - // console.log(campaign) + send({ title, subject, text, html, isTest = false }) { - const scheduledMoment = moment().utcOffset(0).add(1, 'hours'); - const scheduledTime = scheduledMoment.format("YYYY-MM-DD HH:mm:ss"); + try { - const scheduleOptions = { - cid: mailchimpNewsletter.id, - schedule_time: scheduledTime - }; + var campaignOptions = { + type: 'regular', + options: { + list_id: listId, + subject: subject, + from_email: fromEmail, + from_name: fromName + }, + content: { + html: html, + text: text + } + }; - // schedule campaign - const schedule = callSyncAPI('campaigns', 'schedule', scheduleOptions); // eslint-disable-line + // create campaign + const mailchimpNewsletter = callSyncAPI('campaigns', 'create', campaignOptions); - console.log('// Newsletter scheduled for '+scheduledTime); + console.log('// Newsletter created'); + // console.log(campaign) - return mailchimpNewsletter; + const scheduledMoment = moment().utcOffset(0).add(1, 'hours'); + const scheduledTime = scheduledMoment.format("YYYY-MM-DD HH:mm:ss"); - } catch (error) { - console.log(error); - return false; + const scheduleOptions = { + cid: mailchimpNewsletter.id, + schedule_time: scheduledTime + }; + + // schedule campaign + const schedule = callSyncAPI('campaigns', 'schedule', scheduleOptions); // eslint-disable-line + + console.log('// Newsletter scheduled for '+scheduledTime); + + return mailchimpNewsletter; + + } catch (error) { + console.log(error); + return false; + } } + } } \ No newline at end of file diff --git a/packages/vulcan-newsletter/lib/server/integrations/sample.js b/packages/vulcan-newsletter/lib/server/integrations/sample.js index 6e7e70688..4789fd147 100644 --- a/packages/vulcan-newsletter/lib/server/integrations/sample.js +++ b/packages/vulcan-newsletter/lib/server/integrations/sample.js @@ -4,67 +4,75 @@ This is a sample template for future integrations. */ +import { getSetting } from 'meteor/vulcan:core'; +import Newsletters from '../../modules/collection.js'; + /* API */ -const {server, apiKey, listId, somethingElse } = getSetting('providerName'); +const settings = getSetting('providerName'); -const MyProviderAPI = new ProviderAPI(server, apiKey); +if (settings) { -const subscribeSync = options => { - try { - const wrapped = Meteor.wrapAsync( MyProviderAPI.subscribe, MyProviderAPI ); - return wrapped( options ); - } catch ( error ) { - console.log(error) - } -}; + const {server, apiKey, listId, somethingElse } = settings; + const MyProviderAPI = new ProviderAPI(server, apiKey); -const unsubscribeSync = options => { - try { - const wrapped = Meteor.wrapAsync( MyProviderAPI.unsubscribe, MyProviderAPI ); - return wrapped( options ); - } catch ( error ) { - console.log(error) - } -}; + const subscribeSync = options => { + try { + const wrapped = Meteor.wrapAsync( MyProviderAPI.subscribe, MyProviderAPI ); + return wrapped( options ); + } catch ( error ) { + console.log(error) + } + }; -const sendSync = options => { - try { - const wrapped = Meteor.wrapAsync( MyProviderAPI.send, MyProviderAPI ); - return wrapped( options ); - } catch ( error ) { - console.log(error) - } -}; + const unsubscribeSync = options => { + try { + const wrapped = Meteor.wrapAsync( MyProviderAPI.unsubscribe, MyProviderAPI ); + return wrapped( options ); + } catch ( error ) { + console.log(error) + } + }; -/* + const sendSync = options => { + try { + const wrapped = Meteor.wrapAsync( MyProviderAPI.send, MyProviderAPI ); + return wrapped( options ); + } catch ( error ) { + console.log(error) + } + }; -Methods + /* -*/ + Methods -Newsletters.providerName = { + */ - subscribe(email) { - return subscribeSync({email}); - }, + Newsletters.providerName = { - unsubscribe(email) { - return unsubscribeSync({email}); - }, + subscribe(email) { + return subscribeSync({email}); + }, + + unsubscribe(email) { + return unsubscribeSync({email}); + }, + + send({ title, subject, text, html, isTest = false }) { + const options = { + title, + subject, + text, + html + }; + return sendSync(options); + } - send({ title, subject, text, html, isTest = false }) { - const options = { - title, - subject, - text, - html - }; - return sendSync(options); } } \ No newline at end of file diff --git a/packages/vulcan-newsletter/lib/server/integrations/sendy.js b/packages/vulcan-newsletter/lib/server/integrations/sendy.js index 55b997df5..7e370e1c1 100644 --- a/packages/vulcan-newsletter/lib/server/integrations/sendy.js +++ b/packages/vulcan-newsletter/lib/server/integrations/sendy.js @@ -8,72 +8,77 @@ API */ -const {server, apiKey, listId, fromName, fromEmail, replyTo } = getSetting('sendy'); +const settings = getSetting('sendy'); -const SendyAPI = new Sendy(server, apiKey); +if (settings) { + + const { server, apiKey, listId, fromName, fromEmail, replyTo } = settings; + const SendyAPI = new Sendy(server, apiKey); -const subscribeSync = options => { - try { - const wrapped = Meteor.wrapAsync( SendyAPI.subscribe, SendyAPI ); - return wrapped( options ); - } catch ( error ) { - console.log('// Sendy API error') - console.log(error) - if (error.message === 'Already subscribed.') { - return {result: 'already-subscribed'} + const subscribeSync = options => { + try { + const wrapped = Meteor.wrapAsync( SendyAPI.subscribe, SendyAPI ); + return wrapped( options ); + } catch ( error ) { + console.log('// Sendy API error') + console.log(error) + if (error.message === 'Already subscribed.') { + return {result: 'already-subscribed'} + } } - } -}; + }; -const unsubscribeSync = options => { - try { - const wrapped = Meteor.wrapAsync( SendyAPI.unsubscribe, SendyAPI ); - return wrapped( options ); - } catch ( error ) { - console.log('// Sendy API error') - console.log(error) - } -}; + const unsubscribeSync = options => { + try { + const wrapped = Meteor.wrapAsync( SendyAPI.unsubscribe, SendyAPI ); + return wrapped( options ); + } catch ( error ) { + console.log('// Sendy API error') + console.log(error) + } + }; -const createCampaignSync = options => { - try { - const wrapped = Meteor.wrapAsync( SendyAPI.createCampaign, SendyAPI ); - return wrapped( options ); - } catch ( error ) { - console.log('// Sendy API error') - console.log(error) - } -}; + const createCampaignSync = options => { + try { + const wrapped = Meteor.wrapAsync( SendyAPI.createCampaign, SendyAPI ); + return wrapped( options ); + } catch ( error ) { + console.log('// Sendy API error') + console.log(error) + } + }; -/* + /* -Methods + Methods -*/ + */ -Newsletters.sendy = { + Newsletters.sendy = { - subscribe(email) { - return subscribeSync({email, list_id: listId}); - }, + subscribe(email) { + return subscribeSync({email, list_id: listId}); + }, - unsubscribe(email) { - return unsubscribeSync({email, list_id: listId}); - }, + unsubscribe(email) { + return unsubscribeSync({email, list_id: listId}); + }, + + send({ title, subject, text, html, isTest = false }) { + const params = { + from_name: fromName, + from_email: fromEmail, + reply_to: replyTo, + title: subject, + subject: subject, + plain_text: text, + html_text: html, + send_campaign: !isTest, + list_ids: listId + }; + return createCampaignSync(params); + } - send({ title, subject, text, html, isTest = false }) { - const params = { - from_name: fromName, - from_email: fromEmail, - reply_to: replyTo, - title: subject, - subject: subject, - plain_text: text, - html_text: html, - send_campaign: !isTest, - list_ids: listId - }; - return createCampaignSync(params); } } \ No newline at end of file