Vulcan/packages/nova-newsletter/lib/server/mailchimp/mailchimp_list.js

105 lines
2.9 KiB
JavaScript
Raw Normal View History

// methods to add and remove users from MailChimp lists
2016-02-22 10:48:55 +09:00
import Users from 'meteor/nova:users';
import MailChimp from './mailchimp_api.js';
2016-02-22 10:48:55 +09:00
2016-03-18 11:53:46 +09:00
const MailChimpList = {};
MailChimpList.add = function(userOrEmail, confirm, done){
var apiKey = Telescope.settings.get('mailChimpAPIKey');
var listId = Telescope.settings.get('mailChimpListId');
2016-02-22 10:48:55 +09:00
var user, email;
2016-05-07 18:10:33 +09:00
confirm = (typeof confirm === 'undefined') ? false : confirm; // default to no confirmation
2016-02-22 10:48:55 +09:00
// not sure if it's really necessary that the function take both user and email?
if (typeof userOrEmail === "string") {
user = null;
email = userOrEmail;
} else if (typeof userOrEmail === "object") {
user = userOrEmail;
email = Users.getEmail(user);
if (!email)
throw 'User must have an email address';
}
// add a user to a MailChimp list.
// called when a new user is created, or when an existing user fills in their email
if(!!apiKey && !!listId){
try {
console.log('// Adding "'+email+'" to MailChimp list…');
var api = new MailChimp(apiKey);
var subscribeOptions = {
id: listId,
email: {"email": email},
double_optin: confirm
};
// subscribe user
var subscribe = api.call('lists', 'subscribe', subscribeOptions);
// mark user as subscribed
if (!!user) {
Users.methods.setSetting(user._id, 'newsletter_subscribeToNewsletter', true);
2016-02-22 10:48:55 +09:00
}
console.log("// User subscribed");
return subscribe;
} catch (error) {
throw new Meteor.Error("subscription-failed", error.message);
}
2016-03-18 11:53:46 +09:00
} else {
throw new Meteor.Error("Please provide your MailChimp API key and list ID", error.message);
2016-02-22 10:48:55 +09:00
}
};
2016-05-23 09:07:13 +02:00
MailChimpList.remove = (user) => {
const apiKey = Telescope.settings.get('mailChimpAPIKey');
const listId = Telescope.settings.get('mailChimpListId');
const email = Users.getEmail(user);
if (!email) {
throw 'User must have an email address';
}
// remove a user to a MailChimp list.
// called from the user's account
if(!!apiKey && !!listId){
try {
console.log('// Removing "'+email+'" to MailChimp list…');
var api = new MailChimp(apiKey);
var 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)
2016-05-23 09:07:13 +02:00
};
// unsubscribe user
2016-05-23 09:07:13 +02:00
var subscribe = api.call('lists', 'unsubscribe', subscribeOptions);
// mark user as unsubscribed
Users.methods.setSetting(user._id, 'newsletter_subscribeToNewsletter', false);
2016-05-23 09:07:13 +02:00
console.log("// User unsubscribed");
return subscribe;
} catch (error) {
throw new Meteor.Error("unsubscription-failed", error.message);
}
} else {
throw new Meteor.Error("Please provide your MailChimp API key and list ID", error.message);
}
};
2016-03-18 11:53:46 +09:00
export default MailChimpList;