2016-06-23 17:24:58 +09:00
|
|
|
import Users from 'meteor/nova:users';
|
|
|
|
import MailChimp from './mailchimp_api.js';
|
2016-12-12 15:00:56 +09:00
|
|
|
import { getSetting } from 'meteor/nova:core';
|
2016-02-22 10:48:55 +09:00
|
|
|
|
2016-03-18 11:53:46 +09:00
|
|
|
const MailChimpList = {};
|
|
|
|
|
|
|
|
MailChimpList.add = function(userOrEmail, confirm, done){
|
|
|
|
|
2016-12-12 15:00:56 +09:00
|
|
|
var apiKey = getSetting('mailChimpAPIKey');
|
|
|
|
var listId = getSetting('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 {
|
|
|
|
|
2016-11-26 02:46:55 +08:00
|
|
|
console.log('// Adding "'+email+'" to MailChimp list…'); // eslint-disable-line
|
2016-02-22 10:48:55 +09:00
|
|
|
|
|
|
|
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) {
|
2016-12-07 15:30:40 +09:00
|
|
|
Users.setSetting(user, 'newsletter_subscribeToNewsletter', true);
|
2016-02-22 10:48:55 +09:00
|
|
|
}
|
|
|
|
|
2016-11-26 02:46:55 +08:00
|
|
|
console.log("// User subscribed"); // eslint-disable-line
|
2016-02-22 10:48:55 +09:00
|
|
|
|
2016-12-27 11:44:35 +01:00
|
|
|
return {actionResult: 'subscribed', ...subscribe};
|
2016-02-22 10:48:55 +09:00
|
|
|
|
|
|
|
} catch (error) {
|
2016-12-07 15:30:40 +09:00
|
|
|
// if user is already subscribed, update setting
|
2016-12-08 23:48:16 +01:00
|
|
|
if (error.error == 214) {
|
2016-12-07 15:30:40 +09:00
|
|
|
Users.setSetting(user, 'newsletter_subscribeToNewsletter', true);
|
|
|
|
}
|
2016-02-22 10:48:55 +09:00
|
|
|
throw new Meteor.Error("subscription-failed", error.message);
|
|
|
|
}
|
2016-03-18 11:53:46 +09:00
|
|
|
} else {
|
2016-11-26 02:46:55 +08:00
|
|
|
throw new Meteor.Error("Please provide your MailChimp API key and list ID");
|
2016-02-22 10:48:55 +09:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-05-23 09:07:13 +02:00
|
|
|
MailChimpList.remove = (user) => {
|
2016-12-12 15:00:56 +09:00
|
|
|
const apiKey = getSetting('mailChimpAPIKey');
|
|
|
|
const listId = getSetting('mailChimpListId');
|
2016-05-23 09:07:13 +02:00
|
|
|
|
|
|
|
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 {
|
|
|
|
|
2016-11-26 02:46:55 +08:00
|
|
|
console.log('// Removing "'+email+'" from MailChimp list…'); // eslint-disable-line
|
2016-05-23 09:07:13 +02:00
|
|
|
|
|
|
|
var api = new MailChimp(apiKey);
|
|
|
|
var subscribeOptions = {
|
|
|
|
id: listId,
|
|
|
|
email: {"email": email},
|
2016-05-25 09:30:29 +02:00
|
|
|
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
|
|
|
};
|
|
|
|
|
2016-05-25 08:52:04 +02:00
|
|
|
// unsubscribe user
|
2016-05-23 09:07:13 +02:00
|
|
|
var subscribe = api.call('lists', 'unsubscribe', subscribeOptions);
|
|
|
|
|
2016-05-25 08:52:04 +02:00
|
|
|
// mark user as unsubscribed
|
2016-12-07 15:30:40 +09:00
|
|
|
Users.setSetting(user, 'newsletter_subscribeToNewsletter', false);
|
2016-05-23 09:07:13 +02:00
|
|
|
|
2016-11-26 02:46:55 +08:00
|
|
|
console.log("// User unsubscribed"); // eslint-disable-line
|
2016-05-23 09:07:13 +02:00
|
|
|
|
2016-12-27 11:44:35 +01:00
|
|
|
return {actionResult: 'unsubscribed', ...subscribe};
|
2016-05-23 09:07:13 +02:00
|
|
|
|
|
|
|
} catch (error) {
|
|
|
|
throw new Meteor.Error("unsubscription-failed", error.message);
|
|
|
|
}
|
|
|
|
} else {
|
2016-11-26 02:46:55 +08:00
|
|
|
throw new Meteor.Error("Please provide your MailChimp API key and list ID");
|
2016-05-23 09:07:13 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-11-17 11:38:10 -06:00
|
|
|
export default MailChimpList;
|