Vulcan/packages/nova-newsletter/lib/server/mailchimp/mailchimp_list.js
Comus Leong 464e20a96c eslint & clean up code, also fixed some bugs (#1515)
* [eslint] update eslint rules & add .eslintignore to ignore non-ready nova packages

* [clean-up] nova-voting

* [clean-up] [bug] nova-users: missing user parameter

* [clean-up] nova-users

* [clean-up] nova-subscribe

* [clean-up] nova-settings

* [clean-up] nova-rss

* [clean-up] [bug] nova-posts: correct UsersRemoveDeletePosts

* [clean-up] nova-posts

* [clean-up] nova-notifications

* [clean-up] [bug] nova-newsletter: no error.message on throw error

* [clean-up] nova-newsletter

* [clean-up] nova-lib

* [clean-up] nova-kadira

* [clean-up] nova-inject-data

* [clean-up] nova-getting-started

* [clean-up] nova-forms

* [clean-up] nova-events

* [clean-up] [bug] nova-embedly: no FlowRouter

* [clean-up] nova-embedly

* [clean-up] nova-email-templates

* [clean-up] nova-email

* [clean-up] nova-debug

* [clean-up] nova-core

* [clean-up] [bug] nova-comments: correct UsersRemoveDeleteComments

* [clean-up] nova-comments

* [clean-up] [bug] nova-cloudinary: use Telescope.settings.collection instand

* [clean-up] nova-cloudinary

* [clean-up] nova-categories

* [clean-up] nova-base-components

* [clean-up] nova-api

* [eslint] extends react recommended

* [clean-up] for jsx files

* [eslint] extends meteor recommended

* i forgot this one little change
2016-11-25 13:46:55 -05:00

104 lines
2.9 KiB
JavaScript

import Telescope from 'meteor/nova:lib';
import Users from 'meteor/nova:users';
import MailChimp from './mailchimp_api.js';
const MailChimpList = {};
MailChimpList.add = function(userOrEmail, confirm, done){
var apiKey = Telescope.settings.get('mailChimpAPIKey');
var listId = Telescope.settings.get('mailChimpListId');
var user, email;
confirm = (typeof confirm === 'undefined') ? false : confirm; // default to no confirmation
// 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…'); // eslint-disable-line
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.subscribed', true);
}
console.log("// User subscribed"); // eslint-disable-line
return subscribe;
} catch (error) {
throw new Meteor.Error("subscription-failed", error.message);
}
} else {
throw new Meteor.Error("Please provide your MailChimp API key and list ID");
}
};
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+'" from MailChimp list…'); // eslint-disable-line
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)
};
// unsubscribe user
var subscribe = api.call('lists', 'unsubscribe', subscribeOptions);
// mark user as unsubscribed
Users.methods.setSetting(user._id, 'newsletter.subscribed', false);
console.log("// User unsubscribed"); // eslint-disable-line
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");
}
};
export default MailChimpList;