Newsletter package shouldn’t throw errors when no settings are present

This commit is contained in:
SachaG 2017-04-18 09:56:38 +09:00
parent 7e56a7180d
commit 778ad9b64d
3 changed files with 202 additions and 184 deletions

View file

@ -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;
}
}
}
}

View file

@ -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);
}
}

View file

@ -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);
}
}